2

I'm trying to alter the memoir \chapter command by redefining e.g. \afterchaptertitle. I'd like starting a new chapter to change the value of a Latex variable, so that the following chapter definition can depend on this variable. Hopefully a MWE clarifies this better:

\documentclass{memoir}

\newif\ifchanged
\changedfalse

\renewcommand{\afterchaptertitle}{
% The next line initially should print "false", from then on should print "true"
\small At this point, changed is \ifchanged true\else false\fi 
% Set "changed" to true, so future calls will print "true"
\changedtrue
\\ % (looks weird without a newline)
}

\begin{document}

\chapter{Test} % Prints "At this point, changed is false" as expected

Text for the first chapter. 

\chapter{Next} % Prints "At this point, changed is false" again, not as expected

Text for the second chapter

\end{document}

I had hoped that the first chapter heading would be followed by "At this point, changed is false" and the second by "At this point, changed is true". Instead, both display as "false".

I have a feeling that this has something to do with robustness / missing \protects but that's beyond my Latex understanding...

  • 1
    Welcome to TeX.SE. Thanks for this MWE! You need \global\changedtrue, since \afterchaptertitle is apparently called in a group, so the change of \ifchanged from false to true is forgotten outside of this group – user31729 Aug 23 '17 at 20:03
1

The 'culprit' is the \@makechapterhead command that is called in \@chapter, which is basically the background of the \chapter command.

The memoir version of \@makechapterhead is

\def\@makechapterhead#1{%
  \chapterheadstart%  \vspace*{50\p@}%
  {%\parindent \z@ \raggedright \normalfont
   \parskip \z@
   \parindent \z@ \memRTLraggedright \normalfont
   \ifm@m@And
     \printchaptername \chapternamenum \printchapternum
     \afterchapternum % \par\nobreak \vskip 20\p@
   \else
     \printchapternonum
   \fi
   \interlinepenalty\@M
   \printchaptertitle{#1} % \Huge \bfseries #1
   \afterchaptertitle % \par\nobreak \vskip 40\p@
  }}

where the explicit group starts aafter \vspace*{50\p@}% and ends after \afterchaptertitle, so any change in the state of \ifchanged 'variable' will be local unless prepended with \global.

I've done a small version that shows the same issue, with book class where \foo has the meaning of \chapter and \foostuff is \@makechapterhead.

\documentclass{book}


\newif\ifchanged
\changedfalse


\newcommand{\afterchaptertitle}{%
Chapter \small At this point, changed is \ifchanged true\else false\fi 
\global\changedtrue%
}

\def\foostuff{%
  {%
    \afterchaptertitle%
  }%
}

\def\foo#1{%
\foostuff%

#1%
}

\begin{document}
\foo{First chapter}

\foo{Second chapter}
\end{document}

enter image description here

  • Thank you! I would never have worked that out. As a side note, is there a resource that you'd recommend for someone who's getting there with user-space Latex and is starting to want to learn more about internals like these? – CharlieB Aug 23 '17 at 20:37
  • @CharlieB: The internals of LaTeX are TeX, the TeXBook (unfortunately not free for download) or TeX by Topic (see www.ctan.org) and of course the LaTeX source files. In the very end it is a pretty amount of experience – user31729 Aug 23 '17 at 20:42
  • I've just had a look at Tex by Topic and it seems exactly what I was looking for. Thanks again! – CharlieB Aug 23 '17 at 21:28

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.