Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have a bunch of tokens with default values. I need to test each of these and print a message in case they are still the default value. I am able to do this manually as shown below. Now when I attempt to write a macro for this I run into problems. I want to be able to report the name of the token that was not set so attempted to pass in the name of the token and use \the\#1 to get the value. I am thinking there is someting basic I am missing and have tried a bunch of things but still am not able to make this work. Any ideas?

\documentclass[12pt]{article}
\usepackage{ifthen}

\newtoks{\MyParameter}
\MyParameter={X}         % Set default value

\newcommand{\TestIfGivenValue}[2]{%
\ifthenelse{\equal{\the\#1}{#2}}{%
\warningbox{B: Please set the value of $backslash$#1}%
}{%
    B: Yes, the \textbackslash MyParameter has a non default value. 
}}


\begin{document}

% Manual method: works great
\ifthenelse{\equal{\the\MyParameter}{X}}{%
    A: Please set the value of \textbackslash MyParameter \\%
}{
    A: Yes, the \textbackslash MyParameter has a non default value. 
}

% Now attempt to wrap this in a new command
\TestIfGivenValue{MyParameter}{X}

\end{document}
share|improve this question

1 Answer 1

up vote 3 down vote accepted

TeX has to see names as 'tokens': you can't put \#1 and expect TeX to turn it into a control sequence. So you should either do

\newcommand{\TestIfGivenValue}[2]{%
\ifthenelse{\equal{\the#1}{#2}}{%
\warningbox{B: Please set the value of \noexpand#1}%
}{%
    %B: Yes, #1 has a non default value. 
}}

and use it with the control sequence: \TestIfGivenValue\myvalue{something}, or construct the name using \csname:

\newcommand{\TestIfGivenValue}[2]{%
\ifthenelse{\equal{\expandafter\the\csname#1\endcsname}{#2}}{%
\warningbox{B: Please set the value of \expandafter\noexpand\csname#1\endcsname}%
}{%
    %B: Yes, #1 has a non default value. 
}}

which will work as: \TestIfGivenValue{myvalue}{something}.

The \csname ... \endcsname construct turns myvalue into the token \myvalue, which is what TeX needs to see for everything to work. I've also used \expandafter in the second version of the answer, to make sure that \the 'sees' the correct input.

share|improve this answer
    
@Joseph: in fact, the \expandafter before \the seems unnecessary: I believe that \the expands whatever it sees until it cannot anymore. If what remains is a register, then \the reads it, otherwise, error. –  Bruno Le Floch Mar 26 '11 at 11:02
    
This worked great for me. I did have to add \relax at the end or each line, but otherwise excellent solution. Thank You. –  Peter Grill Mar 26 '11 at 21:05
    
@Peter: Interesting. Please let's see where you put the \relax'es. It may have to do with spurious spaces, the need for which isn't apparent in Joseph's solution. –  Ahmed Musa Mar 26 '11 at 23:28
    
Oppps. Sorry about that. I did NOT have to adjust the solution above, just when invoking this macro, I had to add a \relax at the end of each call. I do not understand why they were necessary, but after I included that things worked as I needed. –  Peter Grill Mar 26 '11 at 23:45
    
@Bruno: That's interesting. I didn't find this behaviour of \the documented in the TeXbook or in TeX by Topic. Do you know any reference? –  Hendrik Vogt Mar 27 '11 at 9:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.