9

I need to redefine the \chapter command to change the \addcontentsline command depending on whether or not the \appendix command has been issued. Is there an "if" command that determines if a command has been issued?

4
  • Welcome to TeX.sx! :)
    – cgnieder
    Commented Mar 15, 2015 at 16:27
  • Something like a status variable?
    – user31729
    Commented Mar 15, 2015 at 16:37
  • Maybe you shoud redefine \appendix first
    – touhami
    Commented Mar 15, 2015 at 16:47
  • You can add a redefinition of addcontentsline to the definition of appendix, but in general the @chapapp approach is better.
    – Johannes_B
    Commented Mar 15, 2015 at 16:47

3 Answers 3

7

In general no, you can't tell \empty from nothing, however in at least report or book you can tell if \appendix has been used by looking at \@chapapp which will have definition \chaptername or \appendixname depending.

7

In general, the only way to do this is to add it yourself. For example, specific to \appendix, you can add a boolean-switch like this:

This is before the appendix.
This is after the appendix.

\documentclass{report}

\newif\ifinappendix% Default is \inappendixfalse
\let\oldappendix\appendix% Store \appendix
\renewcommand{\appendix}{% Update \appendix
  \oldappendix% Default \appendix
  \inappendixtrue% Set switch to true
}

\begin{document}

This is \ifinappendix after \else before \fi the appendix.

\appendix

This is \ifinappendix after \else before \fi the appendix.

\end{document}

etoolbox also provides similar such switches as "boolean flags":

\documentclass{report}

\usepackage{etoolbox}
\newbool{inappendix}% Default is \boolfalse{inappendix}
\appto\appendix{\booltrue{inappendix}}% Add boolean switch to \appendix

\begin{document}

This is \ifbool{inappendix}{after}{before} the appendix.

\appendix

This is \ifbool{inappendix}{after}{before} the appendix.

\end{document}

Extending David's answer, and this again is specific to your reference of \appendix, you can check the value of \@chapapp:

\documentclass{report}

\makeatletter
\newcommand{\inappendix}{TT\fi\expandafter\ifx\@chapapp\appendixname}
\makeatother

\begin{document}

This is \if\inappendix after \else before \fi the appendix.

\appendix

This is \if\inappendix after \else before \fi the appendix.

\end{document}
9
  • Ehm, the last solution is quite wrong. Try nesting it in a conditional where the \ifinappendix part is skipped by the outer conditional and you'll be surprised. In the etoolbox solution it's easier to say \appto\appendix{\booltrue{inappendix}} instead of going the \oldappendix route.
    – egreg
    Commented Mar 15, 2015 at 17:36
  • A fix for the last solution would be \newcommand{\inappendix}{TT\fi\ifx\@chapapp\appendixname} and the call is \if\inappendix<true>\else<false>\fi
    – egreg
    Commented Mar 15, 2015 at 17:39
  • @egreg: Yes. The use of an \if... macro rather than an \if... conditional is not appropriate. I'll update.
    – Werner
    Commented Mar 15, 2015 at 17:39
  • While just T\fi works, it is because TeX inserts a “frozen \relax” not to leave the conditional unfinished. Even simply \fi would work, but it's better to have two tokens for \if.
    – egreg
    Commented Mar 15, 2015 at 18:57
  • I created the new command \inappendix as suggested above. Then in the definition of \@chapter I inserted\if\inappendix \addcontentsline{toc}{section}% {\protect\numberline{\@chapapp\space\thechapter}#1}% \else \addcontentsline{toc}{chapter}% {\protect\numberline{\@chapapp\space\thechapter}#1}% \fi However, the two appendices were entered in to ToC at the chapter level; not at the section level. Commented Mar 16, 2015 at 16:01
0

I was able to use the command \ifx directly, but couldn't use \@chapapp to compare to \chaptername. I defined \newcommand{\chapname}{Chapter} for that purpose. Specifically, I used \ifx\chaptername\chapname, which worked perfectly!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .