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

  • It appears that \nograph is \protected. \edef\test{\nograph} fails while \def\test{\nograph} can be printed. – John Kormylo Jul 18 '17 at 22:07
  • It's not a bug; it's how \ifthenelse works. – egreg Jul 18 '17 at 22:13
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

  • 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 '17 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.

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.