[Tex/LaTex] Add a symbol in ToC between the section number and its section

spacingtable of contentstocloft

My problem is that there exists an overlap between the symbol to introduce and the section title; it is manifest with two \heartsuits

\documentclass{article}
\usepackage{tocloft}
\usepackage{lipsum}
%\contentslabel{3em}


\renewcommand\tableofcontents{%
\section*{\contentsname\@mkboth{}{}}%
\thispagestyle{empty}%
%\small%
\@starttoc{toc}%
\thispagestyle{empty}%
\clearpage%
}
\makeatother



\makeatletter
\renewcommand{\@dotsep}{2}
%%\renewcommand{\cftdot}{\ensuremath{\ast}}
\renewcommand{\cftpnumalign}{r}
%\renewcommand{\cftsecpresnum}{SOMETHING }
\renewcommand{\cftsecfont}{\bfseries}
\renewcommand{\cftsecleader}{\bfseries\cftdotfill{\cftsecdotsep}}
\renewcommand{\cftsecpagefont}{\bfseries}
%\renewcommand{\cftsecpresnum}{SOMETHING }
\renewcommand{\cftsecaftersnum}{$\heartsuit$}
    \renewcommand{\cftsecaftersnumb}{$\heartsuit$}
    %\renewcommand{\cftsecaftersnumb}{\\}
%   \newlength{\mylen} % a "scratch" length
%   \settowidth{\mylen}{\bfseries\cftsecpresnum\cftsecaftersnum} % extra space
%   \addtolength{\cftsecnumwidth}{\mylen} % add the extra space
    %\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}

The modifying commands come straight from the manual of tocloft.

My goal is to have {the section number}\heartsuit{the section title} with no space in-between the characters.
I think the solution is to make the section numbers ragged on the right, just like all the page numbers are.
The package does not seem to offer a manner to control the box controlling the section number. Is this possible with titletoc? (its readme is very succinct and I do not understand half of what is written)

EDITion:

@Werner: I have a snag with the code. I want to use the same pattern for the subsections and subsubsections as the one of the sections. So I use

\renewcommand{\cftsecaftersnum}{$\bullet$}
%\renewcommand{\cftsecnumwidth}{2em}

\renewcommand{\cftsubsecaftersnum}{$\bullet$}
%\renewcommand{\cftsubsecnumwidth}{0em}

\renewcommand{\cftsubsubsecaftersnum}{$\bullet$}
\renewcommand{\numberline}[1]{\makebox[\@tempdima][r]{\@cftbsnum #1\@cftasnum}\@cftasnumb} 

but the beginnings of the subsections and subsubsections is not adequate:
(I use roman numbers for my sections and sub(sub)sections)
Ideally, the number subsection (and subsubsection) must be aglin with the first letter of the sections it belongs to

so

II$\bullet$Title
           II.1$\bullet$subtitle
                        II.1.1$\bullet$subtitle

But it is not the case with the above code.

Best Answer

My goal is to have {the section number}\heartsuit{the section title} with no space in-between the characters.

Then the following will do:

enter image description here

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

enter image description here


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:

ToC-alignment

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