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?

Dov
  • 1,481
  • 2
    You need \addcontentsline{toc}{...}. For details about how to use it look, for example, at this post. –  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 –  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 –  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. –  Jul 12 '14 at 16:22
  • The newfloat package http://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
  • @JohnKormylo: If the entries are floats this is correct, but I doubt that Appendices and chapter-like Labs shouldm't float at all? –  Jul 13 '14 at 10:15
  • @Christian Hupfer - Writing entries to lof and lot is easy. Creating a Table of Labs from scratch is hard. You don't need to use floats or captions, you just need the table. – John Kormylo Jul 13 '14 at 17:45
  • @JohnKormylo: I know, I have used \addcontentsline etc. very often ;-) –  Jul 13 '14 at 19:16

1 Answers1

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}
John Kormylo
  • 79,712
  • 3
  • 50
  • 120
  • 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? –  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
  • I forgot that, but when I run it the second time, it crashes with: ! Argument of \contentsline has an extra }. \par l.3 \contentsline {chapter}{\numberline {2}Variables and Animation}{5} – Dov Jul 15 '14 at 00:57
  • What I would like is to make each Chapter start on a new page. The font they use is the right size.

    \Chapter{Title}

    should be:

    Chapter 1: Title

    and similarly:

    \lab{Sew Together Random Body Parts}

    Lab 1: Sew Together Random Body Parts

    in the same font as the chapter.

    Sorry, I do not know how to format comments!

    – Dov Jul 15 '14 at 00:59
  • I replaced the MWE (which had gotten out of sync) with a few mods. Your MWE, and the book.cls code, starts a chapter on a new ODD page, so I set up the lab the same way. It is easy enough to remove, if that is what you want. – John Kormylo Jul 15 '14 at 04:00
  • Ok, feeling stupid, but what's an MWE? – Dov Jul 15 '14 at 13:22
  • Minimum Working Example - Questions are expected to be accompanied by enough code to be copied and run as is. It should show the problem and only the problem. – John Kormylo Jul 15 '14 at 19:11
  • @John, thanks this is amazing. I just wish I could follow it. There is so much complexity here, I will have to pick apart all those arcane macros. – Dov Jul 18 '14 at 16:41
  • Heck, I don't understand them all either. I just modified it a little at a time to get it to work. – John Kormylo Jul 18 '14 at 17:27