5

Consider this script:

\documentclass{report}
\usepackage{ifthen}
\newcommand{\thesissize}{SHORT}
\begin{document}
   \setcounter{page}{
    %3
    \ifthenelse{
    \equal{\thesissize}{SHORT}
    }{3}{2}
  }
  hey
\end{document}

Compiling this script gives the error:

! Missing number, treated as zero.

I suspect that this is due to \ifthenelse not being evaluated at the time of executing \setcounter. How do I solve this?

6

you can't use \ifthenelse inside \setcounter. Change the order:

\documentclass{report}
\usepackage{ifthen}
\newcommand{\thesissize}{SHORT}
\begin{document}
   \ifthenelse{\equal{\thesissize}{SHORT}}
    {\setcounter{page}{3}}{\setcounter{page}{2}}


  hey
\end{document}

Expandable tests are possible with expl3 and etoolbox. For both you should store the reference text in a command:

\documentclass{report}
\usepackage{expl3,etoolbox}
\newcommand{\thesissize}{SHORT}
\newcommand{\shortsize}{SHORT}

\begin{document}
\ExplSyntaxOn
\setcounter{page}{\tl_if_eq:NNTF\thesissize\shortsize{3}{2}}
\ExplSyntaxOff

\setcounter{page}{\ifdefequal{\thesissize}{\shortsize}{3}{2}}

  hey
\end{document}
  • What is the reason? – Viesturs Apr 5 at 13:23
  • 6
    ifthenelse is not expandable. – Ulrike Fischer Apr 5 at 13:26
  • 1
    I added two more versions. – Ulrike Fischer Apr 5 at 13:52
2

Another version of a string comparison using the pdfTeX macro \pdfstrcmp. The following uses the pdftexcmds package to make it available to all engines under the same name:

\documentclass[]{article}

\usepackage{pdftexcmds}
\makeatletter
\newcommand\ifstreq[2]
  {%
    \ifnum\pdf@strcmp{#1}{#2}=0
  }
\makeatother

\newcommand\thesissize{SHORT}

\begin{document}
\setcounter{page}{\ifstreq{\thesissize}{SHORT}3\else2\fi}
hey
\end{document}

If a LaTeX syntax is preferred one can use

\documentclass[]{article}

\usepackage{pdftexcmds}
\makeatletter
\newcommand\ifstreq[2]
  {%
    \ifnum\pdf@strcmp{#1}{#2}=0
      \expandafter\@secondofthree
    \fi
    \@secondoftwo
  }
\providecommand\@secondofthree[3]{#2}
\makeatother

\newcommand\thesissize{SHORT}

\begin{document}
\setcounter{page}{\ifstreq{\thesissize}{SHORT}{3}{2}}
hey
\end{document}
1

No packages.

\documentclass{report}
\newcommand{\thesissize}{SHORT}
\begin{document}

\newcommand\tmp{SHORT}
\ifx\tmp\thesissize\relax\setcounter{page}{3}\else\setcounter{page}{2}\fi

\thepage

\renewcommand\tmp{NOT SHORT}
\ifx\tmp\thesissize\relax\setcounter{page}{3}\else\setcounter{page}{2}\fi

\thepage
\end{document}

enter image description here

The expandable version:

\documentclass{report}
\newcommand{\thesissize}{SHORT}
\begin{document}

\newcommand\tmp{SHORT}
\setcounter{page}{\ifx\tmp\thesissize3\else2\fi}

\thepage

\renewcommand\tmp{NOT SHORT}
\setcounter{page}{\ifx\tmp\thesissize3\else2\fi}

\thepage
\end{document}

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.