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.

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}
share|improve this question
    
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
1  
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

1 Answer 1

up vote 7 down vote accepted

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

share|improve this answer
    
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

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.