14

Can someone tell me why

\let\stdchapter\chapter
\renewcommand{\chapter}[1]{\stdchapter{#1}}

isn't safe to do? Nevermind why I'm asking, I have workarounds so this is more about LaTeX not behaving the way I thought it would. The \let command should backup the original \chapter definition as \stdchapter, and redefining \chapter to expand to \stdchapter should make it do exactly what it did in the first place. However, LaTeX fails to process my document if I make this change (error message: "LaTeX Error: Something's wrong--perhaps a missing \item.").

UPDATE: Here's minimal code needed to reproduce the problem:

\documentclass[a4paper]{report}
\usepackage[american]{babel}
\let\stdchapter\chapter
\renewcommand{\chapter}[1]{\stdchapter{#1}}
\begin{document}
\tableofcontents
\end{document}

2 Answers 2

14

you have to catch the star version of \chapter

\documentclass[openany]{report}

\makeatletter
\let\stdchapter\chapter
\renewcommand*\chapter{%
  \@ifstar{\starchapter}{\@dblarg\nostarchapter}}
\newcommand*\starchapter[1]{\stdchapter*{#1}}
\def\nostarchapter[#1]#2{\stdchapter[{#1}]{#2}}
\makeatother

\begin{document}
\tableofcontents
\chapter{foo}
\chapter*{baz}
\end{document}
1
  • 2
    Let's note that this is a nice demonstration of the usage of \@dblarg !
    – yo'
    Jun 20, 2012 at 12:37
12

It's difficult to say right away what's the source of the problem. Your redefinition should work reasonably, albeit not being what I'd recommend.

The \chapter macro usually has no argument. It appears to have an optional and a mandatory argument, but it really hasn't and this should be taken into consideration when doing redefinitions.

For example, the definition of \chapter in the book class is

\newcommand\chapter{\if@openright\cleardoublepage\else\clearpage\fi
                    \thispagestyle{plain}%
                    \global\@topnum\z@
                    \@afterindentfalse
                    \secdef\@chapter\@schapter}

which shows that \chapter doesn't look for arguments.

I can understand that your example is just an example, but in that way you lose all facilities provided by \chapter (*-form and optional argument).

Why you get that error surely depends on how you're actually redefining \chapter.

Let's see why you get the error in your example. The macro \tableofcontents issues \chapter*{\contentsname} and here's where your redefinition fails: the argument to \chapter is *.

The argument to a command is either the text contained in the braced group that follows the command or, if an open brace doesn't immediately follow, the first token (different from a space).

5
  • It could be that some other package is also modifying \chapter, and that's why it fails. Thanks for answering. Jun 20, 2012 at 6:58
  • 1
    @HaakonRiiser Would you try and present a minimal working example (MWE) that illustrates your problem? Welcome to TeX.SX!
    – egreg
    Jun 20, 2012 at 7:54
  • fwiw, the code as presented works for me; class book, definition after \begin{document} (and of course before the first \chapter command) Jun 20, 2012 at 8:25
  • Thank you for explaining how the starred version causes this problem, and to Herbert for providing a full example! I'm not actually going to use this is my thesis as it was cleaner to just make a new chapter macro with a different name, but it bugged me that I didn't understand why the redefinition didn't work. :) Jun 20, 2012 at 10:59
  • 1
    @HaakonRiiser It's generally unwise to redefine a macro that could be used in internals changing also its syntax: if it has no arguments, then its redefinition shouldn't have them either.
    – egreg
    Jun 20, 2012 at 11:02

You must log in to answer this question.

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