# Weird error: "\csname if@chapter@pp\endcsname" inside of \if ~ \fi

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

\documentclass{beamer}
\newif\iftemp \tempfalse

\iftemp
\csname if@chapter@pp\endcsname
\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
\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?

• 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

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
\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
\fi
\fi
\makeatother

• 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) Dec 6, 2021 at 15:32

In the first conditional

\iftemp
\csname if@chapter@pp\endcsname
\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

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.