One option would be to redefine \subsection
as implemented in article.cls
. In the redefinition you test for the value of the subsection
counter; if the value is greater then one, do nothing; if it's equal to one, add the space to the ToC:
\documentclass[12pt]{article}
\usepackage{tocloft}
\setlength\cftparskip{0pt}
\setlength\cftaftertoctitleskip{44pt}
\makeatletter
\renewcommand\subsection{\@startsection{subsection}{2}{\z@}%
{-3.25ex\@plus -1ex \@minus -.2ex}%
{1.5ex \@plus .2ex}%
{\normalfont\large\bfseries%
\ifnum\value{subsection}>1 \else \addtocontents{toc}{\protect\addvspace{22pt}}\fi}}
\makeatother
\begin{document}
\tableofcontents
\section{Test section one}
\subsection{test section one one}
\subsection{test section one two}
\section{Test section two}
\subsection{test section two one}
\subsection{test section two two}
\clearpage
\section{Test section three}
\subsection{test section three one}
\subsection{test section three two}
\end{document}
Patching the \subsection
command with the help of \patchcmd
from the etoolbox
package simplifies the code:
\documentclass[12pt]{article}
\usepackage{tocloft}
\usepackage{etoolbox}
\setlength\cftparskip{0pt}
\setlength\cftaftertoctitleskip{44pt}
\patchcmd{\subsection}{\bfseries}%
{\ifnum\value{subsection}>1 \else \addtocontents{toc}{\protect\addvspace{22pt}}\fi}{}{}
\begin{document}
\tableofcontents
\section{Test section one}
\subsection{test section one one}
\subsection{test section one two}
\section{Test section two}
\subsection{test section two one}
\subsection{test section two two}
\clearpage
\section{Test section three}
\subsection{test section three one}
\subsection{test section three two}
\end{document}
My goal is to have {the section number}\heartsuit{the section title} with no space in-between the characters.
Then the following will do:
\documentclass{article}
\usepackage{tocloft,lipsum}
\makeatletter
\renewcommand\tableofcontents{%
\section*{\contentsname\@mkboth{}{}}%
\thispagestyle{empty}%
\@starttoc{toc}%
\thispagestyle{empty}%
\clearpage%
}
\renewcommand{\numberline}[1]{%
\@cftbsnum #1\@cftasnum\@cftasnumb}
\renewcommand{\@dotsep}{2}
\renewcommand{\cftpnumalign}{r}
\renewcommand{\cftsecfont}{\bfseries}
\renewcommand{\cftsecleader}{\bfseries\cftdotfill{\cftsecdotsep}}
\renewcommand{\cftsecpagefont}{\bfseries}
\renewcommand{\cftsecaftersnum}{$\heartsuit$}
\makeatother
\begin{document}
\tableofcontents
\section{1}\lipsum[4-11]\section{1}\lipsum[4-11]\section{1}\lipsum[4-11]\section{1}\lipsum[4-11]
\section{1}\lipsum[4-11]\section{1}\lipsum[4-11]\section{1}\lipsum[4-11]\section{1}\lipsum[4-11]
\section{1}\lipsum[4-11]\section{1}\lipsum[4-11]\section{1}\lipsum[4-11]\section{1}\lipsum[4-11]
\section{1}\lipsum[4-11]\section{1}\lipsum[4-11]\section{1}\lipsum[4-11]\section{1}\lipsum[4-11]
\section{1}\lipsum[4-11]\section{1}\lipsum[4-11]\section{1}\lipsum[4-11]\section{1}\lipsum[4-11]
\section{1}\lipsum[4-11]\section{1}\lipsum[4-11]\section{1}\lipsum[4-11]\section{1}\lipsum[4-11]
\section{1}\lipsum[4-11]\section{1}\lipsum[4-11]\section{1}\lipsum[4-11]\section{1}\lipsum[4-11]
\section{1}\lipsum[4-11]\section{1}\lipsum[4-11]\section{1}\lipsum[4-11]\section{1}\lipsum[4-11]
\section{1}\lipsum[4-11]\section{1}\lipsum[4-11]\section{1}\lipsum[4-11]\section{1}\lipsum[4-11]
\section{1}\lipsum[4-11]\section{1}\lipsum[4-11]\section{1}\lipsum[4-11]\section{1}\lipsum[4-11]
\section{1}\lipsum[4-11]\section{1}\lipsum[4-11]\section{1}\lipsum[4-11]
\end{document}
I think the solution is to make the section numbers ragged on the right, just like all the page numbers are.
You probably mean \raggedleft
(or flush right). For this you can use the following definition of \numberline
and set \cftsecnumwidth
:
\renewcommand{\numberline}[1]{%
\makebox[\@tempdima][r]{\@cftbsnum #1\@cftasnum}\@cftasnumb}
\renewcommand{\cftsecnumwidth}{2em}
The package does not seem to offer a manner to control the box controlling the section number. Is this possible with titletoc
?
This is perhaps only partially true. However, for an inexperienced user, it's not that obvious how one can manipulate the macros to achieve what you're looking for. I'll just show some information below, since the control of the boxes really depends on what you want to do.
According to the tocloft
documentation, the construction of the ToC entry has the following form for sectional unit X
:
This is a simplied pseudo-code version for the typesetting of numbered [...] entries.
{\cftXfont {\cftXpresnum SNUM\cftXaftersnum\hfil} \cftXaftersnumb TITLE}%
{\cftXleader}{\cftXpagefont PAGE}\cftXafterpnum\par
In terms of the actual macro contents for section, the above pseudo-code is contained within \l@section
, \numberline
and \cftsecfillnum
(taken from tocloft.dtx
with comments added to highlight certain elements):
\renewcommand*{\l@section}[2]{%
\ifnum \c@tocdepth >\z@
\if@cfthaschapter
\vskip \cftbeforesecskip
\else
\addpenalty\@secpenalty
\addvspace{\cftbeforesecskip}
\fi
{\leftskip \cftsecindent\relax
\rightskip \@tocrmarg
\parfillskip -\rightskip
\parindent \cftsecindent\relax\@afterindenttrue
\interlinepenalty\@M
\leavevmode
\@tempdima \cftsecnumwidth\relax
\let\@cftbsnum \cftsecpresnum
\let\@cftasnum \cftsecaftersnum
\let\@cftasnumb \cftsecaftersnumb
\advance\leftskip \@tempdima \null\nobreak\hskip -\leftskip
{\cftsecfont #1}\nobreak% <----- This is \cftXfont; #1 relates to \numberline
\cftsecfillnum{#2}}%
\fi}
\renewcommand{\numberline}[1]{%
\hb@xt@\@tempdima{\@cftbsnum #1\@cftasnum\hfil}\@cftasnumb}% <-- This is all similar to
% {\cftXpresnum SNUM\cftXaftersnum\hfil} \cftXaftersnumb TITLE}
\newcommand{\cftsecfillnum}[1]{%
{\cftsecleader}\nobreak% <--- This is {\cftXleader}
\makebox[\@pnumwidth][\cftpnumalign]{\cftsecpagefont #1}\cftsecafterpnum\par% <- This is
% {\cftXpagefont PAGE}\cftXafterpnum\par
}
It's clear from \numberline
that the default is "ragged right" (or left aligned) due to the \hfil
on the right. However, we can achieve a "ragged left" (or right aligned) look by changing \cftXpresnum
to be \hfil
and let \cftXaftersnum
(insert a $\heartsuit$
by also) \@gobble
the right-hand \hfil
:
\renewcommand{\cftsecpresnum}{\hfil}
\renewcommand{\cftsecaftersnum}{$\heartsuit$\@gobble}
The above would be similar to our above approach of using
\renewcommand{\numberline}[1]{%
\makebox[\@tempdima][r]{\@cftbsnum #1\@cftasnum}\@cftasnumb}
Here is an updated solution that aligns the ToC-entries at the indentation levels:
\documentclass{article}
\usepackage{tocloft,lipsum}
\makeatletter
\renewcommand\tableofcontents{%
\section*{\contentsname\@mkboth{}{}}%
\thispagestyle{empty}%
\@starttoc{toc}%
\thispagestyle{empty}%
\clearpage%
}
\renewcommand{\@dotsep}{2}
\renewcommand{\cftpnumalign}{r}
\renewcommand{\cftsecfont}{\bfseries}
\renewcommand{\cftsecleader}{\bfseries\cftdotfill{\cftsecdotsep}}
\renewcommand{\cftsecpagefont}{\bfseries}
\renewcommand{\cftsecaftersnum}{\hfill$\bullet$\hfill\null}
\renewcommand{\cftsecnumwidth}{2em}% <--------------------------------------
% | <--------
\renewcommand{\cftsubsecaftersnum}{\hfill$\bullet$\hfill\null} % | |
\renewcommand{\cftsubsecindent}{2em}% Width of \section number in ToC ------- |
\renewcommand{\cftsubsecnumwidth}{2.5em} % |
% |
\renewcommand{\cftsubsubsecaftersnum}{\hfill$\bullet$\hfill\null} % |
\renewcommand{\cftsubsubsecindent}{4.5em}% Width of \section + \subsection number ------
\renewcommand{\cftsubsubsecnumwidth}{3.25em}
\renewcommand{\numberline}[1]{\makebox[\@tempdima][l]{\@cftbsnum #1\@cftasnum}\@cftasnumb}
\makeatother
\begin{document}
\tableofcontents
\section{Section}\lipsum[4-11]\subsection{Subsection}\lipsum[4-11]\subsubsection{Subsubsection}\lipsum[4-11]
\section{Section}\lipsum[4-11]\subsection{Subsection}\lipsum[4-11]\subsubsection{Subsubsection}\lipsum[4-11]
\section{Section}\lipsum[4-11]\subsection{Subsection}\lipsum[4-11]\subsubsection{Subsubsection}\lipsum[4-11]
\section{Section}\lipsum[4-11]\subsection{Subsection}\lipsum[4-11]\subsubsection{Subsubsection}\lipsum[4-11]
\section{Section}\lipsum[4-11]\subsection{Subsection}\lipsum[4-11]\subsubsection{Subsubsection}\lipsum[4-11]
\section{Section}\lipsum[4-11]\subsection{Subsection}\lipsum[4-11]\subsubsection{Subsubsection}\lipsum[4-11]
\section{Section}\lipsum[4-11]\subsection{Subsection}\lipsum[4-11]\subsubsection{Subsubsection}\lipsum[4-11]
\section{Section}\lipsum[4-11]\subsection{Subsection}\lipsum[4-11]\subsubsection{Subsubsection}\lipsum[4-11]
\section{Section}\lipsum[4-11]\subsection{Subsection}\lipsum[4-11]\subsubsection{Subsubsection}\lipsum[4-11]
\section{Section}\lipsum[4-11]\subsection{Subsection}\lipsum[4-11]\subsubsection{Subsubsection}\lipsum[4-11]
\section{Section}\lipsum[4-11]\subsection{Subsection}\lipsum[4-11]\subsubsection{Subsubsection}\lipsum[4-11]
\end{document}
Best Answer
If the skip should be increased before any
\section
entry\cftbeforesecskip
is the right command to be changed.Adjust the value to your needs.