0

I'm writing my own class using LaTeX, and just hit a bump with creating a ToC. I'm not fluent at all in LaTeX, but decided to go with a custom class due to design requirements.

Here's the relevant part of the class:

\RequirePackage{contour}
\RequirePackage{etoolbox}
\RequirePackage{tocloft}

\newcounter{ChapterCounter}
\newcounter{CurrentChapNum}
\setcounter{CurrentChapNum}{0}

\newenvironment{chapter}[2]
{   
    \thispagestyle{empty}

    \refstepcounter{ChapterCounter}

    \ifnum\value{ChapterCounter}=\value{CurrentChapNum}
        % nop
    \else
        \setcounter{CurrentChapNum}{\value{ChapterCounter}}
    \fi

    % Write to ToC
    \addcontentsline{toc}{chapter}%
    {\protect\numberline{\theChapterCounter}#1}%

%   \cftaddtitleline{toc}{section}{Topic 1}{}
}
{
    % Irrelevant details omitted here for simplicity
}

\makeatletter
\newcommand{\tableofcontents}{
    Table of Contents

    \@starttoc{toc}%
}
\makeatother

As you can see, I'm defining my own chapter environment. There's a counter for chapters (actually two, I bet that could probably be better, but it's not the issue here) and I add the chapter title to the ToC file. I also define tableofcontents that simply writes the title (to be formatted) and invokes @starttoc. So far so good. Here's the main file:

\documentclass{whitepaper}

\begin{document}

\tableofcontents

\chapter{Chapter 1}{Description for this chapter}
\chapter{Chapter 2}{Description for this chapter}
\chapter{Chapter 3}{Description for this chapter}

\end{document}

The layout is pretty simple. I just enter three empty chapters, titled Chapter 1, Chapter 2, and Chapter 3, all with the same description.

Everything seems to work, and I get the following output:

enter image description here

To my interpretation, there's a numbering for each line, following by the chapter name including a number, and then the page. So, ignoring all whitespace you get:

1 Chapter 1 .......... 1
2 Chapter 2 .......... 1
3 Chapter 3 .......... 1

The format is pretty odd, and I haven't managed to figure out where the white space comes from, especially the first newline. Adding a \newline at the end of third argument of \addcontentsline yields the exact same result, although I was expecting it to break lines. Like so:

\addcontentsline{toc}{chapter}%
{
    \protect\numberline{\theChapterCounter}#1
    \newline
}%

I get it that \addcontentsline is adding content for me, and I read elsewhere that \cftaddtitleline should be handy for that. I replaced \addcontentsline with the following:

\cftaddtitleline{toc}{chapter}{Chapter #1}{}

And I get the following error:

! Undefined control sequence. \chapter ...r }#1 \newline }\par \cftaddtitleline {toc}{chapter}{Chapter #1}{} l.43 ...r{Chapter 1}{Description for this chapter} The control sequence at the end of the top line of your error message was never \def'ed. If you have misspelled it (e.g., \hobx'), typeI' and the correct spelling (e.g., `I\hbox'). Otherwise just continue, and I'll forget about whatever was undefined.

! Undefined control sequence. \chapter ...r }#1 \newline }\par \cftaddtitleline {toc}{chapter}{Chapter #1}{} l.44 ...r{Chapter 2}{Description for this chapter} The control sequence at the end of the top line of your error message was never \def'ed. If you have misspelled it (e.g., \hobx'), typeI' and the correct spelling (e.g., `I\hbox'). Otherwise just continue, and I'll forget about whatever was undefined.

! Undefined control sequence. \chapter ...r }#1 \newline }\par \cftaddtitleline {toc}{chapter}{Chapter #1}{} l.45 ...r{Chapter 3}{Description for this chapter} The control sequence at the end of the top line of your error message was never \def'ed. If you have misspelled it (e.g., \hobx'), typeI' and the correct spelling (e.g., `I\hbox'). Otherwise just continue, and I'll forget about whatever was undefined.

What I want to know is how to properly output and format the ToC, using either of the commands mentioned or anything else.

Here's the full .cls file:

% ----------------------------------------------------------------------------------------
% Identification
% ----------------------------------------------------------------------------------------
\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\ProvidesClass{whitepaper}[2018/01/01 LaTeX class]

% ----------------------------------------------------------------------------------------
% Defaults
% ----------------------------------------------------------------------------------------
\RequirePackage{ifthen}

\newboolean{doublepage}
\setboolean{doublepage}{false}

% ----------------------------------------------------------------------------------------
% Options
% ----------------------------------------------------------------------------------------
\DeclareOption{doublepage}{
    \setboolean{doublepage}{true}
}
\ProcessOptions\relax

% ----------------------------------------------------------------------------------------
% Geometry
% ----------------------------------------------------------------------------------------
\RequirePackage[a4paper]{geometry}

