2

I want section numbering in ToC to appear as

\renewcommand\thesection{\Roman{section}}
\renewcommand\thesubsection{\alph{subsection}}
\renewcommand\thesubsubsection{\arabic{subsubsection}}

but in text the full numbering should be seen:

\renewcommand\thesection{\Roman{section}}
\renewcommand\thesubsection{\Roman{section}.\alph{subsection}}
\renewcommand\thesubsubsection{\Roman{section}.\alph{subsection}.\arabic{subsubsection}}

Is it possible with titlesec/titletoc?

2
  • Ooh, i think this will be a little bit confusing for the reader, depending how long the document (and levelled) it is.
    – Johannes_B
    Dec 26 '14 at 8:27
  • @Johannes_B: but citing all the subnumbers in ToC is ugly?
    – Adobe
    Dec 26 '14 at 8:35
3

Enclose the parts you don't want in the TOC in a macro that's usually defined just to print its contents, but when doing the note in the .aux file for the TOC entry it's redefined to gobble its contents.

The macro responsible for the annotation is \addtocontents that already does something like this, by redefining \label, \index and \glossary to gobble their arguments; so we just need to add another macro to the list.

\documentclass{article}

\usepackage{etoolbox}
\usepackage{xcolor}
\definecolor{Blue}{HTML}{2E3192}

% heading styles:
\usepackage{titlesec}
\titleformat{\section}
  {\color{teal}\large\sffamily}
  {\thetitle.\hspace{0.5cm}}
  {0cm}
  {}

\titleformat{\subsection}
  {\color{violet}\bfseries}
  {\hspace{0.75cm}\thetitle.\hspace{0.3cm}}
  {0cm}
  {}

\titleformat{\subsubsection}
  {\color{olive}\itshape}
  {\hspace{1.3cm}\thetitle.\hspace{0.3cm}}
  {0cm}
  {}

% ToC styles:
\usepackage[dotinlabels]{titletoc}
\contentsmargin{2.55em} % space for a page number

\titlecontents{section}
    [3.8em] % 1.5em + 2.3em
    {\vspace{0.3cm}\sffamily\bfseries\large}
    {\contentslabel{2.3em}}
    {\hspace*{-2.3em}}
    {\normalsize\normalfont\rmfamily\titlerule*[1pc]{.}\contentspage}
    [\vspace{0.1cm}]

\titlecontents{subsection}
    [5.2em] % 3.8em + 1.3em
    {\vspace{0.1cm}}
    {\contentslabel{1.4em}}
    {\hspace*{-1.4em}}
    {\titlerule*[1pc]{.}\contentspage}
    []

\titlecontents{subsubsection}
    [6.5em] % 5.2em + 1.3em
    {\vspace{0.1cm}}
    {\contentslabel{1.3em}}
    {\hspace*{-1.3em}}
    {\titlerule*[1pc]{.}\contentspage}
    []

\makeatletter
\let\perhapsprint\@firstofone
\patchcmd{\addtocontents}
  {\glossary\@gobble}
  {\glossary\@gobble\let\perhapsprint\@gobble}
  {}{}
%\newcommand{\perhapsprint}[1]{\ifprintprefix#1\fi}
%\newif\ifprintprefix
\renewcommand\thesection{\Roman{section}}
\renewcommand\thesubsection{%
  \perhapsprint{\Roman{section}.}%
  \alph{subsection}%
}
\renewcommand\thesubsubsection{%
  \perhapsprint{\Roman{section}.\alph{subsection}.}%
  \arabic{subsubsection}%
}

\usepackage{lipsum}

\begin{document}

\tableofcontents

\section{A section}\label{1}

References: \ref{1}, \ref{2}, \ref{3}, \ref{4}.

\lipsum[1]
\subsection{A subsection}\label{2}
\lipsum[2-4]
\subsection{Another subsection}\label{3}
\lipsum[5]
\subsubsection{A subsubsection}\label{4}
\lipsum[6]
\subsection{A subsection}\lipsum[7-8]
\subsection{Another subsection}\lipsum[9-10]
\section{Another section}\lipsum[1-2]
\subsection{A subsection}\lipsum[3-4]
\subsection{Another subsection}\lipsum[5-6]
\subsection{A subsection}\lipsum[7-8]
\subsection{Another subsection}\lipsum[9-10]
\end{document}

enter image description here

2

Oh, I solved it. Re-defining section formats in the titleformat works:

\documentclass{article}

\usepackage{xcolor}
\definecolor{Blue}{HTML}{2E3192}

% heading styles:
\usepackage{titlesec}
\titleformat{\section}
  {\color{teal}\large\sffamily}
  {\thetitle.\hspace{0.5cm}}
  {0cm}
  {}

\titleformat{\subsection}
  {\color{violet}\bfseries}
  {\renewcommand\thesubsection{\Roman{section}.\alph{subsection}}\hspace{0.75cm}\thetitle.\hspace{0.3cm}}
  {0cm}
  {}

\titleformat{\subsubsection}
  {\color{olive}\itshape}
  {\renewcommand\thesubsubsection{\Roman{section}.\alph{subsection}.\arabic{subsubsection}}\hspace{1.3cm}\thetitle.\hspace{0.3cm}}
  {0cm}
  {}

% ToC styles:
\usepackage[dotinlabels]{titletoc}
\contentsmargin{2.55em} % space for a page number

\titlecontents{section}
    [3.8em] % 1.5em + 2.3em
    {\vspace{0.3cm}\sffamily\bfseries\large}
    {\contentslabel{2.3em}}
    {\hspace*{-2.3em}}
    {\normalsize\normalfont\rmfamily\titlerule*[1pc]{.}\contentspage}
    [\vspace{0.1cm}]

\titlecontents{subsection}
    [5.2em] % 3.8em + 1.3em
    {\vspace{0.1cm}}
    {\contentslabel{1.4em}}
    {\hspace*{-1.4em}}
    {\titlerule*[1pc]{.}\contentspage}
    []

\titlecontents{subsubsection}
    [6.5em] % 5.2em + 1.3em
    {\vspace{0.1cm}}
    {\contentslabel{1.3em}}
    {\hspace*{-1.3em}}
    {\titlerule*[1pc]{.}\contentspage}
    []

\renewcommand\thesection{\Roman{section}}
\renewcommand\thesubsection{\alph{subsection}}
\renewcommand\thesubsubsection{\arabic{subsubsection}}

\usepackage{lipsum}

\begin{document}

\tableofcontents

\section{A section}\lipsum[1]
\subsection{A subsection}\lipsum[2-4]
\subsection{Another subsection}\lipsum[5-6]
\subsection{A subsection}\lipsum[7-8]
\subsection{Another subsection}\lipsum[9-10]
\section{Another section}\lipsum[1-2]
\subsection{A subsection}\lipsum[3-4]
\subsection{Another subsection}\lipsum[5-6]
\subsection{A subsection}\lipsum[7-8]
\subsection{Another subsection}\lipsum[9-10]
\end{document}

gives:

enter image description here

2
  • Thanks for sharing, can you extend the document to make it compilable and show an example? It is easier for other help seekers that way.
    – Johannes_B
    Dec 26 '14 at 8:38
  • Uh, no! This is not the way to do it!
    – egreg
    Dec 26 '14 at 10:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.