1

This is a follow-on to the question: Adding different kinds of entities to table of contents

As I build a huge document, things don't work as in the MWE from the previous problem. I stripped out the entire document and have just the commands and a couple of sample pages to demonstrate what I want, and what the problems are.

  1. The table of contents crashes. The error message states there is an extra close brace } but I don't see it.

  2. The labs are supposed to be separate, standalone units like appendices. But every page starts with a header "Chapter 1. Title ... " because the book style thinks that everything must be inside chapters. How do I suppress that header?


\documentclass[12pt]{book}              % Book class in 12 points
\parindent0pt  \parskip10pt             % make block paragraphs
\raggedright                            % do not right justify
\usepackage{mathptmx}  % times roman, including math (where possible)
\usepackage{mathpazo}  % palatino, including math (where possible)
\usepackage{helvet}    % helvetica
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage[usenames,dvipsnames]{color}
\usepackage{amsthm}
\usepackage{graphicx}
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\usepackage{comment}
\usepackage{listings}
\usepackage{grffile}
\usepackage{listing}
\usepackage{changepage}
\usepackage{multirow}
\usepackage{outlines}
\usepackage{enumitem}
\usepackage{fancyhdr}
\usepackage{subfigure}
\usepackage{hyperref}
\usepackage{textcomp}
\usepackage[top=1in, bottom=1.5in, left=1in, right=1in]{geometry}
%usepackage{fullpage} would fix the margins problem
\cfoot{\sffamily\textcopyright 2005-2014 copyright notice }
\rfoot{\thepage}
\graphicspath{ {C:/LaTeX/img/} }
\pagestyle{fancy}
\title{\bf Grandiose Title Here \\ %\includegraphics[scale=.5]{ Logo.jpg}\\
Organization Name Here}    % Supply information
\author{Primary Author}            %   for the title page.
\date{\today}                           %   Use current date. 
\definecolor{lgray}{rgb}{0.9,0.9,0.9}
%\definecolor{graycolor}{rgb}{0.9,0.9,0.9}
\definecolor{white}{rgb}{1,1,1}
\definecolor{dkgreen}{rgb}{0,.5,0.1}
\definecolor{wrongcolor}{rgb}{.9,.5,.4}
\definecolor{purple}{rgb}{0.6,0,0.6}
%\input state.tex


\newcommand{\underscore}{ %UNDERLINE COMMAND
  \underline{ \hspace{3 cm}}
}

\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


