1

I have a number of different kinds of entities in a document using the book documentclass. There are Chapters, Labs, Appendices, etc.

Just taking labs as an example, I created a newcommand that defines labs and autonumbers them. I would like them to be in the table of contents at the same level as a Chapter. Right now, as a quick hack, I make labs chapters, which makes them show up, but they display a chapter number and a lab number.

\newcounter{lab}
\newcommand{\lab}[1]{\stepcounter{lab} \chapter{Lab \arabic{lab}: #1}}

How do I specify what entities go into a table of contents?

In addition, if I were to create my own independent equivalent of a chapter, In this other question: Custom \chapter definition

In one of the answers, I saw reference to the definition of the chapter macro, and I am not seeing how chapter sets the font and text size? Here is the code:

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

The upshot is, how can I create new entities to put into a table of contents, and how do those entities define their look?

  • 2
    You need \addcontentsline{toc}{...}. For details about how to use it look, for example, at this post. – Andrew Jul 12 '14 at 13:31
  • 1
    It is the command \@schapter, which itself calls @makeschapterhead, see about lines 400 etc. in the current book.cls file – user31729 Jul 12 '14 at 14:15
  • 2
    Regarding the entries to the TOC itself: I rather suggest a own list of labs etc. instead of putting them into the TOC – user31729 Jul 12 '14 at 14:16
  • Perhaps you may think rather of something like a \newtheorem from amsthm package or the very sophisticated tcolorbox which allows for nice markup/layout and adds list of ... entries too. – user31729 Jul 12 '14 at 16:22
  • The newfloat package texdoc.net/pkg/newfloat allows one to create a new list of floats, similar to lof and lot. One can write to this table without actually creating a caption. – John Kormylo Jul 12 '14 at 20:19
1

\addtocontents write a record to the file toc which is read by \tableofcontents the next time latex is run. Examine the .toc file to see results. \string\contentsline for example writes "\contentsline" but does not expand it. The {chapter} entry has something to do with distinguishing chapters from sections, etc. The rest is the same as with chapters except I use \thelab instead of \thechapter.

\documentclass{book}

\makeatletter
\newcounter{lab}% also defines \thelab

% roughly equivalent to \chapter
\newcommand{\lab}[1]{% #1 is lab heading
  \refstepcounter{lab}%
  \if@openright\cleardoublepage\else\clearpage\fi%
  \addtocontents{toc}{\string\contentsline {chapter}%
    {\hbox to .65in{Lab}\protect\numberline{\thelab}#1}{\thepage}}%
  \global\@topnum\z@% page number to bottom?
  \noindent%
  \@lab{#1}% fixed first character separation
}

% roughly equivalent to \@chapter
\def\@lab#1{% #1 is lab heading
  \if@twocolumn%
    \@topnewpage[{\huge\bfseries Lab \thelab: #1\par}]%
  \else{\huge\bfseries Lab \thelab: #1\par}%
  \@afterheading% no idea
  \fi}

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

% copied from book.cls and modified
\def\@chapter[#1]#2{%
  \ifnum \c@secnumdepth >\m@ne
    \if@mainmatter
      \refstepcounter{chapter}%
      \typeout{\@chapapp\space\thechapter.}%
      \addtocontents{toc}{\string\contentsline {chapter}%
        {\hbox to .65in{Chapter}\protect\numberline{\thechapter}#1}{\thepage}}%
    \else
      \addtocontents{toc}{\string\contentsline {chapter}%
        {\hbox to .65in{Chapter}\protect\numberline{\thechapter}#1}{\thepage}}%
    \fi
  \else
    \addtocontents{toc}{\string\contentsline {chapter}%
      {\hbox to .65in{Chapter}\protect\numberline{\thechapter}#1}{\thepage}}%
  \fi
  \chaptermark{#1}%
  \addtocontents{lof}{\protect\addvspace{10\p@}}%
  \addtocontents{lot}{\protect\addvspace{10\p@}}%
  \if@twocolumn
    \@topnewpage[\@makechapterhead{#2}]%
  \else
    \@makechapterhead{#2}%
    \@afterheading
  \fi}
\makeatother

\begin{document}
\tableofcontents
\chapter{In the beginning}

This space intentionally left blank.

\lab{Eat the apple.}

This space intentionally left blank.
\end{document}
  • This is what I was looking for. I'm sorry, it's horribly unreadable (not your fault, I had no idea how much of a hack LaTeX is). I just read about makeatletter, but don't understand the code at all. Can you explain how this works? I am getting the first letter of the title in huge font, for example, instead of the whole title, and the text continues on the same line, and I can't see where that is happening. Also, I am not seeing this text come up in the table of contents even though it looks like I should. So this doesn't seem to be quite working in my document. I will try standalone. – Dov Jul 14 '14 at 11:45
  • Ok, I just tested your example as you posted it. It does not quite appear to work (don't understand why). The big thing is, the table of contents does not appear. The little thing is that only the first letter of the lab title is huge, and while I would love to learn how to do that for the text starting the chapter itself, I want the entire title to be capitalized. Can you clarify? – Dov Jul 14 '14 at 11:53
  • Truth is, I copied most of it from book.cls and only through out the parts that were chapter specific. – John Kormylo Jul 14 '14 at 16:09
  • @Dov: I did not test this code by John, but could you please be a bit more specific in what your lab (environment/section heading?????) actually is supposed to be and perhaps provide a sketch of it, so that other users can do a fine tuning? – user31729 Jul 14 '14 at 17:48
  • BTW, did you remember to run it twice? The toc never appears until the second run. – John Kormylo Jul 14 '14 at 22:06

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.