[comp.text.tex] "Indirect" definitions in TeX

MBEATTIE@VAX.OXFORD.AC.UK (Malcolm Beattie) (03/09/91)

        I would like to be able to define things "indirectly" or
"dynamically" in TeX (in fact, AMSTeX, not LaTeX).  More precisely,
I would like to be able to do something like:
 
\document
\newcount\chap
\newcount\sect
\define\makemark#1{\define\label#1{\the\chap.\the\sect}}
\define\refermark#1{\label#1}

ASND@TRIUMFCL.BITNET (Donald Arseneau) (03/09/91)

Malcolm Beattie writes:
>    I would like to be able to do something like:
>    ...
>    \define\makemark#1{\define\label#1{\the\chap.\the\sect}}
>    ...
>    \makemark{fred}       % Here, e.g. \chap = 6 and \sect = 7
>    ...
>    Of course, this doesn't work because the #1 in \define\label#1
>    doesn't get replaced by \makemark's first parameter, but instead
>    gets interpreted as a new local parameter for \label itself.
 
Actually, it gets interpreted as part of the SYNTAX for \label;
\makemark{fred} redefines \label as if you typed \define\label fred{...
when what you want is \define\labelfred.  This happens because the
command name is tokenized when it is first read in, not when #1 gets
processed.
 
The soultion is to use \csname to create the right commandname;
but take care not to redefine \csname itself!  :
 
\define\makemark#1{\expandafter\define\csname label#1\endcsname{...
 
I left out the definition because I suspect that what Malcolm tried
won't work as expected.  We need to expand \the\chap.\the\sect when
\makemark is performed, not when the \labelfred is used.
 
I don't really know what \define in AMS-TeX does; maybe it is equivalent
to TeX's \edef, which is what we really want! but I suspect it does a
normal \def, which would cause \labelfred to always report the current
section, not the correct cross reference.  Thus, I would use:
 
\def\makemark#1{\expandafter\edef\csname label#1\endcsname{\the\chap.\the\sect}}
 
Donald Arseneau                      asnd@triumfcl.bitnet

braams@pttrnl.nl (Johannes L. Braams) (03/12/91)

In article <C7A48818E6007431@Post-Office.UH.EDU>, MBEATTIE@VAX.OXFORD.AC.UK (Malcolm Beattie) writes:
> 
>         I would like to be able to define things "indirectly" or
> "dynamically" in TeX (in fact, AMSTeX, not LaTeX).  More precisely,
> I would like to be able to do something like:
>  
> \document
> \newcount\chap
> \newcount\sect
> \define\makemark#1{\define\label#1{\the\chap.\the\sect}}
> \define\refermark#1{\label#1}

	What about
    \def\makemark#1{\expandafter\def\csname#1\endcsname{\thechap.\the\sect}}
-- 

	Johannes Braams

PTT Research Neher Laboratorium,        P.O. box 421,
2260 AK Leidschendam,                   The Netherlands.
Phone    : +31 70 3325051               E-mail : JL_Braams@pttrnl.nl
-------------------------------------------------------------------------------