\geometry{
    a4paper,
    textheight=650pt,
    headsep=50pt,
    voffset=0pt,
    top=95pt,
    left=61mm,
    right=39mm,
}

\savegeometry{defaultgeometry}

% ----------------------------------------------------------------------------------------
% Multilingual support
% ----------------------------------------------------------------------------------------
\RequirePackage[english]{babel}
\RequirePackage{csquotes}

% ----------------------------------------------------------------------------------------
% Driver-independent color extensions
% ----------------------------------------------------------------------------------------
\RequirePackage{xcolor}
\RequirePackage{fmtcount}

\definecolor{primarycolor}{RGB}{47,53,61}
\definecolor{secondarycolor}{RGB}{152,160,171}
\definecolor{covercolor}{RGB}{46,53,61}

% ----------------------------------------------------------------------------------------
% SI units, used for money representations as well
% ----------------------------------------------------------------------------------------
\usepackage{siunitx}

\sisetup{
    group-four-digits = true,
    group-separator = {,}
}

% ----------------------------------------------------------------------------------------
% Font
% ----------------------------------------------------------------------------------------
\RequirePackage[sfdefault]{roboto}

\renewcommand{\normalsize}{\fontsize{10pt}{14pt}\selectfont}

\makeatletter
\AtBeginDocument{
    \color{primarycolor}\global\let\default@color\current@color
}
\makeatother

% ----------------------------------------------------------------------------------------
% Paragraphs
% ----------------------------------------------------------------------------------------
\setlength{\parindent}{0pt}
\setlength{\parskip}{15pt}

% ----------------------------------------------------------------------------------------
% Chapters
% ----------------------------------------------------------------------------------------
\RequirePackage{contour}
\RequirePackage{etoolbox}
%\RequirePackage{tocloft}

\newcounter{ChapterCounter}
\newcounter{CurrentChapNum}
\setcounter{CurrentChapNum}{0}

\newenvironment{chapter}[2]
{   
    %\thispagestyle{chapterstyle}
    \thispagestyle{empty}

    \refstepcounter{ChapterCounter}
%   \vspace*{75pt}

    \ifnum\value{ChapterCounter}=\value{CurrentChapNum}
        % nop
    \else
        \setcounter{CurrentChapNum}{\value{ChapterCounter}}
    \fi

%   { % Numbering
%       \fontsize{24pt}{29pt}\fontseries{b}\selectfont
%       \setlength{\parskip}{0pt}
%       \color{secondarycolor}
%       \hspace*{-70pt}
%       \textbf{\two@digits{\theChapterCounter}}
%   }

%   \vspace*{15pt}

%   { % Title
%       \fontsize{44pt}{39pt}\fontseries{k}\selectfont
%       \setlength{\parskip}{0pt}
%       \color{primarycolor}
%       \hspace*{-75pt}
%       \textbf{#1}
%   }

%   \vspace*{20pt}

    % Write to ToC
    \addcontentsline{toc}{chapter}%
    {
        \theChapterCounter #1
        \newline
    }%
}
{
%   \ifthenelse{\boolean{doublepage}}
%   {
%       \ifodd\therealpage
%           \newpage
%           \thispagestyle{empty}%
%           \null
%           \newpage
%       \else
%           % nop
%       \fi
%   }
%   {}
}

% ----------------------------------------------------------------------------------------
% Bibliography
% ----------------------------------------------------------------------------------------
\RequirePackage[backend=bibtex,sorting=none,autocite=superscript]{biblatex}

\DeclareCiteCommand{\supercite}[\mkbibsuperscript]
{\iffieldundef{prenote}
    {}
    {\BibliographyWarning{Ignoring prenote argument}}%
    \iffieldundef{postnote}
    {}
    {\BibliographyWarning{Ignoring postnote argument}}%
    \bibopenbracket}%
{\usebibmacro{citeindex}%
    \usebibmacro{cite}}
{\supercitedelim}
{\bibclosebracket}

% ----------------------------------------------------------------------------------------
% Graphics
% ----------------------------------------------------------------------------------------
\RequirePackage{graphicx}

% ----------------------------------------------------------------------------------------
% Page settings
% ----------------------------------------------------------------------------------------
\RequirePackage{fancyhdr}
\RequirePackage{xassoccnt}
\RequirePackage{scrextend}
\RequirePackage{everypage}

\newcounter{realpage}

\DeclareAssociatedCounters{page}{realpage}
\AtBeginDocument{%
    \stepcounter{realpage}
}

\newcommand{\thepage}{
    \therealpage
}

\ifthenelse{\boolean{doublepage}}
{
    \fancyheadoffset[leh,roh]{70pt}
    \fancyheadoffset[loh,reh]{65pt}
}
{
    \fancyheadoffset[leh,roh]{135pt}
}

\fancypagestyle{evenpage}{

    \fancyhf{}

    \lhead{}
    \rhead{
        \textcolor{secondarycolor}{
            \textbf{\therealpage}
        }
    }

    \ifthenelse{\boolean{doublepage}}
    {
        \newgeometry{
            textheight=650pt,
            headsep=50pt,
            voffset=0pt,
            top=95pt,
            left=61mm,
            right=39mm,
        }
    }
    {} % defaultgeometry
}

\fancypagestyle{oddpage}{

    \fancyhf{}

    \lhead{
        \textcolor{secondarycolor}{
            \textbf{\therealpage}
        }
    }
    \rhead{}

    \ifthenelse{\boolean{doublepage}}
    {
        \newgeometry{
            textheight=650pt,
            headsep=50pt,
            voffset=0pt,
            top=95pt,
            left=39mm,
            right=61mm,
        }
    }
    {} % defaultgeometry
}

\AddEverypageHook{
    \ifnum\value{ChapterCounter}>0
        \ifthenelse{\boolean{doublepage}}
        {
            \ifodd\therealpage
            \thispagestyle{oddpage}
            \else
            \thispagestyle{evenpage}
            \fi
        }
        { \thispagestyle{oddpage} }
    \fi
}

\renewcommand{\headrulewidth}{0pt}

% ----------------------------------------------------------------------------------------
% Conditional content
%
% \contentlevel impacts the output of \cont by filtering content that is of a level greater
% than the given threshold. Such content does not appear on the output, while everything
% else does. The level also applies to citations. Content levels are defined as follows:
%
%   0       Minimal         Doesn't print any conditional content
%   1       Basic           Prints basic content
%   2       Overview        Prints enough content for a general overview
%   3       Technical       Prints technical content
%   4       Enterprise      Prints all content except private
%   5       Private         Prints all content
% ----------------------------------------------------------------------------------------
\def\contentlevel#1{
    \def\contentlevel_{#1}
}

\def\contentif#1{
    \expandafter\contentiff#1\relax
}

