Text FormattingFormatting for the Section Counter

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

Formatting for the Section Counter

Post by svend_tveskaeg »

Hi all.

Consider the following MWE:

Code: Select all

\documentclass{article}
\usepackage[danish]{babel}
\usepackage{titlesec}
\usepackage{hyperref}

\renewcommand{\thesection}{\thepart.\arabic{section}}

\begin{document}

\tableofcontents

\part{The First Part}

\section{Test~1}

\section{Test~2}

\section{Test~3}

\part{The second Part}
\section{Test~4}

\section{Test~5}

\section{Test~6}

\end{document}
I have two problems:
  1. The space between the \section number and the title in the ToC is wrong.
  2. I would like to restart the \section counter in the beginning of each \part. The \section numbers should be I.1, I.2, I.3, II.1, II.2, II.3.
Thank you in advance!
Last edited by svend_tveskaeg on Mon Feb 06, 2012 12:59 am, edited 7 times in total.
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Formatting for the Section Counter

Post by localghost »

svend_tveskaeg wrote:[…] I have two problems:
  1. The space between the \section number and the title in the ToC is wrong.
  2. I would like to restart the \section counter in the beginning of each \part. The \section numbers should be I.1, I.2, I.3, II.1, II.2, II.3.
[…]
Both problems are solved by the below code. Since you already included the titlesec package, its twin titletoc is first choice for the section number formatting in the ToC. The settings could similarly be done by the tocloft package.

Code: Select all

\documentclass[11pt,danish]{article}
\usepackage[T1]{fontenc}
\usepackage{selinput}     % semiautomatic input selection
\SelectInputMappings{     % determination of input encoding
  ae={æ},                 % by selected glyphs
  oslash={ø},             % for details see
  Euro={€}                % http://partners.adobe.com/public/developer/en/opentype/glyphlist.txt
}
\usepackage{babel}
\usepackage{titlesec}
\usepackage{titletoc}

\makeatletter
\@addtoreset{section}{part}   % reset section counter in every part
\makeatother

\titlecontents{section}[2.3em]{}{\contentslabel{2.3em}}{\hspace*{-2.3em}}{\titlerule*[1pc]{.}\contentspage}

\renewcommand{\thesection}{\thepart.\arabic{section}}

\begin{document}
  \tableofcontents

  \part{The First Part}

    \section{Test~1}

    \section{Test~2}

    \section{Test~3}

  \part{The second Part}

    \section{Test~4}
\end{document}
For details refer to the package manuals.

Note the semiautomatic input selection by the selinput package from the oberdiek bundle. For European languages other than English it is preferable to fixed input encoding by the inputenc package. Especially code samples that are copied and pasted will work out of the box without the need to determine a fixed input encoding.


Thorsten
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

Formatting for the Section Counter

Post by svend_tveskaeg »

Thank you, Thorsten!

This is almost exactly what I want. The only small problem is that I would like "Del I", "Del II", ... to be horizontically centered on the page instead of left flushed.

Update:
Never mind. ;)
Last edited by svend_tveskaeg on Sat Feb 04, 2012 5:06 pm, edited 1 time in total.
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

Formatting for the Section Counter

Post by svend_tveskaeg »

Hmm! I see now that I have another problem:

Assume the (trivial?) extension of the code in Thorsten's first post:

Code: Select all

\documentclass[11pt,danish]{article}
\usepackage[T1]{fontenc}
\usepackage{selinput}     % semiautomatic input selection
\SelectInputMappings{     % determination of input encoding
  ae={æ},                 % by selected glyphs
  oslash={ø},             % for details see
  Euro={€}                % http://partners.adobe.com/public/developer/en/opentype/glyphlist.txt
}
\usepackage{babel}
\usepackage{titlesec}
\usepackage{titletoc}

\makeatletter
\@addtoreset{section}{part}             % reset section counter in every part
\@addtoreset{subsection}{section}       % reset subsection counter in every section
\@addtoreset{subsubsection}{subsection} % reset subsubsection counter in every subsection
\makeatother

\titlecontents{section}[2.3em]{}{\contentslabel{2.3em}}{\hspace*{-2.3em}}{\titlerule*[1pc]{.}\contentspage}

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


\begin{document}

\tableofcontents

\part{The First Part}

\section{Test~1}
\subsection{Test~a}
\subsubsection{Test~i}

\section{Test~2}
\subsection{Test~b}
\subsubsection{Test~ii}
\subsection{Test~c}

\part{The Second Part}

\section{Test~3}
\subsection{Test~d}
\subsubsection{Test~iii}

\section{Test~4}
\subsection{Test~e}
\subsubsection{Test~iv}
\subsection{Test~f}
\subsubsection{Test~v}

\end{document}
How do I add two line similar to

Code: Select all

\titlecontents{section}[2.3em]{}{\contentslabel{2.3em}}{\hspace*{-2.3em}}{\titlerule*[1pc]{.}\contentspage}
in order to get the correct horizontal spacing in all the ToC entries?

Thank you in advance!

Update:
I have now fixed it my using

Code: Select all

\makeatletter
  \@addtoreset{section}{part}
  \@addtoreset{subsection}{section}
  \@addtoreset{subsubsection}{subsection}
\makeatother

\titlecontents{section}[2.3em]{}{\contentslabel{2.3em}}{\hspace*{-2.3em}}{\titlerule*[1pc]{.}\contentspage}
\titlecontents{subsection}[5.1em]{}{\contentslabel{2.8em}}{\hspace*{-2.3em}}{\titlerule*[1pc]{.}\contentspage}
\titlecontents{subsubsection}[8.9em]{}{\contentslabel{3.8em}}{\hspace*{-2.3em}}{\titlerule*[1pc]{.}\contentspage}

\renewcommand{\thesection}{\thepart.\arabic{section}}
\renewcommand{\thesubsection}{\thesection.\arabic{subsection}}
\renewcommand{\thesubsubsection}{\thesubsection.\arabic{subsubsection}}
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
Post Reply