为什么使用 tocloft 的 \setlength 会导致 TOC 中的奇怪对齐?

为什么使用 tocloft 的 \setlength 会导致 TOC 中的奇怪对齐?

我遇到的问题是如何对齐目录中的数字和文本。我搜索后发现一个解决方案是使用包tocloft,如TOC 文本 - 数字对齐。然后我尝试将它应用到我的文档中,不幸的是我没有得到预期的结果。

(输出)

如您所见,当我使用时,部分和小节完全关闭:\setlength{\cftsecnumwidth}{4em}

最小示例

\documentclass[10pt,letterpaper]{article}

\usepackage[left=1in,right=1in,top=1in,bottom=1in]{geometry} 
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{ntheorem}
\usepackage{polynomial}
\usepackage{layouts}
\usepackage{enumerate}

\usepackage[version=0.96]{pgf}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes,automata,backgrounds,petri,positioning}
\usetikzlibrary{decorations.pathmorphing}
\usetikzlibrary{decorations.shapes}
\usetikzlibrary{decorations.text}
\usetikzlibrary{decorations.fractals}
\usetikzlibrary{decorations.footprints}
\usetikzlibrary{shadows}

\setcounter{tocdepth}{3}
\setcounter{secnumdepth}{3}
\usepackage[bookmarksopen,bookmarksdepth=3]{hyperref}
\usepackage{titlesec}
\usepackage{xcolor}

\definecolor{dark-red}{rgb}{0.4,0.15,0.15}
\definecolor{dark-blue}{rgb}{0.15,0.15,0.4}
\definecolor{medium-blue}{rgb}{0,0,0.5}
\hypersetup{
    colorlinks, linkcolor={medium-blue},
    citecolor={dark-blue}, urlcolor={medium-blue}
}

\usepackage{tocloft}


\title{\textbf{Solution for Chapter 1}}

\begin{document}
\setlength{\cftsecnumwidth}{4em}

\tableofcontents 
\setlength{\parindent}{0pt}
\setlength{\parskip}{1ex}
\maketitle

    \phantomsection
    \section*{1.1}
    \addcontentsline{toc}{section}{\numberline{1.1} The following are the state diagrams for two DFAs, $M_1$, and $M_2$. Answer the following question 
    about each of these machines.}

    The following are the state diagrams for two DFAs, $M_1$, and $M_2$. Answer the following question 
    about each of these machines.\\

    \begin{tikzpicture}[shorten >=1pt, node distance=2cm,auto,on grid,initial text=, every state/.style={minimum size=3mm,draw=blue!50,very thick,fill=blue!20}]
        \begin{scope}
            \node[state,initial]    (q_1)                           {$q_1$}; 
            \node[state,accepting]  (q_2)   [right=of q_1]          {$q_2$}; 
            \node[state]            (q_3)   [below right=of q_1]    {$q_3$}; 

            \path[->]
            (q_1) edge  [bend left]     node {a}        (q_2)
            (q_1) edge  [loop above]    node {b}        (q_1)

            (q_2) edge  [bend left]     node {a,b}      (q_3)

            (q_3) edge  [bend left]     node {a}        (q_2)
            (q_3) edge  [bend left]     node {b}        (q_1);  
            \node [below=1cm, align=flush center,text width=8cm] at (q_3)
            {
                $M_1$
            };
        \end{scope}

        \begin{scope}[xshift=8cm]
            \node[state,initial,accepting]  (q_1)                   {$q_1$}; 
            \node[state]             (q_2)  [right=of q_1]          {$q_2$}; 
            \node[state]             (q_3)  [below=of q_1]          {$q_3$}; 
            \node[state,,accepting]  (q_4)  [below=of q_2]          {$q_4$};

            \path[->]
            (q_1) edge  [bend left]     node {a}        (q_2)
            (q_1) edge  [loop above]    node {b}        (q_1)

            (q_2) edge  [bend left]     node {a}        (q_3)
            (q_2) edge  [bend left]     node {b}        (q_4)

            (q_3) edge  [bend left]     node {b}        (q_1)
            (q_3) edge  [bend left]     node {a}        (q_2)

            (q_4) edge  [loop below]    node {b}        (q_4)
            (q_4) edge  [bend left]     node {a}        (q_3)           
            ;   
        \node [below=1cm, align=flush center,text width=8cm] at (q_3)
            {
                $M_2$
            };
        \end{scope}
    \end{tikzpicture}

    \textbf{Solution}\\

    \phantomsection 
    \subsection*{a.}
    \addcontentsline{toc}{subsection}{a. What is the start state?}
    What is the start state?\\
        $M_1$ start states: $q_1$\\
        $M_2$ start states: $q_1$\\

    \phantomsection         
    \subsection*{b.}
    \addcontentsline{toc}{subsection}{b. What is the set of accept states?}
    What is the set of accept states?\\
        $M_1$ accepting states: $q_2$\\
        $M_2$ accepting states: $q_1, q_4$\\

    \phantomsection     
    \subsection*{c.}
    \addcontentsline{toc}{subsection}{c. What sequence of states does the machine go through on input $aabc$}
    What sequence of states does the machine go through on input $aabc$?\\
        $M_1$: $q_1 \rightarrow q_2 \rightarrow q_3 \rightarrow q_1 \rightarrow q_1$\\
        $M_2$: $q_1 \rightarrow q_1 \rightarrow q_1 \rightarrow q_2 \rightarrow q_4$\\

    \phantomsection 
    \subsection*{d.}
    \addcontentsline{toc}{subsection}{d. Does the machine accept the string $aabb$?}
    Does the machine accept the string $aabb$?\\
        $M_1$: No because the last state $q_1$ is not an accepting state.\\
        $M_2$: Yes because the last state $q_4$ is an accepting state.\\

    \phantomsection 
    \subsection*{e.}
    \addcontentsline{toc}{subsection}{e. Does the machine accept the string $\varepsilon$?}
    Does the machine accept the string $\varepsilon$?\\
    No, both of them do not accept string $\varepsilon$.

\end{document}

有什么想法吗?也欢迎提供相关问题的链接。提前致谢。

答案1

在您的最小示例中,有几件事需要更改。

  • 您需要确保各个分段的长度相同,但事实并非如此。如果没有手动更改,则默认值在表 1 中给出tocloft包裹 文档

ToC 条目的默认长度

\setlength{\cftsecnumwidth}{4em}% Set numwidth of section
\setlength{\cftsubsecnumwidth}{\cftsecnumwidth}% Make subsection numwidth the same as section
\setlength{\cftsubsecindent}{\cftsecindent}% Make subsection indent the same as section
  • 与在目录中使用章节标题类似\numberline{...},您需要使用\numberline{}(留空)作为小节标题。
\addcontentsline{toc}{subsection}{\numberline{}a. What is the start state?}%
...
\addcontentsline{toc}{subsection}{\numberline{}b. What is the set of accept states?}
...
\addcontentsline{toc}{subsection}{\numberline{}c. What sequence of states does the machine go through on input $aabc$}
...
\addcontentsline{toc}{subsection}{\numberline{}d. Does the machine accept the string $aabb$?}
...
\addcontentsline{toc}{subsection}{\numberline{}e. Does the machine accept the string $\varepsilon$?}
  • 此后的所有内容都\numberline{...}将被视为分段符的标题,即使您添加了空格“ ”。
\addcontentsline{toc}{section}{\numberline{1.1}The following are the state diagrams for two DFAs, $M_1$, and $M_2$. Answer the following question about each of these machines.}

改变 ToC 外观

相关内容