9

I have a command that uses package ifthen. I use the command in section titles. I have to protect the command because otherwise I obtained the error Undefined control sequence \equal. So far, so good. Now I started using package hyperref, and the error is here again. How could I solve it? An example follows. Thanks.

\documentclass{article}
\usepackage{ifthen}
\usepackage{hyperref} % remove this line => no error
\newcommand{\something}[1]{Was something}
\newcommand{\somethingelse}[1]{\ifthenelse{\equal{#1}{X}}{Was X}{Was not X}}
\begin{document}
     \tableofcontents
     \section{\something{}}
     \section{\protect\somethingelse{}} % remove this line => no error
\end{document}
5

The etoolbox package has a command that I sometimes use to deal with premature expansion and Token not allowed in a PDF:

\documentclass{article}
\usepackage{etoolbox}
\usepackage{hyperref}
\makeatletter
\newcommand{\something}[1]{Was something}
\newcommand{\somethingelse}[1]{%
  \ifnum\pdfstrcmp{#1}{X}=\z@\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
  {Was X}{Was not X}%
}
\makeatother

\begin{document}
     \tableofcontents
     \section{\something{}}
     \section{\protecting{\somethingelse{X}}}
     \section{\protecting{\somethingelse{Y}}}
\end{document}

Another solution is

\documentclass{article}
\usepackage{etoolbox}
\usepackage{hyperref}
\makeatletter
% In case the platform has no \pdfstrcmp:
\ifdef\pdfstrcmp
  {\let\asdel@strcmp\pdfstrcmp}
  {\usepackage{pdftexcmds}\let\asdel@strcmp\pdf@strcmp}
\let\then\iffalse
\newcommand*\ifstrcmp{}
\def\ifstrcmp#1\with#2\then{\ifnum\asdel@strcmp{#1}{#2}=\z@}
\newcommand{\somethingelse}[1]{\ifstrcmp#1\with X\then Was X\else Was not X\fi}
\makeatother

\begin{document}
  \tableofcontents
  \section{\protecting{\somethingelse{X}}}
  \section{\protecting{\somethingelse{Y}}}
\end{document}

enter image description here

4

As a workaround, you can use \IfEq from the xstring package instead:

\documentclass{article}
\usepackage{xstring}
\usepackage{hyperref}
\newcommand{\something}[1]{Was something}
\newcommand{\somethingelse}[1]{\IfEq{#1}{X}{Was X}{Was not X}}
\begin{document}
     \tableofcontents
     \section{\something{}}
     \section{\protect\somethingelse{}} % remove this line => no error
\end{document}
  • Doesn't seem to work in certain situations, using this solution in my document produces ! Argument of \M@sect has an extra }.. – O. R. Mapper Oct 1 '15 at 11:54
  • @O.R.Mapper And which might be your document class? – Gonzalo Medina Oct 1 '15 at 14:14
  • memoir in this case. I meanwhile managed to use \ifstrequal from etoolbox as suggested in another answer. – O. R. Mapper Oct 1 '15 at 14:14
3

I finally used the tests \ifstrequal and \ifstrempty from package etoolbox. As a side effect, now there is no need to \protect the commands within the titles. I think that package xstring also solved the problem, but I needed additional tests for doing other tasks. In summary, I switched from package ifthen to package etoolbox.

  • 1
    You might want to accept Achmed Musa's answer about etoolbox then, since it seem the most useful for you. Note that you do not get the usual 15rep for self-accepted answers anyway. – Martin Scharrer Jan 15 '12 at 13:42
  • 1
    I think my answer is the most useful answer for others. Do you think I should accept the most useful answer just for me? About rep, I do not care. – ASdeL Jan 15 '12 at 16:37
  • \ifstrequal does not seem to expand the macro inside. I could not get it run with \csname #1Suffix\endcsname – koppor Dec 12 '13 at 23:14
  • @MartinScharrer: As a user who could just solve his problem with this answer, I agree this answer is much more useful than the other answer above. The answer above provides helpful explanations, but the question on how to fix \ifthenelse{\equal{... in a command is most clearly solved by a command like \ifstrequal{a}{b}{true}{false} that's analogous to other programming languages, rather than a (in comparison to that solution) cryptic \ifnum\pdfstrcmp{#1}{X}=\z@\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi{Was X}{Was not X}. – O. R. Mapper Oct 1 '15 at 12:01
2

Using the LaTeX3 packages:

\usepackage{expl3}
\ExplSyntaxOn
\cs_set:Npn \somethingelse #1
  {
   \str_if_eq:nnTF{#1}{X}{Was~X}{Was~not~X}
  }
\ExplSyntaxOff

This even gives no warning about non allowed tokens in PDF strings.

However, if this command is made in order to write different things in the section title than in the page header, a different strategy should be used.

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.