2

I'd like to give some section special "labels" to be printed in their titles and in the table of contents. My document describes several "axes", each of them containing one or more "themes". The "axes" are preceded by an introduction, and succeeded by some conclusions, etc.

I use titlesec to specify the prefix ("Axis", "Theme") of numbered sections and subsections, which works in the document text, but not in the table of contents.

Any idea how to get this working in the TOC ?

Also, the \addcontentsline{... is somewhat awkward, there should be better ways to customize all this.

MWE:

\documentclass{article}

\usepackage{titlesec}
\usepackage{titletoc}

\titleformat{\section}
  {\normalfont\Large\bfseries}{Axis \thesection}{1em}{}
\titlespacing*{\section}
  {0pt}{3.5ex plus 1ex minus .2ex}{2.3ex plus .2ex}

\titleformat{\subsection}
  {\normalfont\large\bfseries}{Theme \thesubsection}{1em}{}
\titlespacing*{\subsection}
  {0pt}{3.5ex plus 1ex minus .2ex}{2.3ex plus .2ex}

\begin{document}
\title{Some title}
\maketitle
\tableofcontents{}
\section*{Introduction}
\addcontentsline{toc}{section}{Introduction}
\subsubsection*{Sub-intro}
\addcontentsline{toc}{subsection}{Sub-intro}
\section{First real section}
\subsection*{Some more intro}
\addcontentsline{toc}{subsection}{Some more intro}
\subsection{This is important}
\subsection{This is also important}
\section{Second real section}
\subsection*{Some more intro}
\addcontentsline{toc}{subsection}{Some more intro}
\subsection{This is important}
\subsection{This is also important}
\end{document}

2 Answers 2

2

Here is a solution with titletoc. Also, in order to have a lighter code, I automated \addcontentsline for unnumbered sections and subsections, thanks to the numberless key of \titleformat. You can deactivate it locally ( e. g. to not include the table of contents in itself…) with the \setboolfalse{addtoc} command.

\documentclass{article}
\usepackage[x11names]{xcolor}
\usepackage[showframe]{geometry}
\usepackage[explicit]{titlesec}%
\usepackage{titletoc}
\usepackage{etoolbox}
\newbool{addtoc}%initial value: false

\titleformat{\section}[hang]
{\normalfont\Large\bfseries}{Axis \thesection}{1em}{#1}
\titleformat{name=\section, numberless}[hang]
{\normalfont\large\bfseries}{}{0em}{#1}[\ifbool{addtoc}{\addcontentsline{toc}{section}{#1}}{}]
\titlespacing*{\section}
{0pt}{3.5ex plus 1ex minus .2ex}{2.3ex plus .2ex}

 \titleformat{\subsection}
 {\normalfont\large\bfseries}{Theme \thesubsection}{1em}{#1}
 \titleformat{name=\subsection, numberless}
 {\normalfont\large\bfseries}{}{0em}{#1}[\ifbool{addtoc}{\addcontentsline{toc}{subsection}{#1}}{}]
 \titlespacing*{\subsection}
 {0pt}{3.5ex plus 1ex minus .2ex}{2.3ex plus .2ex}

\titlecontents{section}[3.5em]{\medskip\bfseries}%
{\contentslabel[\color{IndianRed3}Axis \thecontentslabel.]{3em}\enspace}%numbered
{\hskip-3em}%numberless
{\titlerule*[1.2pc]{.}\contentspage}%

\titlecontents{subsection}[6.7em]{\smallskip\bfseries}%
{\contentslabel[Theme \thecontentslabel.]{4.6em}\enspace}%numbered
{\hskip-4.6em}%numberless
{\titlerule*[1.2pc]{.}\contentspage}%

\begin{document}

\title{Some title}
\maketitle
\tableofcontents
\clearpage
\booltrue{addtoc}

\section*{Introduction}
\subsubsection*{Sub-intro}
\section{First real section}
\subsection*{Some more intro}
\subsection{This is important}
\subsection{This is also important}
\section{Second real section}
\subsection*{Some more intro}
\subsection{This is important}
\subsection{This is also important}

\end{document} 

enter image description here

6
  • Thanks, this looks really great! However, it seems to interfere with wrapfigures: the lines don't fill the text width anymore after the first wrapfigure. Would you have any idea what would cause this?
    – Jos
    Commented Nov 29, 2015 at 21:00
  • I don't understand very well. You have wrap figures in the table of contents?
    – Bernard
    Commented Nov 29, 2015 at 21:18
  • No, they are in the text. After the first occurrence of a wrapfigure, the text doen't fill the lines anymore. Should I post a MWE here or elsewhere ? Anyway, just add \usepackage{wrapfig} and \usepackage{lipsum} in the preamble, put some \lipsum commands to fill the sections, and put \begin{wrapfigure}{O}{0.4\textwidth}% \caption{Some caption} \end{wrapfigure}% somewhere
    – Jos
    Commented Nov 30, 2015 at 8:33
  • I think it is another problem. It would be fine if you could post another question, with a minimal example, which you might link to this one.
    – Bernard
    Commented Nov 30, 2015 at 11:06
  • You're right, it seems that wrapfig isn't really reliable. In your example code, it interfered with the geometry package, and in my document it couldn't handle the occurrence of a list, a subsection heading, and the wrapfig on the same page. I'll adopt your solution and will check wrapfig behaviour when the document is finished.
    – Jos
    Commented Nov 30, 2015 at 12:31
1
\usepackage{fmtcount}

\renewcommand{\thesection}{\ORDINALstring{section} AXIS}

Would get you "FIRST AXIS" "SECOND AXIS" etc. that is also in the TOC.

Because "\thesection" already exists one has to use "\renewcommand" which functions by replacing an already existing command with your own version.

4
  • It would be nice to see this in combination with the post of the O.P. Wouldn't this result in AXIS FIRST AXIS, regarding the titlesec usage?
    – user31729
    Commented Nov 29, 2015 at 17:14
  • You are right, I left out that the fmtcount package which is needed for this to work. I wish I had a visual of the desired output.
    – A Feldman
    Commented Nov 30, 2015 at 6:20
  • Nice idea to have the numbering in words, but did you try this in the MWE I provided ? Apart from spacing issues, my subsections ("themes") now read "Theme FIRST AXIS.1".
    – Jos
    Commented Nov 30, 2015 at 8:43
  • OK, I get it, I could redefine the subsection command \renewcommand{\thesubsection}{Theme \arabic{subsection}}. But can I get "Theme 1.1" ? And more importantly, the spacing in the TOC doesn't work, "FIRST AXIS" and the section title get printed on top of one another.
    – Jos
    Commented Nov 30, 2015 at 8:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .