8

I was using titletoc until I realized that it has some incompatibilities with hyperref. The thing is, that titletoc can do the same thing in a easier way, less one.

\titlecontents*{subsection}[5.4em]
{\small}
{\thecontentslabel~ }
{}
{}
[.~\textbullet\ ]
[.]

this part does the subsection as a paragraph (with section number and without page number). I have realized that the problem is the pseudo-code used by tocloft for the entries in the TOC

{\cftXfont {\cftXpresnum SNUM\cftXaftersnum\hfil} \cftXaftersnumb TITLE}%
{\cftXleader}{\cftXpagefont PAGE}\cftXafterpnum\par

I mean, the last command \par make impossible to leave two subsection entries in the same textline. Any idea? Another interesting solution is with etoolbox, some examples with that package are awesome, but it's not on my hands.

This is a MWE:

\documentclass{book}

\usepackage{titletoc}
\titlecontents*{subsection}[5.4em]{\small}
{\thecontentslabel~ }{}{}[.~\textbullet\ ][.]

\begin{document}

\tableofcontents

\chapter{Test Chapter 1}
\section{Test Section 1.1}
\subsection{First Test Subsection}
\subsection{Second Test Subsection}
\subsection{Third Test Subsection}
\subsection{Fourth Test Subsection}
\section{Test Section 1.2}
\subsection{First Test Subsection}
\subsection{Second Test Subsection}

\chapter{Test Chapter 2}
\section{Test Section 2.1}
\subsection{First Test Subsection}
\subsection{Second Test Subsection}
\subsection{Third Test Subsection}
\subsection{Fourth Test Subsection}
\section{Test Section 2.2}
\subsection{First Test Subsection}
\subsection{Second Test Subsection}

\end{document}
lockstep
  • 250,273
Gitano
  • 1,389

1 Answers1

9

One possible solution; \l@subsection is redefined to suppress the page number and then, the etoolbox package is used to patch \section and \subsection: each subsection (except the first one) adds \unskip.~\textbullet\ before the corresponding entry in the ToC, and every \section adds the final dot (\unskip.) to the last subsection of every section in the ToC:

\documentclass{article}          
\usepackage{etoolbox}

\newlength\rightmargintoc
\setlength\rightmargintoc{\linewidth}
\addtolength\rightmargintoc{-3em}

