TransWikia.com

Fancyhdr's extra dot in header for section title: How to remove?

TeX - LaTeX Asked on July 31, 2021

I like to use Arabic numbers with a dot in front of section titles. So, for example 1. My section title instead of 1 My section title or something else. To achieve this, the syntactically simple option

renewcommandthesection{arabic{section}.}

works like a charm. The dot also appears in the table of contents, which is great. Next, I load the fancyhdr package (documentation). The aim is to obtain the identical format in fancyhdr‘s fancy header. By default (when renewcommandthesection{arabic{section}.} is in use), it gives two dots instead of the desired one dot, so 1.. My section title instead of 1. My section title. This is the only thing I would like to change.


After reading a little bit of the documentation, I guessed that

renewcommand{sectionmark}[1]{markright{thesection #1}}

would work. Compiling with Overleaf, this did not help. Compiling instead with my local (more updated) distribution the problem was solved.

  • Is this a reasonable/safe/canonical way to solve the problem?

("Safe" – likely to work in the future, not breaking something important in the background)

  • If not, what is a better/alternative solution? (Minimally invasive, syntactically simple solution is preferred)

MWE (uncomment for my attempted solution):

documentclass{book}

usepackage{fancyhdr}
usepackage{titlesec}
usepackage[colorlinks=true, linkcolor=blue, hypertexnames=false]{hyperref}

renewcommandthechapter{Roman{chapter}}
renewcommandthesection{arabic{section}.}
%renewcommand{sectionmark}[1]{markright{thesection #1}} <-- uncomment this for my attempt

begin{document}

    clearpagethispagestyle{empty}
    {hypersetup{linkcolor=black}tableofcontents}
    pagestyle{fancy}
    thispagestyle{empty}
    
    chapter{My chapter title}
        section{My chapter title}
            pagebreak
            Why two dots in fancy header here?

end{document}

Output of page 5 (Overleaf, commented or uncommented – no difference):

Output of page 5 (Overleaf

Output of page 5 (local, commented):

Output of page 5 (local, commented)

Output of page 5 (local, uncommented):

Output of page 5 (local, uncommented)

I am running pdflatex 3.141592653-2.6-1.40.22 (MikTeX 21.3), and every package in my local distrubution was updated on 28/04/2021.

3 Answers

Without titlesec you have two things you need to update - the sectional unit number in the section title (@seccntformat) and what is printed in the ToC (cftsecafternsum from tocloft):

enter image description here

documentclass{book}

usepackage{fancyhdr}
usepackage{tocloft}

renewcommandthechapter{Roman{chapter}}
makeatletter
% Add dot after sectional unit number (for section, subsection, ...)
renewcommand{@seccntformat}[1]{csname the#1endcsname.quad}
makeatother
renewcommandthesection{arabic{section}}% Remove chapter number from section number
renewcommand{cftsecaftersnum}{.}% Add dot after section number in ToC

pagestyle{fancy}

begin{document}

tableofcontents

chapter{My chapter title}
section{My chapter title}
clearpage
Why two dots in fancy header here? No moreldots

end{document}

The reason for not including the period in thesection is because it would otherwise be included in refs to a section, which might be odd, as they can occur mid-sentence.

Correct answer by Werner on July 31, 2021

UPDATED You can use the titlesec package that you already loaded, to add the dot to the section number instead of redefining thesection.

In a similar way after the subsection number.

I added some dummy text to check the headers generated by fancyhdr.

Strictly speaking, fancyhdr is not needed to solve the issue of the double dot. But it gives you a better control of the headers and footers.

I added part of Werner`s code to add, for sections and subsections, the dot in the TOC (last figure).

s

sub

t

% !TeX TS-program = pdflatex

documentclass{book}
usepackage{fancyhdr}

pagestyle{fancy}

usepackage[compact,explicit]{titlesec}

titleformat{section}{largebfseries}{}{0pt}{thesection .quad #1} % section number with dot
titleformat{subsection}{bfseries}{}{0pt}{thesubsection .quad #1} % subsection number with dot  

usepackage[colorlinks=true, linkcolor=blue, hypertexnames=false]{hyperref} 
renewcommandthechapter{Roman{chapter}}

% *********************************** TOC
usepackage{tocloft} % added to correct TOC%
renewcommand{cftsecaftersnum}{.}% Add dot after section number in ToC
renewcommand{cftsubsecaftersnum}{.}% Add dot after subsection number in ToC
% *********************************** TOC

usepackage{kantlipsum}  % dummy text
begin{document}
    
    {hypersetup{linkcolor=black}tableofcontents}  
    clearpagethispagestyle{empty}
    
    chapter{My chapter title}  
    11. kant[11-15]
    
    section{My section title (sec)}
    12. kant[12-16]
    
        subsection{My subsection title (subsec)}
    14. kant[14-18]
    
    newpage
    Why two dots in fancy header here?

end{document}

Answered by Simon Dispa on July 31, 2021

Since you load titlesec, you can use its titlelabel command:

documentclass{book}

usepackage{fancyhdr}
usepackage{titlesec}
usepackage[colorlinks=true, linkcolor=blue, hypertexnames=false]{hyperref}
titlelabel{thetitle.quad}
renewcommandthechapter{Roman{chapter}}

begin{document}

    clearpagethispagestyle{empty}
    {hypersetup{linkcolor=black}tableofcontents}
    pagestyle{fancy}
    thispagestyle{empty}

    chapter{My chapter title}
        section{My section title}
            pagebreak
            Why two dots in fancy header here?

end{document} 

enter image description here

Answered by Bernard on July 31, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP