3

In the following fragment, my use of \ifthenelse in a \newcommand adds a space:

\documentclass[letterpaper]{article}

\usepackage{ifthen}
\usepackage{graphicx}

\newboolean{color-version}
\setboolean{color-version}{false}

\newcommand{\figsubdir}[0]{%
\ifthenelse{\boolean{color-version}}{color}{bw}%
}

\begin{document}

../figures/GAelectrodynamics/\figsubdir/twoParameterDifferentialFig1

\end{document}

In Reducing space after a result of a command I see to add % signs after all the newlines, and I've done so, but it still seems to be there. This shows up more visibly, if the path above is changed to an \includegraphics statement, such as:

\includegraphics[totalheight=0.4\textheight]{../figures/GAelectrodynamics/\figsubdir/twoParameterDifferentialFig1}

With this \includegraphics, the extra rogue space causes a " Missing endcsname inserted." error, and compilation bombs. If I eliminate the ifthenelse use, using an hardcoded macro like:

\newcommand{\figsubdir}[0]{%
bw%
}

Then there is no extra space (and the \includegraphics also works.) But that defeats the purpose of ifthenelse.

Latex version:

  • texlive 2019 (Ubuntu 20.04.1, in a Windows WSL2 VM)
  • ifthen 2014/09/29 v1.1c Standard LaTeX ifthen package (DPC)
3
  • 1
    I see no space, but \ifthenelse is not expandable, you can't use it in a path for a graphic. Commented Nov 22, 2020 at 21:11
  • What is meant by "not expandable"? Commented Nov 22, 2020 at 21:14
  • 1
    consider a simpler example that also does not work \includegraphics{../figures/GAelectrodynamics/\def\zzz{bw}\zzz/twoParameterDifferentialFig1} this does not exapnd to the required path, it contains internal definitions that need to be executed. \ifthenelse makes many such internal definitions while making its tests. Commented Nov 22, 2020 at 21:53

1 Answer 1

7

There is no space and I asked TeX in order to confirm it: its response was

....\OT1/cmr/m/n/10 .
....\OT1/cmr/m/n/10 .
....\OT1/cmr/m/n/10 /
....\OT1/cmr/m/n/10 ^^L (ligature fi)
....\OT1/cmr/m/n/10 g
....\OT1/cmr/m/n/10 u
....\OT1/cmr/m/n/10 r
....\OT1/cmr/m/n/10 e
....\OT1/cmr/m/n/10 s
....\OT1/cmr/m/n/10 /
....\OT1/cmr/m/n/10 G
....\OT1/cmr/m/n/10 A
....\OT1/cmr/m/n/10 e
....\OT1/cmr/m/n/10 l
....\OT1/cmr/m/n/10 e
....\OT1/cmr/m/n/10 c
....\OT1/cmr/m/n/10 t
....\OT1/cmr/m/n/10 r
....\OT1/cmr/m/n/10 o
....\kern0.27779
....\OT1/cmr/m/n/10 d
....\OT1/cmr/m/n/10 y
....\OT1/cmr/m/n/10 n
....\OT1/cmr/m/n/10 a
....\OT1/cmr/m/n/10 m
....\OT1/cmr/m/n/10 i
....\OT1/cmr/m/n/10 c
....\OT1/cmr/m/n/10 s
....\OT1/cmr/m/n/10 /
....\OT1/cmr/m/n/10 b
....\kern-0.27779
....\OT1/cmr/m/n/10 w
....\OT1/cmr/m/n/10 /
....\OT1/cmr/m/n/10 t
....\kern-0.27779
....\OT1/cmr/m/n/10 w
....\kern-0.27779
....\OT1/cmr/m/n/10 o
....\OT1/cmr/m/n/10 P
....\kern-0.27779
....\OT1/cmr/m/n/10 a
....\OT1/cmr/m/n/10 r
....\OT1/cmr/m/n/10 a
....\OT1/cmr/m/n/10 m
....\OT1/cmr/m/n/10 e
....\OT1/cmr/m/n/10 t
....\OT1/cmr/m/n/10 e
....\OT1/cmr/m/n/10 r
....\OT1/cmr/m/n/10 D
....\OT1/cmr/m/n/10 i
....\OT1/cmr/m/n/10
                     (ligature ff)
....\OT1/cmr/m/n/10 e
....\OT1/cmr/m/n/10 r
....\OT1/cmr/m/n/10 e
....\OT1/cmr/m/n/10 n
....\kern-0.27779
....\OT1/cmr/m/n/10 t
....\OT1/cmr/m/n/10 i
....\OT1/cmr/m/n/10 a
....\OT1/cmr/m/n/10 l
....\OT1/cmr/m/n/10 F
....\OT1/cmr/m/n/10 i
....\OT1/cmr/m/n/10 g
....\OT1/cmr/m/n/10 1

and we only see implicit kerns between o and d, b and w and so on.

However, the working of \ifthenelse doesn't allow it to do the job you intend to. There are better ways. For instance you could load etoolbox and do

\usepackage{etoolbox}

\newtoggle{color-version}
%\toggletrue{color-version}% uncomment if you want the color version
\newcommand{\figsubdir}{\iftoggle{color-version}{color}{bw}}

or hand make your test:

\newif\ifcoloroutput
%\coloroutputtrue % uncomment if you want the color version
\newcommand{\figsubdir}{\ifcoloroutput color\else bw\fi}

Perhaps more simply

\newcommand{\figsubdir}{bw}
%\renewcommand{\figsubdir}{color}% uncomment if you want the color version
3
  • 1
    +1: I had to read "There is no space and I asked TeX to be sure: its response was" twice before I understood it :). Commented Nov 22, 2020 at 21:50
  • @Dr.ManuelKuehner Changed: is it better now?
    – egreg
    Commented Nov 22, 2020 at 22:18
  • Yes :). And I didn't meant ti criticize since I assumed that you are making it sound funny on purpose. Commented Nov 22, 2020 at 22:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .