From: Wolfgang Schuster <schuster.wolfgang@googlemail.com>
To: mailing list for ConTeXt users <ntg-context@ntg.nl>
Subject: Re: Automated Quotation/Punctuation Placement
Date: Fri, 8 Jan 2010 17:51:08 +0100 [thread overview]
Message-ID: <28CEDF1F-4AFE-4B44-9A69-3355696FDC00@googlemail.com> (raw)
In-Reply-To: <4B475CF9.1050305@cyberdude.com>
Am 08.01.2010 um 17:27 schrieb Tad Ashlock:
> Hi All,
>
> I'm trying to create a command that will apply a consistent style to a word or phrase. For example, when documenting source code, I'd like to be able to mark variables with \Var{var_name}. Then if I want the variable names to be in mono, I can \def\Var#1{\type{#1}}. No problem there. If I want variable names to be in quotes, then \def\Var#1{\quote{#1}}.
>
> The problem is that in my ConTeXt code I'd write "This is \Var{var_name}, a variable." Which would get typeset as "This is 'var_name', a variable." where punctuation convention (at least in American English) would have the comma inside the quote like this: "This is 'var_name,' a variable."
>
> I've tried four different ways of implementing this, but none of them work consistently. Here's my last attempt:
>
> ==========================
> \startluacode
> function move_end_punctuation (text, punc, cmd_start, cmd_mid, cmd_end)
> context(cmd_start .. text .. cmd_mid)
> if string.find('.,!?', punc, 1, true) then
> context(punc .. cmd_end)
> else
> context(cmd_end .. ' ' .. punc)
> end
> end
> \stopluacode
>
> \def\Var#1#2{\ctxlua{move_end_punctuation([==[#1]==],[==[#2]==],
> '\\quote{\\type{','}','}')}}
>
> \starttext
>
> This is \Var{var_name}, a variable.
>
> \stoptext
> ==========================
>
> This works, until the \Var{} macro appears in the argument of another macro. For example, make the text:
>
> \framed{This is \Var{var_name}, a variable.}
>
> and the following error results:
> ==========================
> systems : begin file test.tex at line 16
> ! Missing $ inserted.
> <inserted text>
> $
> <to be read again>
> _
> l.1 ...spaces quote{unskip ignorespaces type{var_
> name}
> \Var ...=],[==[#2]==], '\\quote{\\type{','}','}')}
> l.18 \framed{This is \Var{var_name},
> a variable.}
> ?
> ==========================
>
> I think my problem has to do with parameter expansion, but I don't understand the intricacies enough to solve this. I flailed away, unsuccessfully, with various combinations of \unexpanded, \normalunexpanded, luaescapestring, etc.
>
> Does anyone have a solution or a pointer in the right direction?
\nonknuthmode % make '_' a normal character in text mode
\define[1]\Var
{\doifnextcharelse{,}{\doVar{#1,}\gobbleoneargument}{\noVar{#1}}}
\define[1]\doVar
{\mono{#1}}
\define[1]\noVar
{\mono{#1}%
\doifnextcharelse{;} % \autoinsertnextspace is no longer available in MkIV :(
{\donothing}
{\doifnextcharelse{.}
{\donothing}
{\space}}}
\starttext
This is \Var{var_name}, a variable.
This is \Var{var_name,} a variable.
\framed{This is \Var{var_name}, a variable.}
\stoptext
Wolfgang
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage : http://www.pragma-ade.nl / http://tex.aanhet.net
archive : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___________________________________________________________________________________
next prev parent reply other threads:[~2010-01-08 16:51 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-01-08 16:27 Tad Ashlock
2010-01-08 16:51 ` Wolfgang Schuster [this message]
2010-01-08 21:30 ` Tad Ashlock
2010-01-08 17:01 ` Khaled Hosny
2010-01-08 21:50 ` Tad Ashlock
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=28CEDF1F-4AFE-4B44-9A69-3355696FDC00@googlemail.com \
--to=schuster.wolfgang@googlemail.com \
--cc=ntg-context@ntg.nl \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).