4

In some hand-me-down tex i inherited, a control sequence is used as a variable, for instance:

\def\accessflavor{ats}

it is used to generate postfixes on included tex files (called, i.e., image01_ats) and the like.

I want to use it in a conditional structure, i. e. something like

\if\accessflavor ats
This text is only shown if the Flavor of Access is 'ats'.
\fi

I tried a a lot of variations, including various brackets, escape sequences and macros, but i cannot get it to work. Mostly, not even an error message appears. The online examples i found either deal with a more specific IF, like \ifodd, \ifx, etc., or more (seemingly) complex cases like testing two macros for identity.

Can somebody clear up the usage for me?

  • I can recommend the multiaudience package which allows you to separate some parts of your text for different "audiences" and provides higher-level switches. As part of this, you can use its \CurrentAudience variable to get the audience you are currently writing for (e.g. for file names). – TeXnician Apr 15 at 14:57
3
\documentclass{article}
\def\atscomparetoken{ats}
\newcommand\domytest{%
  \ifx\atscomparetoken\accessflavor MATCHES ats\else DOES NOT MATCH ats\fi
}
\begin{document}
\def\accessflavor{ats}
\domytest

\def\accessflavor{pdq}
\domytest
\end{document}

enter image description here

  • Thank you! Can you explain what is going on? If i understand it correctly, you def-ed atscomparetoken so that the ifx could work on two tokens - is there a reason you packed the whole thing into a command? Also, do you know why one needs the string as an expansion inside a token (for the ifx case it is clear, but why dies if not work?) – bukwyrm Apr 15 at 15:22
  • @bukwyrm Correct, the \ifx works on two tokens, and the comparison is whether the substitution text (one level of expansion) for the two tokens matches or not. The problem with trying to do this with \if is that \if fully expands and then compares the first two tokens; thus, \if\accessflavor ats expands to \if atsats and a is compared to t, with the remaining sats extraneous. – Steven B. Segletes Apr 15 at 15:50
2

If you want to maintain the \if...\else...\fi structure, you can use

\documentclass{article}
\usepackage{pdftexcmds}

\makeatletter
\newcommand\flavor[1]{%
  TT\fi
  \ifnum\pdf@strcmp{#1}{\accessflavor}=\z@
}
\makeatother

\begin{document}

\def\accessflavor{ats}

Shown

\if\flavor{ats}
This text is only shown if the Flavor of Access is `ats'.
\fi

\def\accessflavor{notats}

Not shown

\if\flavor{ats}
This text is only shown if the Flavor of Access is `ats'.
\fi

\end{document}

The usage of pdftexcmds is to obtain engine independence, so the code works with pdftex, luatex and xetex.

The trick is that \if expands tokens.

Note that this can be used inside other conditionals.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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