TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
3  
@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

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

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
    
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
up vote 3 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
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\@secondoft‌​wo\fi{Was X}{Was not X}. – O. R. Mapper Oct 1 '15 at 12:01

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

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.