\makeatletter
\renewcommand*\l@subsection[2]{%
  \parshape 2  0em \rightmargintoc \parindent \rightmargintoc%
  \leavevmode#1}
\makeatother

\pretocmd{\subsection}{%
  \ifnum\value{subsection}=0\relax
    \else\protect\addcontentsline{toc}{subsection}{\unskip.~\textbullet\ }
  \fi}{}{}{}

\pretocmd{\section}{%
  \ifnum\value{subsection}>0\relax\addcontentsline{toc}{subsection}{\unskip.}
    \else\fi%
  \addtocontents{toc}{\par}}{}{}

\AtEndDocument{\ifnum\value{subsection}>0\relax\addcontentsline{toc}{subsection}{\unskip.}\else\fi}

\begin{document}

\tableofcontents
\section{Test Section One}
\subsection{First Test Subsection}
\subsection{Second Test Subsection}
\subsection{Third Test Subsection}
\subsection{Fourth Test Subsection}

\section{Test Section Two}

\section{Test Section Three}
\subsection{First Test Subsection}
\subsection{Second Test Subsection}

\end{document}

enter image description here

A modification for the book document class was requested in the comments:

\documentclass{book}
\usepackage{etoolbox}

\newlength\rightmargintoc
\setlength\rightmargintoc{\linewidth}
\addtolength\rightmargintoc{-5em}

\makeatletter
\renewcommand*\l@subsection[2]{%
  \setlength\@tempdima{3em}%
\parshape 2  2.5em \rightmargintoc \dimexpr\parindent+2.5em\relax \rightmargintoc%
  \leavevmode#1}
\makeatother

\pretocmd{\subsection}{%
  \ifnum\value{subsection}=0\relax
  \else\protect\addcontentsline{toc}{subsection}{\unskip.~\textbullet\ }
  \fi}{}{}{}

\pretocmd{\chapter}{\addtocontents{toc}{\par}}{}{}

\AtEndDocument{%
\ifnum\value{subsection}>0\relax
  \addcontentsline{toc}{subsection}{\unskip.}\addtocontents{toc}{\par}
\else\fi}

\begin{document}
\tableofcontents

\chapter{Test Chapter One}
\section{Test Section One One}
\subsection{First Test Subsection}
\subsection{Second Test Subsection}
\subsection{Third Test Subsection}
\subsection{Fourth Test Subsection}
\section{Test Section One Two}
\subsection{First Test Subsection}
\subsection{Second Test Subsection}

\chapter{Test Chapter Two}
\section{Test Section Two One}
\subsection{First Test Subsection}
\subsection{Second Test Subsection}

\chapter{Test Chapter Three}
\section{Test Section Three One}
\section{Test Section Three Two}
\subsection{First Test Subsection}
\subsection{Second Test Subsection}

\end{document}

An image of the resulting ToC:

enter image description here

If hyperref is loaded, some additional precaution must be taken:

\documentclass{book}
\usepackage{etoolbox}
\usepackage{hyperref}

\newlength\rightmargintoc
\setlength\rightmargintoc{\linewidth}
\addtolength\rightmargintoc{-5em}

\makeatletter
\renewcommand*\l@subsection[2]{%
  \setlength\@tempdima{3em}%
\parshape 2  2.5em \rightmargintoc \dimexpr\parindent+2.5em\relax \rightmargintoc%
  \leavevmode#1}
\makeatother

\pretocmd{\subsection}{%
  \ifnum\value{subsection}=0\relax
  \else\protect\addcontentsline{toc}{subsection}{\texorpdfstring{\unskip.~\textbullet\ }{}}
  \fi}{}{}{}

\pretocmd{\chapter}{\addtocontents{toc}{\par}}{}{}

\AtEndDocument{%
\ifnum\value{subsection}>0\relax
  \addcontentsline{toc}{subsection}{\texorpdfstring{\unskip.}{}}\addtocontents{toc}{\par}
\else\fi}

\begin{document}
\tableofcontents

\chapter{Test Chapter One}
\section{Test Section One One}
\subsection{First Test Subsection}
\subsection{Second Test Subsection}
\subsection{Third Test Subsection}
\subsection{Fourth Test Subsection}
\section{Test Section One Two}
\subsection{First Test Subsection}
\subsection{Second Test Subsection}

\chapter{Test Chapter Two}
\section{Test Section Two One}
\subsection{First Test Subsection}
\subsection{Second Test Subsection}

\chapter{Test Chapter Three}
\section{Test Section Three One}
\section{Test Section Three Two}
\subsection{First Test Subsection}
\subsection{Second Test Subsection}

\end{document}

Here's now an improved version:

\documentclass{book}
\usepackage{etoolbox}
\usepackage[colorlinks=true,linkcolor=cyan]{hyperref}

\newlength\rightmargintoc
\setlength\rightmargintoc{\linewidth}
\addtolength\rightmargintoc{-7em}

\makeatletter
\def\subsectocline#1#2#3#4#5{%
\parshape 2  2.5em \rightmargintoc \dimexpr\parindent+2.5em\relax \rightmargintoc
\@tempdima#3
\ifdim\lastskip=1sp\relax\ \textbullet~\else\fi{\small#4.}\hskip1sp%
}
\renewcommand*\l@subsection{\subsectocline{1}{0em}{3em}}
\makeatother

\pretocmd{\chapter}{\addtocontents{toc}{\par}}{}{}
\pretocmd{\section}{\addtocontents{toc}{\par}}{}{}

\AtEndDocument{%
\ifnum\value{subsection}>0\relax
  \addtocontents{toc}{\par}
\fi}

\begin{document}
\tableofcontents

\chapter{Test Chapter One}
\section{Test Section One One}
\subsection{First Test Subsection}
\subsection{Second Test Subsection}
\subsection{Third Test Subsection}
\subsection{Fourth Test Subsection}
\section{Test Section One Two}
\subsection{First Test Subsection}
\subsection{Second Test Subsection}

\chapter{Test Chapter Two}
\section{Test Section Two One}
\subsection{First Test Subsection}
\subsection{Second Test Subsection}

\chapter{Test Chapter Three}
\section{Test Section Three One}
\section{Test Section Three Two}
\subsection{First Test Subsection}
\subsection{Second Test Subsection}

\end{document}

The resulting ToC:

enter image description here

Gonzalo Medina
  • 505,128
  • It works perfect! Thanks Gonzalo Medina, but I need it with the book class. I tried to modify the \chapter command with \pretocmd, similar as you did with \section and \subsection, but my experience is not enough. I've already given a MWE, as @Werner suggests. – Gitano Oct 26 '12 at 19:32
  • @Gitano I've updated my answer with the modifications requested. – Gonzalo Medina Oct 26 '12 at 21:10
  • @Gitano I updated my answer with a solution with hyperref. Please let me know of this solves the problem. – Gonzalo Medina Oct 27 '12 at 20:54
  • Thanks Gonzalo Medina. I just have some questions. 1) Why is the subsection number placed in a box? Can I get it as: 1.1.1(@tempdima{3em})This is the first Subsection.? Where @tempdima{3em} is the length used between the box and the title. I ask that because this spacing is not the same when there are 2 digits on the subsection number. 2) The command \addtocontents{toc}{\protect\contentsline{chapter}{\small APPENDICES}{\protect\relax}{}} now is causing problems. 3) Some warnings are shown with the hyperref package. I've edited the original the MWE in order to get a tip or suggest of you. – Gitano Oct 27 '12 at 21:16
  • Thanks, I have already checked! the hyperref warning have disappeared. – Gitano Oct 27 '12 at 21:18
  • @Gitano 1) requires more work to be done; I'll have to think about it. 2) I'll do some tests and let you know the result. In any case, I'll let you know what have I found. – Gonzalo Medina Oct 27 '12 at 21:27
  • Thanks Gonzalo Medina! Just one small detail, the last solution is for the article class, how can I modify for the book class? – Gitano Oct 27 '12 at 21:45
  • @Gitano Oh, sorry for that. Answer updated. – Gonzalo Medina Oct 27 '12 at 21:52
  • @Gitano I didn't experience any problems using \addtocontents{toc}{\protect\contentsline{chapter}{\small APPENDICES}{\protect\relax}{}} in my last code. What kind of problems do you get with that line? – Gonzalo Medina Oct 28 '12 at 20:59
  • When I add \addtocontents{toc}{\protect\contentsline{chapter}{\small APPENDICES}{}} to your last code, it show me the next error: Runaway argument? ! Paragraph ended before \contentsline was complete. <to be read again> \par l.23 \par

    )

    – Gitano Oct 29 '12 at 19:09
  • In addition, if I use \usepackage[colorlinks=true]{hyperref}, there is a large spacing between the subsection name and the period. Do you now why? Furthermore, how can I change the font size? I would like to use \small only in the subsection part. Thanks Gonzalo Medina. – Gitano Oct 29 '12 at 19:14
  • @Gitano the issue with \addtocontents can be solved by using \addtocontents{toc}{\par\protect\contentsline{chapter}{\small APPENDICES}{\protect\relax}{}}. Regarding the other issues, I'll take a look and let you know. – Gonzalo Medina Oct 29 '12 at 19:33
  • @Gitano I updated my answer; I hope this time we're getting closer to the final solution ;-) – Gonzalo Medina Oct 29 '12 at 21:02
  • Thanks Gonzalo Medina, everything is perfect. I just updated the MWE in the original questions in order to show you the differences between the spacing of the subsecnumbers and the titles when the chapter number increases. Do you think it is hard to fix that spacing? If the answer is yes, so I will modify the parameter {2.3em}, and it's enough. Thank you very much. – Gitano Oct 29 '12 at 22:59
  • @Gitano I'll think about the spacing and let you know. Just a little remark: you should edit your question and recover the original MWE; if you want to, you can add the new MWE but preserving the older one; otherwise, the question looses meaning since, as it is now, it gives the impression that the new MWE was the starting point. – Gonzalo Medina Oct 29 '12 at 23:04
  • You are right! Here it is Gonzalo Medina! – Gitano Oct 30 '12 at 15:13