[Tex/LaTex] Problem‎ with redefining \chapter{…} command‎

chaptersheader-footermacrospage-numbering

I want to change the definition of \chapter[...]{...} ‎to‎ \chapter[...]{...‎}‎‎‎\thispagestyle{empty}‎‎.‎ To do this, I'm using the code ‎below:‎

\documentclass{book}
\let\origchapter\chapter
\renewcommand{\chapter}[2][]{%
\origchapter[#1]{#2}
\thispagestyle{empty}
}
\begin{document}
\tableofcontents
\chapter{Bla Bla}
Some text ...
\newpage
\chapter{Bla Bla}
\end{document}

‎Everything is ok, but when I call ‎\tableofcontents‎ after the ‎‎\begin{‎document}‎‎‎,‎ ‎I'll‎ get the following error:‎

! LaTeX Error: Something's wrong--perhaps a missing \item.‎‎

What‎ am I ‎‎doing wrong‎?‎

Best Answer

Addition to Werner's answer.

Unknown \chapter

The definition of \chapter might not contain \thispagestyle{empty}, because it is hidden in later called macros or this action looks different. Then the patching as in Werner's answer will fail, because {plain} is missing in the definition text of \chapter. Therefore the following example provides a generic solution, it redefines \chapter with support of the star form and the optional argument:

\documentclass{report}

\makeatletter
\newcommand*{\saved@chapter}{}
\let\saved@chapter\chapter
\renewcommand*{\chapter}{%
  \begingroup
  \toks@{\endgroup\saved@chapter}%
  \@ifstar{%
    \toks@\expandafter{\the\toks@*}%
    \final@chapter
  }{\next@chapter}%
}
\newcommand*{\final@chapter}[1]{%
  \the\toks@{#1}%
  \thispagestyle{empty}%
}
\newcommand*{\next@chapter}{%
  \@ifnextchar[{\opt@chapter}{\final@chapter}%
}
\newcommand*{\opt@chapter}{}
\def\opt@chapter[#1]{%
  \toks@\expandafter{\the\toks@[{#1}]}%
  \final@chapter
}
\makeatother

\begin{document}
\tableofcontents
\chapter*{Chapter with star}
\chapter{Chapter without optional argument}
\chapter[Chapter's optional argument]{Chapter with optional argument}
\end{document}

KOMA-Script

The KOMA-Script classes uses the following definition for \chapter:

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

It misses plain, because it is replaced by \chapterpagestyle. However, this macro can be redefined, no need for redefining \chapter:

\renewcommand*{\chapterpagestyle}{empty}

Class memoir

Chapters in memoir support a special page style chapter:

\newcommand\chapter{%
  \ifartopt\par\@nameuse{chapterblock}\else
    \clearforchapter
    \thispagestyle{chapter}
    \global\@topnum\z@
  \fi
  \m@mindentafterchapter
  \@ifstar{\@m@mschapter}{\@m@mchapter}}

The page style can be changed by \aliaspagestyle:

\aliaspagestyle{chapter}{empty}

Full example

The following example catches five cases:

  • \chapter is not available.
  • Class memoir.
  • KOMA-Script classes.
  • Patching via etoolbox's \patchcmd (see Werner's answer).
  • Generic redefinition of \chapter.

The code:

\documentclass{scrreprt}

\makeatletter
\begingroup\expandafter\expandafter\expandafter\endgroup
\expandafter\ifx\csname chapter\endcsname\relax
  \wlog{* No \string\chapter.}%
\else
  \@ifclassloaded{memoir}{%
    % Class memoir
    \aliaspagestyle{chapter}{empty}%
    \wlog{* Chapter page style redefined via \string\aliaspagestyle.}%
  }{%
    \begingroup\expandafter\expandafter\expandafter\endgroup
    \expandafter\ifx\csname chapterpagestyle\endcsname\relax
      % Patching with etoolbox
      \RequirePackage{etoolbox}[2010/09/12]%
      \patchcmd{\chapter}{plain}{empty}{%
        \wlog{* Chapter patched by \string\patchcmd.}%
      }{%
        % Generic case
        \newcommand*{\saved@chapter}{}%
        \let\saved@chapter\chapter
        \renewcommand*{\chapter}{%
          \begingroup
          \toks@{\endgroup\saved@chapter}%
          \@ifstar{%
            \toks@\expandafter{\the\toks@*}%
            \final@chapter
          }{\next@chapter}%
        }
        \newcommand*{\final@chapter}[1]{%
          \the\toks@{#1}%
          \thispagestyle{empty}%
        }
        \newcommand*{\next@chapter}{%
          \@ifnextchar[{\opt@chapter}{\final@chapter}%
        }
        \newcommand*{\opt@chapter}{}%
        \def\opt@chapter[#1]{%
          \toks@\expandafter{\the\toks@[{#1}]}%
          \final@chapter
        }%
        \wlog{* Chapter redefined.}%
      }%
    \else
      % KOMA-Script classes
      \renewcommand*{\chapterpagestyle}{empty}%
      \wlog{* Chapter page style set by redefining \string\chapterpagestyle.}%
    \fi
  }%
\fi
\makeatother

\begin{document}
\tableofcontents
\chapter*{Chapter with star}
\chapter{Chapter without optional argument}
\chapter[Chapter's optional argument]{Chapter with optional argument}
\end{document}