% Note that book class by default is formatted to be printed back-to-back.
\begin{document}                        % End of preamble, start of text.
\setenumerate[1]{label=\arabic*.}
\setenumerate[2]{label=\Alph*.}
\setenumerate[3]{label=\roman*.}
\setenumerate[4]{label=\alph*.}
\frontmatter                            % only in book class (roman page #s)
\maketitle                                  % Print title page.
\pagebreak
{\Large Acknowledgements }

Acknowledgements ... Thanks Stack Overflow experts!

{\Large Authors}

Main author \\
author2 \\
author3

{\Large Editors} \\
editor-in-chief

{\Large Layout and LaTeX Programming} \\
interns who laid out the document

{\Large Contributors} \\
contributor 1 \\
contributor 2


{\Large Example Programs in Java} \\

Code contributors

{\Large Example Programs in C++}

{\Large Online Quiz Development and Testing}

{\Large Lab Photos and  Testing}

\pagebreak
\tableofcontents                        % Print table of contents
\mainmatter                                 % only in book class (arabic page #s)

\chapter{How to Use this Document}                % Print a "chapter" heading
This is where we describe how it all works

Now, here come the individual short self-paced lessons,
called labs for now for want of a better term.

\lab{ Let There be Light}

To embed a live link ...
%\url{http://www.AdAstraEducation.org}

\lab{C++ Code and BadCode Styles}

Write some code, yadayada

\end{document}                          
5
  • A tip: If you want to markup code and have a nearby enumeration list, it is good idea to use a horizontal line inbetween, otherwise markup does not work
    – user31729
    Jul 20, 2014 at 16:04
  • 1
    The problem comes from a conflict between hyperref and the re-deifinition of what's in the .toc file: if uou don't load hyperref, it compiles fine.
    – Bernard
    Jul 20, 2014 at 16:47
  • @Dov: Is it really necessary to redefine the \chapter command as well?
    – user31729
    Jul 20, 2014 at 17:47
  • @Christian I'm not claiming it is. I am a rank amateur at this stuff. I should probably have started with a question like "Here's my format, please tell me all the bad stuff I'm doing so I can start with best practices."
    – Dov
    Jul 20, 2014 at 20:43
  • @christian, thanks for editing the question. I could not for the life of me understand why it wasn't coming out formatted right!
    – Dov
    Jul 20, 2014 at 20:45

1 Answer 1

2

The \addtocontents command is the source of the error, in my point of view, so I changed it to \addcontentsline version. However, the lab command does not seem to be well-defined in this case.

Additionally, for use with hyperref, there should be a \phantomsection command before \addcontentsline.

\documentclass[12pt]{book}              % Book class in 12 points
\parindent0pt  \parskip10pt             % make block paragraphs
\raggedright                            % do not right justify
\usepackage{mathptmx}  % times roman, including math (where possible)
\usepackage{mathpazo}  % palatino, including math (where possible)
\usepackage{helvet}    % helvetica
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage[usenames,dvipsnames]{color}
\usepackage{amsthm}
\usepackage{graphicx}
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\usepackage{comment}
\usepackage{listings}
\usepackage{grffile}
\usepackage{listing}
\usepackage{changepage}
\usepackage{multirow}
\usepackage{outlines}
\usepackage{enumitem}
\usepackage{fancyhdr}
\usepackage{subfigure}
\usepackage[bookmarksnumbered=true]{hyperref}
\usepackage{textcomp}
\usepackage[top=1in, bottom=1.5in, left=1in, right=1in]{geometry}
%usepackage{fullpage} would fix the margins problem
\cfoot{\sffamily\textcopyright 2005-2014 copyright notice }
\rfoot{\thepage}
\graphicspath{ {C:/LaTeX/img/} }
\pagestyle{fancy}
\title{\bf Grandiose Title Here \\ %\includegraphics[scale=.5]{ Logo.jpg}\\
Organization Name Here}    % Supply information
\author{Primary Author}            %   for the title page.
\date{\today}                           %   Use current date. 
\definecolor{lgray}{rgb}{0.9,0.9,0.9}
%\definecolor{graycolor}{rgb}{0.9,0.9,0.9}
\definecolor{white}{rgb}{1,1,1}
\definecolor{dkgreen}{rgb}{0,.5,0.1}
\definecolor{wrongcolor}{rgb}{.9,.5,.4}
\definecolor{purple}{rgb}{0.6,0,0.6}
%\input state.tex


\newcommand{\underscore}{ %UNDERLINE COMMAND
  \underline{ \hspace{3 cm}}
}

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

\newcommand{\labname}{Lab}

% roughly equivalent to \chapter
\newcommand{\lab}[1]{% #1 is lab heading
  \refstepcounter{lab}%
  \if@openright\cleardoublepage\else\clearpage\fi%
  \phantomsection%%%%
  \addcontentsline{toc}{chapter}{\labname~\protect\numberline{\thelab}#1}%
%
%  \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.}%
      \addcontentsline{toc}{chapter}{Chapter~\protect\numberline{\thechapter}#1}%
%      \addtocontents{toc}{\string\contentsline {chapter}%
%        {\hbox to .65in{Chapter}\protect\numberline{\thechapter}#1}{\thepage}}%
    \else
      \addcontentsline{toc}{chapter}{Chapter~\protect\numberline{\thechapter}#1}%
%      \addtocontents{toc}{\string\contentsline {chapter}%
%        {\hbox to .65in{Chapter}\protect\numberline{\thechapter}#1}{\thepage}}%
    \fi
  \else
      \addcontentsline{toc}{chapter}{Chapter~\protect\numberline{\thechapter}#1}%
  %  \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


% Note that book class by default is formatted to be printed back-to-back.
\begin{document}                        % End of preamble, start of text.
\setenumerate[1]{label=\arabic*.}
\setenumerate[2]{label=\Alph*.}
\setenumerate[3]{label=\roman*.}
\setenumerate[4]{label=\alph*.}
\frontmatter                            % only in book class (roman page #s)
\maketitle                                  % Print title page.
\pagebreak
{\Large Acknowledgements }

Acknowledgements ... Thanks Stack Overflow experts!

{\Large Authors}

Main author \\
author2 \\
author3

{\Large Editors} \\
editor-in-chief

{\Large Layout and LaTeX Programming} \\
interns who laid out the document

{\Large Contributors} \\
contributor 1 \\
contributor 2


{\Large Example Programs in Java} \\

Code contributors

{\Large Example Programs in C++}

{\Large Online Quiz Development and Testing}

{\Large Lab Photos and  Testing}

\pagebreak
\tableofcontents                        % Print table of contents
\mainmatter                                 % only in book class (arabic page #s)

\chapter{How to Use this Document}                % Print a "chapter" heading
This is where we describe how it all works

Now, here come the individual short self-paced lessons,
called labs for now for want of a better term.

\lab{Let There be Light}

To embed a live link ...
%\url{http://www.AdAstraEducation.org}

\lab{C++ Code and BadCode Styles}

Write some code, yadayada

\end{document}   

**The OP requested empty page headers for the laboratory sections, so use a different pagestyle called lab for those 'sections'.

\documentclass[12pt]{book}              % Book class in 12 points
\parindent0pt  \parskip10pt             % make block paragraphs
\raggedright                            % do not right justify
\usepackage{mathptmx}  % times roman, including math (where possible)
\usepackage{mathpazo}  % palatino, including math (where possible)
\usepackage{helvet}    % helvetica
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage[usenames,dvipsnames]{color}
\usepackage{amsthm}
\usepackage{graphicx}
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\usepackage{comment}
\usepackage{listings}
\usepackage{grffile}
\usepackage{listing}
\usepackage{changepage}
\usepackage{multirow}
\usepackage{outlines}
\usepackage{enumitem}
\usepackage{fancyhdr}
\usepackage{subfigure}
\usepackage[bookmarksnumbered=true]{hyperref}
\usepackage{textcomp}
\usepackage[top=1in, bottom=1.5in, left=1in, right=1in]{geometry}
%usepackage{fullpage} would fix the margins problem
\cfoot{\sffamily\textcopyright 2005-2014 copyright notice }
\rfoot{\thepage}
\graphicspath{ {C:/LaTeX/img/} }
\pagestyle{fancy}
\title{\bf Grandiose Title Here \\ %\includegraphics[scale=.5]{ Logo.jpg}\\
Organization Name Here}    % Supply information
\author{Primary Author}            %   for the title page.
\date{\today}                           %   Use current date. 
\definecolor{lgray}{rgb}{0.9,0.9,0.9}
%\definecolor{graycolor}{rgb}{0.9,0.9,0.9}
\definecolor{white}{rgb}{1,1,1}
\definecolor{dkgreen}{rgb}{0,.5,0.1}
\definecolor{wrongcolor}{rgb}{.9,.5,.4}
\definecolor{purple}{rgb}{0.6,0,0.6}
%\input state.tex


\newcommand{\underscore}{ %UNDERLINE COMMAND
  \underline{ \hspace{3 cm}}
}

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

\newcommand{\labname}{Lab}

% roughly equivalent to \chapter
\newcommand{\lab}[1]{% #1 is lab heading
  \refstepcounter{lab}%
  \if@openright\cleardoublepage\else\clearpage\fi%
  \phantomsection%%%%
  \addcontentsline{toc}{chapter}{\labname~\protect\numberline{\thelab}#1}%
%
%  \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
  \cleardoublepage
}

% roughly equivalent to \@chapter


\fancypagestyle{lab}{%
%\fancyhf{}%
\renewcommand{\headrulewidth}{0pt}%
\renewcommand{\footrulewidth}{0pt}%
  \lfoot{}%
  \lhead{}%
  \rhead{}%
  \rfoot{}%
}%



\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.}%
      \addcontentsline{toc}{chapter}{Chapter~\protect\numberline{\thechapter}#1}%
%      \addtocontents{toc}{\string\contentsline {chapter}%
%        {\hbox to .65in{Chapter}\protect\numberline{\thechapter}#1}{\thepage}}%
    \else
      \addcontentsline{toc}{chapter}{Chapter~\protect\numberline{\thechapter}#1}%
%      \addtocontents{toc}{\string\contentsline {chapter}%
%        {\hbox to .65in{Chapter}\protect\numberline{\thechapter}#1}{\thepage}}%
    \fi
  \else
      \addcontentsline{toc}{chapter}{Chapter~\protect\numberline{\thechapter}#1}%
  %  \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


% Note that book class by default is formatted to be printed back-to-back.
\begin{document}                        % End of preamble, start of text.
\setenumerate[1]{label=\arabic*.}
\setenumerate[2]{label=\Alph*.}
\setenumerate[3]{label=\roman*.}
\setenumerate[4]{label=\alph*.}
\frontmatter                            % only in book class (roman page #s)
\maketitle                                  % Print title page.
\pagebreak
{\Large Acknowledgements }

Acknowledgements ... Thanks Stack Overflow experts!

{\Large Authors}

Main author \\
author2 \\
author3

{\Large Editors} \\
editor-in-chief

{\Large Layout and LaTeX Programming} \\
interns who laid out the document

{\Large Contributors} \\
contributor 1 \\
contributor 2


{\Large Example Programs in Java} \\

Code contributors

{\Large Example Programs in C++}

{\Large Online Quiz Development and Testing}

{\Large Lab Photos and  Testing}

\pagebreak
\tableofcontents                        % Print table of contents
\mainmatter                                 % only in book class (arabic page #s)

\chapter{How to Use this Document}                % Print a "chapter" heading
This is where we describe how it all works

Now, here come the individual short self-paced lessons,
called labs for now for want of a better term.

\pagestyle{lab}

\lab{Let There be Light}

To embed a live link ...
%\url{http://www.AdAstraEducation.org}

\lab{C++ Code and BadCode Styles}

Write some code, yadayada

\end{document}

Note Most of the strange code is piled up by using code from another question not answered by me. It would be easier to use a newtheorem or a tcolorbox environment with a clean interface instead of abusing some kind of the \chapter command.

5
  • As long as you are giving all this feedback, can you tell me whether graphicspath can be made relative so it's portable on all platforms? Right now it's hardcoded to a specific place in the tree and very windows specific, the question is if it were relative, it would be relative to WHERE?
    – Dov
    Jul 20, 2014 at 20:47
  • @Dov: If your *.tex file is stored in C:/LaTeX, I would say \graphicspath{img}, but I can't test it, since I do work with Windows
    – user31729
    Jul 20, 2014 at 20:52
  • My intern swore relative paths did not work, but I'm not 100% sure he got it right. I will test it. I hate embedding a hardcoded absolute path that is windows-specific.
    – Dov
    Jul 21, 2014 at 1:14
  • @ChristianHupfer Can you please have a look at this meta.tex.stackexchange.com/questions/4551/…?
    – percusse
    Jul 30, 2014 at 20:57
  • @percusse: Thanks for notifying me. I just tested my code, it runs without compilation errors. If the OP is unsatisfied with the answer or it does not provide what he requested, he should give me a notice what fails in his point of view or even unaccept the answer. If there are compilation errors, I would like to know which errors occur
    – user31729
    Jul 30, 2014 at 21:35

You must log in to answer this question.

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