4

I can't understand why the following doesn't work:

\documentclass{beamer}
\newif\iftemp \tempfalse

\iftemp
    \csname if@chapter@pp\endcsname 
        \addcontentsline{toc}{part}{Appendices} 
    \fi
\fi

\begin{document}
\begin{frame}{Test}
\end{frame}
\end{document}

while the following works:

\documentclass{beamer}
\usepackage{ifthen}
\def\temptemp{temp}

\ifthenelse{\equal{\temptemp}{wrong}}{
    \csname if@chapter@pp\endcsname 
        \addcontentsline{toc}{part}{Appendices} 
    \fi
}{}

\begin{document}
\begin{frame}{Test}
\end{frame}
\end{document}

Seems like \iftemp ... \fi does NOT ignore it's content even when \iftemp is false.

For a separate reason, I want to use \iftemp instead of \ifthenelse{...}. Any hotfix here?

1
  • 1
    To keep \if(true|false) ... \fi paired, try the trick \expandafter\ifx\csname if@chapter@pp\expandafter\endcsname\csname iftrue\endcsname from Heiko Oberdiek's previous answer Dec 6, 2021 at 10:14

2 Answers 2

4

The conditional \if@chapter@pp needs to be defined for the code to do something sensible. Based on the name, I guess you're loading appendix.

\documentclass{beamer}
\usepackage{appendix}

\newif\iftemp
%\tempfalse % a new conditional always starts false

\expandafter\iftemp
    \csname if@chapter@pp\endcsname
        \addcontentsline{toc}{part}{Appendices}
    \fi
\fi

\begin{document}

\begin{frame}{Test}
\end{frame}

\end{document}

What is the problem? When \iftemp returns false, the “true branch” is skipped, but explicit conditionals are taken into account. But \csname if...\endcsname is skipped over as no expansion is performed, so it it doesn't count as an explicit conditional.

With \expandafter, the token \if@chapter@pp is formed before \iftemp is examined.

More simply, use \makeatletter and \makeatother:

\makeatletter
\iftemp
    \if@chapter@pp
        \addcontentsline{toc}{part}{Appendices}
    \fi
\fi
\makeatother
1
  • Thanks! That part is in the .sty file, so I just change it to \if@chapter@pp. Now it works perfect! (Instead of loading the appendix package, I just define \newif\if@chapter@pp \@chapter@pptrue)
    – user182849
    Dec 6, 2021 at 15:32
4

In the first conditional

\iftemp
  \csname if@chapter@pp\endcsname 
    \addcontentsline{toc}{part}{Appendices} 
  \fi
\fi

\iftemp finds the first \fi since there is no other explicit \if<...>. And since \tempfalse, there is an extra (unpaired) \fi, hence the error

! Extra `\fi`.

In the second conditional

\ifthenelse{\equal{\temptemp}{wrong}}{%
  \csname if@chapter@pp\endcsname 
      \addcontentsline{toc}{part}{Appendices} 
  \fi
}{}

you should understand that \ifthenelse is defined to only take a single argument - the condition. After evaluating the condition, it either executes \@firstoftwo or \@secondoftwo. Under \ifthenelse there is no closing \fi required.

1
  • Thanks! Is there any way I can use \if ... \fi instead?
    – user182849
    Dec 6, 2021 at 5:43

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.