[OS X TeX] counters and commands
Ross Moore
ross at ics.mq.edu.au
Fri Mar 3 18:19:01 EST 2006
Hello Friedrich,
On 04/03/2006, at 7:23 AM, Rechtsanwalt Friedrich Vosberg wrote:
> Morning.
>
> The example that follows prints the names and the professions of
> some people. I want to get the profession of each of them each in
> an other macro \profA, \profB, \profC, .... I tried it the way in
> the commented lines, but it doesn't work.
>
> What must I do?
You are trying to generate macro-names automatically, according
to what other input is received within your document.
This is certainly possible, but requires TeX constructions with
which you may not be familiar --- yet.
The following variation of your example illustrates 2 slightly
different ways to achieve what I think you want:
\documentclass{article}
\newcounter{member}
\newcommand{\member}[2]{%
\def\name{#1}%
\def\prof{#2}%
\stepcounter{member}%
\printmember%
}
\makeatletter
\def\printmemberA{%
\framebox{No.\,\arabic{member}: \name\ (\prof)}%
\expandafter\edef\csname profno-\the\c at member\endcsname{\prof}%
}
\makeatother
\def\printmemberB{%
\framebox{No.\,\arabic{member}: \name\ (\prof)}%
\edef\thismember{\Alph{member}}%
\expandafter\edef\csname prof\thismember\endcsname{\prof}%
}
\begin{document}
\let\printmember\printmemberA
\noindent
\member{Bush}{President}
\member{Blair}{Prime Minister}
\member{Merkel}{Chancellor}
\par
\bigskip\noindent
The professions are:
%
\begin{itemize}
\item \csname profno-1\endcsname
\item \csname profno-2\endcsname
\item \csname profno-3\endcsname
\end{itemize}
\vspace{2cm}
\setcounter{member}{0}
\let\printmember\printmemberB
\noindent
\member{Bush}{President}
\member{Blair}{Prime Minister}
\member{Merkel}{Chancellor}
\par
\bigskip\noindent
The professions are:
%
\begin{itemize}
\item \profA
\item \profB
\item \profC
\end{itemize}
\end{document}
Note the use of TeX primitive commands:
\let
\edef
\csname .... \endcsname
\expandafter
BTW, there is no name-clash in using \member
and \newcounter{member} for different things.
This is because TeX refers to the counter internally
as \c at member .
However, it may lead to confusion in your own mind.
For example, if you mistakenly wrote: \arabic{\member}
then the error message returned by TeX would almost
certainly be unintelligible to you.
Just this week I debugged a colleague's problem.
He had written \fnsymbol{\footnote} and couldn't
see the reason behind TeX's error messages.
Hope this helps,
Ross
>
> ===== 8>< ====
> \documentclass{minimal}
> \newcounter{member}
> \newcommand{\member}[2]{%
> \def\name{#1}%
> \def\prof{#2}%
> \stepcounter{member}%
> \printmember%
> }
> \def\printmember{%
> \framebox{No.\,\arabic{member}: \name\ (\prof)}%
> % \def\profno\Alpha{member}{\prof}
> }
> \begin{document}
> \member{Bush}{President}
> \member{Blair}{Prime Minister}
> \member{Merkel}{Chancellor}
> \par
> The professions are:
> \begin{itemize}
> %\item \profnoA
> \item prof2
> \item prof3
> \end{itemize}
> \end{document}
> ===== ><8 =====
>
> TIA and kind regards. Friedrich
>
> --
> Rechtsanwalt Friedrich Vosberg
> Alt-Oetzsch 4, 04416 Markkleeberg
> Ruf: 0341 3502074 Mobil: 0174 1602887
> Fax: 0341 3502075 http://www.meinrechtsanwalt.com
------------------------------------------------------------------------
Ross Moore ross at maths.mq.edu.au
Mathematics Department office: E7A-419
Macquarie University tel: +61 +2 9850 8955
Sydney, Australia 2109 fax: +61 +2 9850 8114
------------------------------------------------------------------------
------------------------- Info --------------------------
Mac-TeX Website: http://www.esm.psu.edu/mac-tex/
& FAQ: http://latex.yauh.de/faq/
TeX FAQ: http://www.tex.ac.uk/faq
List Archive: http://tug.org/pipermail/macostex-archives/
More information about the MacOSX-TeX
mailing list