This is how the original TOC looks.
This original format of the table of contents takes too much space. I wanted some format that is compact and found this sample.
I am also searching for other nicer looking formats that are compact. For example
table of contents
This is how the original TOC looks.
This original format of the table of contents takes too much space. I wanted some format that is compact and found this sample.
I am also searching for other nicer looking formats that are compact. For example
Nothing fancy ... just copy-and-paste of the relevant macro definitions from the LaTeX sources and from article.cls
.
\documentclass{IOS-Book-Article}
\makeatletter
% Copied from the LaTeX sources
\def\addcontentsline#1#2#3{%
\addtocontents{#1}{\protect\contentsline{#2}{#3}{\thepage}}}
\long\def\addtocontents#1#2{%
\protected@write\@auxout
{\let\label\@gobble \let\index\@gobble \let\glossary\@gobble}%
{\string\@writefile{#1}{#2}}}
% Copied from article.cls
\newcommand\@pnumwidth{1.55em}
\newcommand\@tocrmarg{2.55em}
\newcommand\@dotsep{4.5}
\setcounter{tocdepth}{3}
\newcommand\tableofcontents{%
\section*{\contentsname
\@mkboth{%
\MakeUppercase\contentsname}{\MakeUppercase\contentsname}}%
\@starttoc{toc}%
}
\newcommand*\l@section[2]{%
\ifnum \c@tocdepth >\z@
\addpenalty\@secpenalty
\addvspace{1.0em \@plus\p@}%
\setlength\@tempdima{1.5em}%
\begingroup
\parindent \z@ \rightskip \@pnumwidth
\parfillskip -\@pnumwidth
\leavevmode \bfseries
\advance\leftskip\@tempdima
\hskip -\leftskip
#1\nobreak\hfil \nobreak\hb@xt@\@pnumwidth{\hss #2}\par
\endgroup
\fi}
\newcommand*\l@subsection{\@dottedtocline{2}{1.5em}{2.3em}}
\newcommand*\l@subsubsection{\@dottedtocline{3}{3.8em}{3.2em}}
\newcommand*\l@paragraph{\@dottedtocline{4}{7.0em}{4.1em}}
\newcommand*\l@subparagraph{\@dottedtocline{5}{10em}{5em}}
\newcommand\listoffigures{%
\section*{\listfigurename}%
\@mkboth{\MakeUppercase\listfigurename}%
{\MakeUppercase\listfigurename}%
\@starttoc{lof}%
}
\newcommand*\l@figure{\@dottedtocline{1}{1.5em}{2.3em}}
\newcommand\listoftables{%
\section*{\listtablename}%
\@mkboth{%
\MakeUppercase\listtablename}%
{\MakeUppercase\listtablename}%
\@starttoc{lot}%
}
\let\l@table\l@figure
\newcommand\contentsname{Contents}
\newcommand\listfigurename{List of Figures}
\newcommand\listtablename{List of Tables}
\makeatother
\begin{document}
\tableofcontents
\section{foo}
\subsection{bar}
Some text.
\end{document}
EDIT: Removed definition of \l@part
because IOS-Book-Article
doesn't feature a \part
command.
My solution goes like this (I actually extended this to include the subsubsections too):
Load hyperref
w/o ToC links (option linktoc=none
) to define them manually. Here I decided that sections should link to whole pages they start on, while subsections link normally.
Define custom counter for numberred subsections -- for referencing, in main matter only.
Customize ToC entries for sections with titletoc
(it's easier).
Customize Toc entries for sub- and subsubsections with redefining \l@subsection
and \l@subsubsection
respectively, with help of some etoolbox
toggles. I could customize these entries with titletoc
, but then they would not be justified and therefore look much worse.
Finally, switch \hypersetup{linktoc=page}
so that links on pages like List of Figures etc will work normally.
The code:
\documentclass[12pt]{article}
\usepackage[cp1250]{inputenc}
\usepackage{lmodern} % for the looks
\usepackage{biolinum} % for the looks
\usepackage[T1]{fontenc}
\usepackage{etoolbox}
\usepackage{titletoc}
\usepackage[colorlinks,linktoc=none]{hyperref}
\newcounter{numSubSection}
\setcounter{numSubSection}{0}
\gpreto{\subsection}{%
\refstepcounter{numSubSection}}%
\titlecontents{section}[0pt]%
{\addvspace{6pt}\sffamily\bfseries\normalsize%
\makebox[18pt][r]{%
\hyperlink{page.\thecontentspage}{\textbf{\thecontentspage}}%
\hspace{6pt}}}%
{\makebox[30pt][r]{\thecontentslabel\hspace{12pt}}}%
{\makebox[30pt][r]{\null}}%
{}%
\makeatletter
\newlength{\TOCleftSkip}
\setlength{\TOCleftSkip}{66pt}%
\newlength{\TOCrightSkip}
\setlength{\TOCrightSkip}{18pt}%
\newtoggle{ifSub}
\togglefalse{ifSub}%
\newtoggle{ifSection}
\toggletrue{ifSection}%
\newcounter{RefSubSection}%
\setcounter{RefSubSection}{0}%
\renewcommand*\theRefSubSection{\@arabic\c@RefSubSection}%
\renewcommand*{\tableofcontents}{%
\section*{\contentsname}%
\addcontentsline{toc}{section}{\contentsname}%
\toggletrue{ifSection}%
\setcounter{RefSubSection}{0}%
\@starttoc{toc}}%
\gpreto{\l@section}{%
\nottoggle{ifSection}
{\par%
\addtolength{\leftskip}{-\TOCleftSkip}%
\addtolength{\rightskip}{-\TOCrightSkip}%
\togglefalse{ifSub}}{}%
\toggletrue{ifSection}}%
\renewcommand*{\l@subsection}[2]{%
\stepcounter{RefSubSection}%
\toggletrue{ifSub}%
\iftoggle{ifSection}
{\togglefalse{ifSection}%
\addtolength{\leftskip}{\TOCleftSkip}%
\addtolength{\rightskip}{\TOCrightSkip}%
\noindent}%
{\unskip\begingroup\upshape\small\ \textbullet~\endgroup}%
\begingroup%
\upshape\small#1\ \hyperlink{numSubSection.\theRefSubSection}{\textbf{#2}}%
\endgroup}%
\renewcommand*{\l@subsubsection}[2]{%
\iftoggle{ifSection}
{\togglefalse{ifSection}%
\addtolength{\leftskip}{\TOCleftSkip}%
\addtolength{\rightskip}{\TOCrightSkip}%
\noindent}%
{\iftoggle{ifSub}
{\togglefalse{ifSub}\relax%
\unskip\begingroup\upshape\small:\ \endgroup}%
{\unskip\begingroup\itshape\small,\ \endgroup}}%
\begingroup\itshape\small#1\endgroup}%
\renewcommand*{\numberline}[1]{}%
\makeatother
% solution ends here, code below is just for the example
\begin{document}
% Frontmatter - only unnumbered/unlabeled sections
\pagenumbering{roman}
\clearpage
\tableofcontents
\clearpage
\section*{Preface}
\addcontentsline{toc}{section}{Preface}
\clearpage
\section*{Another Preface}
\addcontentsline{toc}{section}{Another Preface}
% Mainmatter - only numbered/labeled sections
\clearpage
\addtocontents{toc}{\protect\vspace{18pt}} % just some separation
\pagenumbering{arabic}
%\clearpage
\section{First}
\subsection{Australia}
\subsubsection{Alpha}
\subsubsection{Beta}
\subsubsection{Charlie}
\subsubsection{Delta}
\subsubsection{Echo}
\subsubsection{Foxtrot}
\subsection{Brazil}
\subsubsection{Golf}
\subsubsection{Hotel}
\subsubsection{India}
\subsection{Canada}
\clearpage
\section{Second}
\clearpage
\section{Third}
\subsection{Denmark}
\subsubsection{Juliet}
\subsubsection{Kilo}
\subsubsection{Lima}
\subsubsection{Mike}
\subsubsection{November}
\subsection{Egypt}
\subsubsection{Oscar}
\subsubsection{Papa}
\subsubsection{Quebec}
\subsubsection{Romeo}
\clearpage
\section{Fourth}
\subsection{Georgia}
\subsubsection{Sierra}
\subsection{Haiti}
\subsubsection{Tango}
\subsubsection{Uniform}
\subsubsection{Victor}
\subsubsection{Whiskey}
\subsection{Iceland}
\subsection{Japan}
\subsubsection{X-ray}
\subsubsection{Yankee}
\subsubsection{Zulu}
\subsubsection{Alpha}
\subsubsection{Beta}
\subsubsection{Charlie}
\subsubsection{Delta}
\subsubsection{Echo}
\subsection{Kenya}
\subsubsection{Foxtrot}
\subsection{Laos}
\subsubsection{Golf}
\subsubsection{Hotel}
\subsubsection{India}
\subsubsection{Juliet}
\subsubsection{Kilo}
% Backmatter - all (sub)sections w/o labels
\clearpage
\addtocontents{toc}{\protect\vspace{18pt}} % just some separation
\hypersetup{linktoc=page}
\section*{References}
\addcontentsline{toc}{section}{References}
\clearpage
\section*{List of Figures}
\addcontentsline{toc}{section}{List of Figures}
\clearpage
\section*{Index}
\addcontentsline{toc}{section}{Index}
\end{document}
This could most likely be done much easier/prettier, but at least it does the job. Hopefully everything is quite clear ;-)
The code translates to something like this:
Best Answer
The
titletoc
can achieve this though the\titlecontents
command. The starred version places all the titles on the same paragraph.The command isn't the most user friendly to use, so I tried to annotate as best as I could.
An important thing to note is that due to the way the table of contents is generated, LaTeX struggles to create line breaks. In order to allow line breaks between successive elements, I have added
\space
in theabove code
and also in the separator. Do not use~
as this is creates a space that cannot break across lines. The only time I have used~
is between the a section's number and its title in order to avoid: