Page Layoutfancyhdr | Problem with Sections in Header

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
Post Reply
fededevita
Posts: 5
Joined: Tue Feb 14, 2012 6:43 pm

fancyhdr | Problem with Sections in Header

Post by fededevita »

Hi I am using fancyhdr for a book. I am using both \leftmark, which is working fine putting the chapter names on the header, and \rightmark, which is giving me a problem: it does put the section names in, as it should, but it adds a dot to the left. I am quite lost.

Recommended reading 2024:

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

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

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

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

fancyhdr | Problem with Sections in Header

Post by localghost »

Unfortunately you keep back important information that would allow others to help you. Descriptions are nice to have, but only the code that reproduces the undesired behaviour is really helpful to get closer to a solution. So it's necessary that you provide the critical parts within a small sample document that is compilable out of the box.


Best regards and welcome to the board
Thorsten
fededevita
Posts: 5
Joined: Tue Feb 14, 2012 6:43 pm

fancyhdr | Problem with Sections in Header

Post by fededevita »

Good point, apologies.

Here is the code. I think the problem lies in the re-definition of the section, but I have no idea how to solve it. In the header on the last page, it doesn't say "Short title of sec" but puts a dot in front of it so it reads ". Short title of sec". Any help is much appreciated

Code: Select all

\documentclass{book}

\usepackage{fancyhdr}

\pagestyle{fancy}
\fancyhead[RO,LE]{\leftmark}
\fancyhead[RE,LO]{\rightmark}
\fancyfoot[LE,RO]{\thepage}
\fancyfoot[RE,LO]{}
\cfoot{}

\renewcommand\thesection{\hspace{-1em}}

\begin{document}

\begin{titlepage}
\Huge {\bf The Title}
\end{titlepage}

\setcounter{tocdepth}{2}
\tableofcontents

\chapter{One}

\section[Short title of sec]{Long title of section}

And here there is some text, which should be long enough to fill an entire page, hence I repeat it several times. And here there is some text, which should be long enough to fill an entire page, hence I repeat it several times.And here there is some text, which should be long enough to fill an entire page, hence I repeat it several times.And here there is some text, which should be long enough to fill an entire page, hence I repeat it several times.And here there is some text, which should be long enough to fill an entire page, hence I repeat it several times.And here there is some text, which should be long enough to fill an entire page, hence I repeat it several times.And here there is some text, which should be long enough to fill an entire page, hence I repeat it several times.And here there is some text, which should be long enough to fill an entire page, hence I repeat it several times.And here there is some text, which should be long enough to fill an entire page, hence I repeat it several times.And here there is some text, which should be long enough to fill an entire page, hence I repeat it several times.And here there is some text, which should be long enough to fill an entire page, hence I repeat it several times.And here there is some text, which should be long enough to fill an entire page, hence I repeat it several times.And here there is some text, which should be long enough to fill an entire page, hence I repeat it several times.And here there is some text, which should be long enough to fill an entire page, hence I repeat it several times.And here there is some text, which should be long enough to fill an entire page, hence I repeat it several times.And here there is some text, which should be long enough to fill an entire page, hence I repeat it several times.

\end{document}
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

fancyhdr | Problem with Sections in Header

Post by localghost »

What do you hope to achieve by this redefinition of the section counter?

Code: Select all

\renewcommand{\thesection}{\hspace{-1em}}
This is not comprehensible to me.
fededevita
Posts: 5
Joined: Tue Feb 14, 2012 6:43 pm

Re: fancyhdr | Problem with Sections in Header

Post by fededevita »

This is just redefining the section command so that it doesn't come up with a section number (without this line it would come up with "1.1 name of section", this way it only shows the section name). The negative hspace is simply removing the indent
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Re: fancyhdr | Problem with Sections in Header

Post by localghost »

So you want to turn off numbering of section headings (and probably of all the lower heading levels)?
fededevita
Posts: 5
Joined: Tue Feb 14, 2012 6:43 pm

Re: fancyhdr | Problem with Sections in Header

Post by fededevita »

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

fancyhdr | Problem with Sections in Header

Post by localghost »

Then set the counter secnumdepth accordingly.

Code: Select all

\documentclass{book}
\usepackage{fancyhdr}
\usepackage{lipsum}

\fancyhf{}
\fancyhead[RO,LE]{\leftmark}
\fancyhead[RE,LO]{\rightmark}
\fancyfoot[LE,RO]{\thepage}
\pagestyle{fancy}

\setcounter{secnumdepth}{0}  % default value 2 for »book« class
\setcounter{tocdepth}{2}

\begin{document}
  \tableofcontents

  \chapter{One}
    \section[Short title of Section]{Long title of Section}

    \lipsum
\end{document}
fededevita
Posts: 5
Joined: Tue Feb 14, 2012 6:43 pm

Re: fancyhdr | Problem with Sections in Header

Post by fededevita »

That works beautifully, thanks very much.
Post Reply