# How to emulate titletoc with tocloft

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


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}

-
Welcome to TeX.sx! A tip: If you indent lines by 4 spaces, they'll be marked as a code sample. You can also highlight the code and click the "code" button (with "{}" on it). You can use backticks  to mark your inline code as I did in my edit. –  percusse Oct 24 '12 at 20:25
Please provide a complete, minimal working example (MWE) that shows your problem rather than code snippets. It speeds up the possibility of getting solutions without having to recreating something from scratch. –  Werner Oct 24 '12 at 20:27
Sorry, it's the first time I ask. Here it is. –  Gitano Oct 26 '12 at 19:34

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}

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

\pretocmd{\subsection}{%
\ifnum\value{subsection}=0\relax
\fi}{}{}{}

\pretocmd{\section}{%
\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}


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

\documentclass{book}
\usepackage{etoolbox}

\newlength\rightmargintoc
\setlength\rightmargintoc{\linewidth}

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

\AtEndDocument{%
\ifnum\value{subsection}>0\relax
\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:

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

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

\newlength\rightmargintoc
\setlength\rightmargintoc{\linewidth}

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

\AtEndDocument{%
\ifnum\value{subsection}>0\relax
\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}

\newlength\rightmargintoc
\setlength\rightmargintoc{\linewidth}

\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

\AtEndDocument{%
\ifnum\value{subsection}>0\relax
\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:

-
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