6

I'm trying to redefine the \appendix command so that my appendix headings go to the top of the pages. To do this, I tried the following code, but I get the error ! Illegal parameter number in definition of \appendix. Why am I getting this error and how to get rid of it?

\documentclass{book}
\makeatletter
\renewcommand\appendix{\par
\def\@makechapterhead#1{%
  \vspace*{0\p@}%
  {\parindent \z@ \raggedright \normalfont
    \ifnum \c@secnumdepth >\m@ne
      \if@mainmatter
        \huge\bfseries \@chapapp\space \thechapter
        \par\nobreak
        \vskip 20\p@
      \fi
    \fi
    \interlinepenalty\@M
    \Huge \bfseries #1\par\nobreak
    \vskip 40\p@
  }}
  \setcounter{chapter}{0}%
  \setcounter{section}{0}%
  \gdef\@chapapp{\appendixname}%
  \gdef\thechapter{\@Alph\c@chapter}}
\makeatother
\begin{document}
\appendix
\chapter{Foo}
\end{document}

Please keep in mind that, I know that a solution is to put the redefinition of the \@makechapterhead at the beginning of the \appendix command in the document body, but I want to do the job in the preamble. Also, I'd prefer not to use extra packages to do the job.

1 Answer 1

20

Inside the definition of \appendix, #1 would be the first parameter of \appendix that does not exists, \appendix is defined without parameters. For the parameters of a definition inside a definition you need to double the hash char:

\renewcommand\appendix{\par
  \def\@makechapterhead##1{%
    ... ##1 ...
  }%
  ...
}

If \@makechapterhead contained another definition with a parameter, then #### would be needed for the inner parameter.

1
  • 1
    This really helped me with the todonotes package. If you want \todo{Here is some \lstinline{SomeClass#SomeMethod}}, you have to escape the # or todonotes gets confused e.g. \todo{Here is some \lstinline{SomeClass\#SomeMethod}}. HTH future travelers
    – Hamy
    Aug 26, 2013 at 14:14

You must log in to answer this question.

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