4

I'm working on a macro that when given Hello will make a chapter named hello, and number it like this: 1 - Hello, with the 1 - inside the margins. I also want it to automatically label the chapter by lower-casing the argument and remove all the spaces, creating a \label{chap:hello}.

So far, I've figured out that to remove the "Chapter" declaration, I must use \chapter*{}, and I have to append other commands to make sure the chapter is properly numbered and displayed in the TOC.

What I have now is:

\renewcommand{\chapter}[1]
{%
\chapter*{#1}
\addcontentsline{toc}{chapter}{#1}
\stepcounter{chapter}
}

In a MWE (Minimum working example):

\documentclass{report}
\title{An Example} \author{Nobody}
\renewcommand{\chapter}[1]{%
\chapter*{#1}
\addcontentsline{toc}{chapter}{#1}
\stepcounter{chapter}
}
\begin{document}
\maketitle
\tableofcontents
\chapter{Hello}
\end{document}

(The title is needed for some formatting issues.)

When I test this though, I get the error:

line 10: TeX capacity exceeded, sorry [input stack size=5000] \tableofcontents

I can't seem to find an answer to this anywhere:

It seems to be a very non descriptive error. Help?

One request: If you give me a macro that does the entire job, please explain how it works. (With documentation if it isn't too much trouble)

  • You can't redefine \chapter in terms of \chapter. Why not simply say \setcounter{secnumdepth}{-1}? – egreg Dec 7 '13 at 15:08
3

You have a little hidden infinite loop. You are redefinying \chapter, which calls \chapter*. These are variants of the same command. Hence TeX builds a huge stack, ending with an overfull message.

  • Perfect, using `\newcommand{\chapterx}[1]{} worked great! – NictraSavios Dec 7 '13 at 15:14
1

You can't redefine \chapter in terms of itself. Use the internal version \@schapter which is called when \chapter* is issued. We have to take into account that \tableofcontents calls \chapter*, so an indirect call has to be made.

\documentclass{report}
\title{An Example} \author{Nobody}

\makeatletter
\renewcommand{\chapter}{\@ifstar{\@schapter}{\new@chapter}}
\newcommand{\new@chapter}[1]{%
  \@schapter{#1}
  \addcontentsline{toc}{chapter}{#1}
  \stepcounter{chapter}
}
\makeatother

\begin{document}
\maketitle
\tableofcontents
\chapter{Hello}
\end{document}
  • Your MWE produces an error: Something's wrong--perhaps a missing \item. – NictraSavios Dec 7 '13 at 15:56
  • @NictraSavios Yes; now it's fixed. – egreg Dec 7 '13 at 16:03

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.