Generalxifthen | Scope Issue

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
jbgrad
Posts: 5
Joined: Sat Nov 17, 2012 4:31 am

xifthen | Scope Issue

Post by jbgrad »

In changing notation in my thesis, I thought it would be convenient to change a command I named \XDir. However, the most convenient way that I knew to change it was to use an if/else construct as shown in the following MWE example. The commented line doesn't work (i.e. gives the error that \equal is unknown) and was therefore replaced with the line immediately preceding it.

Code: Select all

\documentclass[12pt,twoside]{report}
\usepackage{xifthen}
\newcommand{\XDir}[1]
{
  \ensuremath{\ifthenelse{\equal{#1}{1}}{X}{Y}}
}
\begin{document}
\section[Going in direction $X$]{ Going in direction $\mathbf{X}$ }
%\section[Going in direction \XDir{1}]{ Going in direction $\mathbf{\XDir{1}}$ }
We will go in direction \XDir{1}.
\end{document}
Another, slightly more complex MWE follows, similarly with the error-giving line commented out and replaced by the line immediately preceding it:

Code: Select all

\documentclass[12pt,twoside]{report}
\usepackage{xifthen}
\newcommand{\XDir}[1]
{
  \ensuremath{\ifthenelse{\equal{#1}{1}}{X}{Y}}
}
\newcommand{\myfigurecaption}[3][]
{
  \caption{#3}
  \label{#2}
  \ifthenelse{\isempty{#1}}
  {\addcontentsline{lot}{figure}{Figure \ref{#2}: #3}}
  {\addcontentsline{lot}{figure}{Figure \ref{#2}: #1}}
}
\begin{document}
We will go in direction \XDir{2}.
\begin{figure}
  \myfigurecaption[Let's look at direction $Y$]{fig:myfigure}{Let's look at direction $\mathbf{Y}$}
%  \myfigurecaption[Let's look at direction $\XDir{2}$]{fig:myfigure}{Let's look at direction $\mathbf{\XDir{2}}$}

\end{figure}
\end{document}
I don't understand why the command \XDir sometimes works and sometimes does not. Perhaps my misunderstanding stems from the fact that I am a C++ trained (functional and OOP) individual who doesn't understand the inner workings of macro expansion.

I could manually go through and make changes like above (with the commented lines), but I would like to learn a more elegant, proper solution.

Thanks

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

xifthen | Scope Issue

Post by cgnieder »

\XDir fails in the moving argument of \section (i.e. the one that gets written to the ToC) since your definition of \XDir is not expandable. The same is true for \caption. It's argument (or if given the optional one) is also a moving argument, i.e., its contents get written to an auxiliary file to be able to create the LoF (or the LoT).

The way around is to protect the command in the moving arguments by adding \protect in front of it.

As a side note: your definition contains spurious spaces:

Code: Select all

\newcommand{\XDir}[1]
{% here is a space if you don't comment the line
  \ensuremath{\ifthenelse{\equal{#1}{1}}{X}{Y}}% here is a space if you don't comment the line
}
I also would leave out the \ensuremath as I believe that stuff that's supposed to be in math mode should be clearly mark as in math mode in the running text. Feel free to add it again if you insist.

The following works:

Code: Select all

\documentclass[12pt,twoside]{report}
\usepackage{xifthen}
\newcommand{\XDir}[1]{\ifthenelse{\equal{#1}{1}}{X}{Y}}
\begin{document}
\section[Going in direction $\protect\XDir{1}$]{ Going in direction $\mathbf{\XDir{1}}$ }
We will go in direction $\XDir{1}$.
\end{document}
Another way would be to use a TeX conditional as this would make the command expandable:

Code: Select all

\documentclass[12pt,twoside]{report}
\newcommand*\XDir[1]{\ifx#11\relax X\else Y\fi}
\begin{document}
\tableofcontents
\section[Going in direction $\XDir{1}$]{ Going in direction $\mathbf{\XDir{1}}$ }
We will go in direction $\XDir{1}$.
\end{document}
Regards
site moderator & package author
Post Reply