In this script:


  my directory is \mydirectory

%    \includegraphics[width=0.45\textwidth]{\protect\mydirectory/mypic.pdf}%                                                                  
    \caption{picture in \protect\mydirectory}



I \protect the fragile variable \mydirectory in \caption - see comments to How to protect a fragile command in a moving argument? The case of macro containing \ifthenelse in \caption.

However, when I provide:


the classical symptoms of a fragile \ifthenelse appear:

! Undefined control sequence.
<argument> \equal 

that are not cured even by \protect:


How to protect a fragile variable in a file path?

  • 3
    Ifthenelse is not expandable. You can't use it in this place. – Ulrike Fischer Oct 15 '18 at 11:29
  • 3
    I think it should by now be clear to you that \ifthenelse doesn't work by pure expansion. A method with \ifstreq similar to one I suggested for another case will work flawlessly. – egreg Oct 15 '18 at 11:41
  • 1
    This can not work. – David Carlisle Oct 15 '18 at 12:28

I would not use ifthenelse but if you use it (or any other test mechanism) you want \mydirectory to expand to the directory name, not to a complicated sequence of assignments and tests that may eventually typeset a directory name.



can not possibly work, you would need


so that the definition of \mydirectory is either dir1 or dir2.

  • As you define \mydirectory in {\def\mydirectory{dir1}} will not \mydirectory live only inside the scope {}? – Viesturs Oct 15 '18 at 12:36
  • 1
    @Viesturs no: {}: that delimit macro arguments do not form a group. – David Carlisle Oct 15 '18 at 12:37
  • Is it advisable to use \newcommand in place of \def? – Viesturs Oct 15 '18 at 14:25
  • 1
    @Viesturs well \newcommand wouldn't work after the first time, you could give it an initial definition and then use \renewcommand so it always redefines, but no point really, it just makes it check every time that the command is already defined, when you have set it up to be defined. – David Carlisle Oct 15 '18 at 14:27

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.