I would like to create a macro to get the value of current chapter or
part or section, etc. by using another macro, let say \level. So I
created like this:
\def\level{chapter}
\def\printlevel{\expandafter\the\level}
But it doesn't work, because the macro \the is directly expanded
although I asked TeX to suspend it via \expandafter macro.
Does anybody know how I can solve this problem?
The problem is that to see what is to be suspended, in your words, TeX has
to find the end of the command (token). Once it has decided the next
command is \the, it will not change its mind to make it \thechapter. The
solution is to use the construction \csname the\level\endcsname.
\csname...\endcsname mostly works like writing \..., only that the name
can be created programmatically instead of being written directly in the
input. (Just in case you make a typo: \csname...\endcsname works like
\relax if the macro you're trying to call isn't defined, so you'll not get
the usual "undefined control sequence" errors.)
HTH
Ulrich
Thank you. I can fix my scope test like this:
\def\level{chapter}
\def\scopetest#1{%
\def\levelnum{\csname the\level\endcsname}%
\ifnum\levelnum>0 #1\fi
}
where the \level is a variable.
If you want a number, use \value instead:
\def\level{chapter}
\def\scopetest#1{%
\ifnum\value{\level}>0 #1\fi
}
--
Change “LookInSig” to “tcalveu” to answer by mail.
>
>Thank you. I can fix my scope test like this:
>
>\def\level{chapter}
>\def\scopetest#1{%
> \def\levelnum{\csname the\level\endcsname}%
> \ifnum\levelnum>0 #1\fi
>}
If you have a document class where \thechapter is a
roman numeral, you might get
\ifnum I>0 #1\fi
which fails badly.
Use \thechapter _only_ when you want the tokens that would
be printed, use \value{chapter} when you want to deal with
a number (assignments, comparisons, etc.).
Dan
To reply by email, change LookInSig to luecking
And if you were actually trying to get the *text* of the chapter
heading, rather than the chapter number, this will just get you the
number. To reproduce the textual value of the chapter heading would mean
accessing it from where it gets put when the chapter starts, which is
likely to be \leftmark or \rightmark, depending on the style of running
heads you are using, if any.
///Peter