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?

cgnieder
  • 66,645

3 Answers3

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.

David Carlisle
  • 757,742
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}
egreg
  • 1,121,712
Werner
  • 603,163
  • 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 Mar 15 '15 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 Mar 15 '15 at 17:39
  • @egreg: Yes. The use of an \if... macro rather than an \if... conditional is not appropriate. I'll update. – Werner Mar 15 '15 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 Mar 15 '15 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. – Clifford Weil Mar 16 '15 at 16:01
  • @CliffordWeil: Can you provide a sample document that I can copy-and-paste-and-compile? – Werner Mar 16 '15 at 18:01
  • Werner: Could my problem be the manner in which I'm trying to redefine @chapter? – Clifford Weil Mar 18 '15 at 01:20
  • Werner: Could my problem be the manner in which I'm trying to redefine @chapter? How does one redefine a command that's defined by \def rather than \newcommand? I can send a sample document as requested, but not enough characters are allowed. – Clifford Weil Mar 18 '15 at 01:32
  • @CliffordWeil: Use PasteBin and provide a link to the code. – Werner Mar 18 '15 at 02:05
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!