在自定义目录中写入条目

在自定义目录中写入条目

我正在使用 LaTeX 编写自己的课程,但在创建目录时遇到了困难。我完全不懂 LaTeX,但出于设计要求,我决定使用自定义课程。

以下是该课程的相关部分:

\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

如您所见,我正在定义自己的chapter环境。有一个章节计数器(实际上是两个,我敢打赌可能会更好,但这不是这里的问题),我将章节标题添加到 ToC 文件中。我还定义了tableofcontents只需写入标题(要格式化)并调用@starttoc。到目前为止一切顺利。这是主文件:

\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}

布局很简单。我只输入三个空白章节,标题分别为Chapter 1Chapter 2Chapter 3,所有章节的描述都相同。

一切似乎都正常,我得到了以下输出:

在此处输入图片描述

根据我的解释,每行都有一个编号,后面是章节名称(包括编号),然后是页码。因此,忽略所有空格,您将获得:

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

格式很奇怪,我还没搞清楚空格从何而来,尤其是第一个换行符。\newline在第三个参数的末尾添加一个\addcontentsline会产生完全相同的结果,尽管我原本以为它会换行。如下所示:

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

我明白这\addcontentsline为我添加了内容,我在其他地方读到的内容\cftaddtitleline应该很方便。我\addcontentsline用以下内容替换了:

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

我收到以下错误:

! 未定义的控制序列。\chapter ...r }#1 \newline }\par \cftaddtitleline {toc}{chapter}{Chapter #1}{} l.43 ...r{Chapter 1}{本章说明} 错误消息顶行末尾的控制序列从未被 \def'ed。如果您拼错了它(例如,\hobx'), typeI' 和正确的拼写(例如,`I\hbox')。否则继续,我会忘记未定义的内容。

! 未定义的控制序列。\chapter ...r }#1 \newline }\par \cftaddtitleline {toc}{chapter}{Chapter #1}{} l.44 ...r{Chapter 2}{Description for this chapter} 错误消息顶行末尾的控制序列从未被 \def'ed。如果您拼错了它(例如,\hobx'), typeI' 和正确的拼写(例如,`I\hbox')。否则继续,我会忘记未定义的内容。

! 未定义的控制序列。\chapter ...r }#1 \newline }\par \cftaddtitleline {toc}{chapter}{Chapter #1}{} l.45 ...r{Chapter 3}{本章说明} 错误消息顶行末尾的控制序列从未被 \def'ed。如果您拼错了它(例如,\hobx'), typeI' 和正确的拼写(例如,`I\hbox')。否则继续,我会忘记未定义的内容。

我想知道的是如何使用上述命令或其他命令正确输出和格式化目录。

这是完整的 .cls 文件:

% ----------------------------------------------------------------------------------------
% 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

答案1

成功了!我用没有注释的内容替换了注释的内容:

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


    \addtocontents{toc}
    {
        \unexpanded{\unexpanded{\twodigits}}{\theChapterCounter}
        #1
        \newline
    }

相关内容