Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

The problem I'm having is to align the number and text in the TOC. I searched and found one solution is to use package tocloft, like proposed in TOC Text - numbers alignment. Then I tried to apply it into my document, unfortunately I didn't get the expected result.

(the output)

As you can see, the section and subsection are completely off when I use: \setlength{\cftsecnumwidth}{4em}

Minimal Example

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

Any idea? A link to a related question is also welcome. Many thanks in advance.

share|improve this question

1 Answer 1

up vote 5 down vote accepted

There are a couple of things you need to change in your minimal example.

  • You need to make sure that the lengths for respective sectional breaks are the same, which they are not. The defaults, if not manually changed, are given in Table 1 of the tocloft package documentation:

Default lengths for ToC entries

\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
  • Similar to using \numberline{...} for your section title addition to the ToC, you need to use \numberline{} (leaving it empty) for the subsection titles.
\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$?}
  • Everything after \numberline{...} is considered the title of the sectional break, even if you add a space ' '.
\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.}

Altered ToC look

share|improve this answer
    
I fixed up the weird code formatting (not your fault). See Why code snippet after listing not work again. (Nice answer, BTW.) –  Alan Munn Aug 2 '11 at 22:19
    
@Werner: Many thanks for a detailed answer ;) –  Chan Aug 2 '11 at 22:28
    
@Alan: Thanks... I didn't know what caused that. –  Werner Aug 2 '11 at 22:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.