6

I want roman section numbers to be followed by a period in TOC and section titles. Here is the MWE.

\documentclass{book}
\renewcommand{\thesection}{\Roman{section}.}
\begin{document}
 \tableofcontents
 \section{First section\label{l}}
 This is Section \ref{l}.
\end{document}

This results in

Extra period in ref

An extra period in the reference appears. However, if I remove the trailing period from

\renewcommand{\thesection}{\Roman{section}.},

the dots in TOC and section label disappear with it.

No period in TOC and section label

How can I solve this? Thank you.

  • Remove the trailing period from This is Section \ref{l}. i.e. make it This is Section \ref{l} – Moriambar Apr 24 '17 at 7:35
  • 1
    @Moriambar This workaround will not do for sentences with inner reference, e.g. Section \ref{l} contains new definitions will have an undesired period in the middle. – Anvita Apr 24 '17 at 7:39
  • 2
    adding dot in the definition of \thesection is not good idea – touhami Apr 24 '17 at 8:26
4

The writing of the dot . to the .aux file for the label must be prevented, so the trick is to attack \@currentlabel when it comes to write \p@section.

This gobbles the dot in\thesection by changing \p@section and writes \Roman{section} again for the references, but keeps the I. format for the ToC and headings.

There's no other class or package involved, the disadvantage is that the definition of \thesection must be remembered.

See Writing theorem conditions with automatic numbering and labels for another application of this 'trick'.

\documentclass{book}

\renewcommand{\thesection}{\Roman{section}.}


\makeatletter
\def\remove@@dot\csname the#1\endcsname{\Roman{#1}}
\def\p@section{\remove@@dot}
\makeatother

\begin{document}
 \tableofcontents
 \section{First section\label{l}}
 This is Section \ref{l}.
\end{document}

enter image description here

  • @Mico: Yes, the \p@... macros are underrated or widely unknown... – user31729 Apr 24 '17 at 8:17
  • Just out of curiosity: It looks like \remove@@dot isn't defined to take arguments, yet its definition contains two instances of #1. Is what's really going on that \remove@@dot makes \p@section gobble its argument and insert \Roman{#1} instead? – Mico Apr 24 '17 at 8:24
  • 1
    @Mico: \csname the#1\endcsname is basically what is in \@currentlabel after \p@#1, so \p@section must gobble the following \csname the#1\endcsname` and insert something different. I think, there are other methods to achieve this, but I need a bunch of \expandafters instead then – user31729 Apr 24 '17 at 8:32
4

The easiest way is to change the class. scrbook from the KOMA-bundle has an "autodot" feature which adds the period only at the appropriate places:

\documentclass{scrbook}
\renewcommand{\thesection}{\Roman{section}}

\begin{document}
 \tableofcontents
 \section{First section\label{l}}
 This is Section \ref{l}.
\end{document}

enter image description here

  • This approach adds dots after the numbers of all sectioning headers, not just section-level headers. (Of course, this may be what the OP actually wants; however, I wouldn't simply assume that this is the case.) Is there a way to restrict the scope of the "autodot" feature to just section-level headers? – Mico Apr 24 '17 at 8:10
  • @Mico: I know that but prefer to let the OP discover flaws in their problem description themselves. It is possible to configure all levels independently, but as it is not very logical it needs more commands (and a bit thinking). – Ulrike Fischer Apr 24 '17 at 8:43
4

Here's a solution that works with the book document class. It (a) uses the tocloft package and (b) resets the low-level LaTeX macro \@seccntformat for section-level entries.

\documentclass{book}

\renewcommand{\thesection}{\Roman{section}}
\makeatletter
\def\@seccntformat#1{\@ifundefined{#1@cntformat}%
   {\csname the#1\endcsname\quad}%     default
   {\csname #1@cntformat\endcsname}}%  enable individual control
\newcommand\section@cntformat{\thesection.\quad} % for section-level entries
\makeatother
\usepackage[titles]{tocloft}
\renewcommand{\cftsecaftersnum}{.} % place "." after section-level "number"

\begin{document}
\tableofcontents
\section{First section\label{l}}
This is Section \ref{l}.
\end{document}
  • (+1) for not using ....sec or .... OMA ... ;-) – user31729 Apr 24 '17 at 8:16
4

You can use titlesec and titletoc. But then a dot is added to the numbers of all section levels.

\documentclass{book}
\renewcommand{\thesection}{\Roman{section}}
\usepackage{titlesec}
\titlelabel{\thetitle.\enskip}
\usepackage[dotinlabels]{titletoc}
\begin{document}
\tableofcontents
\section{First section\label{l}}
Section \ref{l} contains new definitions.
\end{document}

enter image description here


Here is an additional KOMA-Script suggestion that adds the dot only to the section level:

\documentclass[
  numbers=noenddot
  ]
  {scrbook}
\addtokomafont{disposition}{\rmfamily}
\renewcommand{\thesection}{\Roman{section}}

\usepackage{xpatch}
\xpatchcmd{\sectionformat}{\autodot}{.}{}{\PatchFailed}
\newcommand\sectionentrynumberformat[1]{\renewcommand\autodot{.}#1}
\RedeclareSectionCommand[
  tocentrynumberformat=\sectionentrynumberformat
]{section}

\usepackage{blindtext}
\begin{document}
 \tableofcontents
 \section{First section\label{l}}
 Section \ref{l} contains new definitions.
%
\blinddocument
\end{document}

enter image description here

enter image description here

  • You may want to mention explicitly that your proposed method appends . ("dot") to the "numbers" of all levels of sectioning headers -- not just to the (Roman) numbers of section-level headers. Of course, this may be exactly what the OP wants; however, I wouldn't simply assume that it's the case. – Mico Apr 24 '17 at 8:04

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.