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?
- 66,645
3 Answers
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.
- 757,742
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}
-
Ehm, the last solution is quite wrong. Try nesting it in a conditional where the
\ifinappendixpart is skipped by the outer conditional and you'll be surprised. In theetoolboxsolution it's easier to say\appto\appendix{\booltrue{inappendix}}instead of going the\oldappendixroute. – 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\fiworks, it is because TeX inserts a “frozen\relax” not to leave the conditional unfinished. Even simply\fiwould 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
-
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!
addcontentslineto the definition ofappendix, but in general the@chapappapproach is better. – Johannes_B Mar 15 '15 at 16:47