\def\contentiff#1,#2\relax{
    \ifnum\contentlevel_ < 1
        % nop
    \else
        \ifnum\numexpr#1-1 < \contentlevel_\relax#2\fi
    \fi
}

\def\citeif#1{
    \expandafter\citeiff#1\relax
}

\def\citeiff#1,#2\relax{
    \ifnum\numexpr#1-1 < \contentlevel_\relax\supercite{#2}\fi
}

% ----------------------------------------------------------------------------------------
% Blank page
% ----------------------------------------------------------------------------------------
\newcommand\blankpage{%
    \thispagestyle{empty}%
    \null
    \thispagestyle{empty}%
    \newpage
}

% ----------------------------------------------------------------------------------------
% Cover page
% ----------------------------------------------------------------------------------------
\RequirePackage{authoraftertitle}
\RequirePackage{pagecolor}
\RequirePackage{afterpage}
\RequirePackage[export]{adjustbox}

\newcommand{\maketitle}{

    \thispagestyle{empty}
    \newpagecolor{covercolor}
    \newgeometry{
        textheight=750pt,
        headsep=50pt,
        voffset=0pt,
        top=30pt,
        left=20mm,
        right=20mm,
    }
    \includegraphics[width=200pt,right]{logo-h-white}
    {
        \color{white}
        \fontseries{bx}
        \fontsize{45pt}{60pt}
        \selectfont
        {
            \vspace*{25pt}
            \newline
            \MyTitle
            \par
        }
    }
    \afterpage{\restorepagecolor}

    % Blank page
    \ifthenelse{\boolean{doublepage}}
    { \blankpage\blankpage }
    {}

    % Copyright
    \vspace*{\fill}
    \centerline{Copyright {\copyright} 2018 André.}
    \centerline{All rights reserved.}

    % Blank page
    \ifthenelse{\boolean{doublepage}}
    { \blankpage\blankpage }
    {}

    \restoregeometry
}

% ----------------------------------------------------------------------------------------
% Table of Contents
% ----------------------------------------------------------------------------------------
\newcommand{\printchapterlist}[1]{%
    \textbf{#1}     
}%

\makeatletter
\newcommand{\tableofcontents}{
    %\include{\hocfilename}

    Table of Contents

    \@starttoc{toc}%
}
\makeatother
  • We need more information. How about posting the entire .cls? – Werner Jan 16 '18 at 16:13
  • Please see the update unswer! I can't think of anything that is related with this issue to give you pointers, but I guess I just don't know enough :) – André Fratelli Jan 16 '18 at 16:22
1

Managed to make it work! I replaced what is commented by what is not:

    % Write to ToC
%   \addcontentsline{toc}{chapter}%
%   {
%       \theChapterCounter #1
%       \newline
%   }%


    \addtocontents{toc}
    {
        \unexpanded{\unexpanded{\twodigits}}{\theChapterCounter}
        #1
        \newline
    }
| improve this answer | |

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.