6
\documentclass{article}
\usepackage{ifthen}
\usepackage[final,pdftex]{graphicx}
\newboolean{GraphicsDraft}
\setboolean{GraphicsDraft}{true}
\newcommand{\nograph}{%
  \ifthenelse{\boolean{GraphicsDraft}}
  {true}%
  {false}%
}%nograph

\begin{document}
\includegraphics*[draft=\nograph]{pic} % This gives an error
%
\renewcommand\nograph{\ifGraphicsDraft true\else false\fi}
\includegraphics*[draft=\nograph]{pic} % This is OK
\end{document}

An ifthenelse is problematic whereas the \ifGraphicsDraft works

Am I doing something stupid or is this a real bug

2
  • It appears that \nograph is \protected. \edef\test{\nograph} fails while \def\test{\nograph} can be printed. Jul 18, 2017 at 22:07
  • It's not a bug; it's how \ifthenelse works.
    – egreg
    Jul 18, 2017 at 22:13

2 Answers 2

2

You need an expandable test, which \ifthenelse isn't.

\documentclass{article}
\usepackage[final]{graphicx}
\usepackage{etoolbox}

\newtoggle{GraphicsDraft}
\togglefalse{GraphicsDraft}

\newcommand{\nograph}{%
  \iftoggle{GraphicsDraft}{true}{false}%
}

\begin{document}

\includegraphics[draft=\nograph]{example-image-a}

\toggletrue{GraphicsDraft}

\includegraphics[draft=\nograph]{example-image-b}

\end{document}

enter image description here

1
  • Thank you all for pointing this out to me. I hesitate to use etoolbox for just one toggle, especially where \newcommand\nograph{\ifGraphicsDraft true\else false\fi} does what is expected. @David Carlisle Being part of a bigger machinery, I can't easily use your suggestion \newcommand\nograph{true} That's the price one has to pay for a minimal example:-) But I'll reconsider that at due time.
    – Jack
    Jul 19, 2017 at 16:34
1

The value of the draft key has to expand to true or false, and \ifthenelse does not work via expansion. It fails for the same reason that

\includegraphics[draft=\def\tmp{true}\tmp]

fails, even though \def\tmp{true}\tmp would typeset true.

You could, as you indicate use

\renewcommand\nograph{\ifGraphicsDraft true\else false\fi}

or simpler not have a switch at all and instead of having

\setboolean{GraphicsDraft}{true}

or

\setboolean{GraphicsDraft}{true}

Just have

\newcommand\nograph{true}

or

\newcommand\nograph{false}

Then \nograph directly expands to true or false.

You must log in to answer this question.

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