Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Macros to append text to list

3 views
Skip to first unread message

BoxingDay

unread,
May 27, 2003, 8:43:31 PM5/27/03
to
I'm trying to do what I would have though was a relatively common
operation - write a macros that takes a piece of text as an argument
and appends that text to the string formed by all previous macro
calls, possibky with a little bit of delimitting text insert in
between. For example, I want to be able to have towards the top of
the doument macro calls like:

\choice{Pizza}
\choice{Ice Cream}
\choice{Sushi}

And then later be able to include something like:

Your choices are: \choices

And the output would be:

Your choices are: Pizza, Ice Cream, Sushi

Suggestions or pointers to help? Is there a standard idiom for this
sort of thing?

Jay

Donald Arseneau

unread,
May 27, 2003, 11:48:57 PM5/27/03
to
jbloo...@sc.rr.com (BoxingDay) writes:

> \choice{Pizza}
> \choice{Ice Cream}
> \choice{Sushi}
>
> And then later be able to include something like:
>
> Your choices are: \choices


\gdef\choices{}

\newcommand\choice[1]{\relax
\ifx\choices\@empty\else
\g@addto@macro\choices{, }% add separators, conditionally
\fi
\g@addtomacro\choices{#1}}


Donald Arseneau as...@triumf.ca

Cameron, Charles B.

unread,
May 28, 2003, 8:07:45 AM5/28/03
to
Donald Arseneau wrote:

It may not be obvious to some that the above code from Donal Arseneau
needs to be surrounded by \makeatletter and \makeatother to work inside
ordinary LaTeX documents. Also, there is an "@" symbol missing in the
second instance of "\g@addto@macro".

A modification of the macro "\choice" might be helpful. Here is the
modified and corrected source code with the addition of macro
"\finalchoice":

\makeatletter
\gdef\choices{}

\newcommand\choice[1]{\relax
\ifx\choices\@empty\else
\g@addto@macro\choices{, }% add separators, conditionally
\fi

\g@addto@macro\choices{#1}}

\newcommand\finalchoice[1]{\relax
\ifx\choices\@empty\else
\g@addto@macro\choices{, and }% conditionally add a separator and the
word "and"
\fi
\g@addto@macro\choices{#1}
}

\makeatother

The usage is like this:

\choice{dogs}
\choice{cats}
\choice{pigs}
\finalchoice{cows}

Here are your choices: \choices.

and the result is:

Here are your choices: dogs, cats, pigs, and cows.


Charles B. Cameron


BoxingDay

unread,
May 28, 2003, 4:32:56 PM5/28/03
to
"Cameron, Charles B." <came...@aplcenmp.apl.jhu.edu> wrote in message news:<3ED4A691...@aplcenmp.apl.jhu.edu>...
> Donald Arseneau wrote:
>

[helpful stuff]

Thanks to both of you. The code was going in a class file anyway, so
the the @ business was not a problem, and witha little trial and error
I found the typo in \g@addto@macro.

Quick followup: Can anybody point me to an online source that
documents (la)tex programming at this level? I've googled and I've
googled, and can't find anything that offers much on latex programming
beyond newcommand.

Thanks again,
Jay

Guy Worthington

unread,
May 29, 2003, 9:19:22 AM5/29/03
to
> Quick followup: Can anybody point me to an online source that
> documents (la)tex programming at this level? I've googled and I've
> googled, and can't find anything that offers much on latex programming
> beyond newcommand.

A corporate body for LaTeX documentation doesn't exist. No single
book, source, or archive can offer what you want.


0 new messages