Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
    
possible duplicate of How to make a "condition" in a macro? –  Marco Daniel Dec 15 '11 at 18:55
4  
See also Why is the ifthen package obsolete? –  rdhs Dec 15 '11 at 18:59
2  
I think it is duplicated because with the test ifstrequal of the package etoolbox everythink works well. –  Marco Daniel Dec 15 '11 at 19:33
2  
@MarcoDaniel There's still the warning about "Token not allowed in a PDF string" (\texorpdfstring may come handy). –  egreg Dec 15 '11 at 20:44
    
@egreg: Sorry I didn't mentioned this. –  Marco Daniel Dec 15 '11 at 20:45
add comment

4 Answers

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

share|improve this answer
add comment

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}
share|improve this answer
add comment

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.

share|improve this answer
add comment
up vote 2 down vote accepted

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.

share|improve this answer
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
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.