5

I have set the headings to show the section number and name on the left side of the page, and the page number on the right.

This works fine for most section names, but one particular one is a little long, but not long enough (it seems): it just overlaps the page number, but not enough to make LaTeX wrap automatically. (I have a longer heading and LaTeX notices this and does the right thing.)

I changed the section name to

\section[really long section\\name]{really long section name}

which works in the header fine, but (obviously) also makes a newline in the TOC. The section name fits the TOC fine, so:

Is it possible to force a newline in the header, but not the TOC entry?

This is how my preamble looks:

\usepackage{fancyhdr}
\pagestyle{fancy}

% with this we ensure that the Chapter and Section
% headings are in lowercase
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection\ #1}{}}

\fancyhf{}                              % delete the current header and footer
\fancyhead[LO]{\textbf{\rightmark}}
\fancyhead[RO]{\textbf{\thepage}}

\renewcommand{\headrulewidth}{0pt}

\fancypagestyle{plain}{
\fancyhead{}                            % get rid of the headers on plain pages
\renewcommand{\headrulewidth}{0pt}      % and the line
\fancyfoot[R]{\textbf{\thepage}}
}
5
  • You can use \markboth{}{}. You will find a lot of examples at tex stackexchange. Mar 21, 2012 at 19:24
  • multi-line running heads aren't usually a good idea. \markboth with a shorter form for the running head would be better. Mar 21, 2012 at 19:40
  • @MarcoDaniel thanks. I searched round (and elsewhere), and experimented, but can't get what I want. I've edited my question to show the preamble I'm using (book class, oneside). Mar 21, 2012 at 20:15
  • @barbarabeeton could you explain in more detail what you mean please? I tried changing \markright to \markboth when renewing the \sectionmark command, to no avail. Mar 22, 2012 at 12:20
  • Headers on left pages are easily fixable. \fancyhead[LO]{\textbf{\rightmark} \phantom{aaa}}. Unfortunately, I haven't been able to use the same trick to fix right pages.
    – Rufo
    Jan 6, 2017 at 23:19

2 Answers 2

2

Instead of using \\ create a new macro with, say, \DeclareRobustCommand\NL{ } and redefine it locally in the headers with \DeclareRobustCommand\NL{\\}. Then write:

\section[really long section\NL name]{really long section name}
1
  • this seems to have the same (unwanted) effect as just putting \\. The TOC entry gets the new line too.
    – themenace
    Jan 11 at 22:29
1

Put your fancy head in LO inside a parbox as shown below:

\fancyhf{}                              % delete the current header and footer
\fancyhead[LO]{\parbox{0.7\textwidth}{\textbf{\rightmark}}}% <----\parbox here.
\fancyhead[RO]{\textbf{\thepage}}

And do not use \\ in short title: \section[really long section\\name]{really long section name}

EDIT: You may use

\fancyhf{}                              % delete the current header and footer
\fancyhead[LO]{\parbox[t]{0.9\textwidth}{\textbf{\rightmark}}} %<--- use [t] for parbox.
\fancyhead[RO]{\parbox[t]{0.02\textwidth}{\textbf{\thepage}}} %<--- better use [t]  here also for parbox to ensure that alignment.

Also to adjust the head separation you can use

\setlength{\headsep}{0.25in} % <---change the value accordingly

enter image description here

2
  • Thanks. That did work (also for 0.95 * textwidth), but unfortunately it moves all headings slightly (perhaps 3/8 em or something) down, relative to the page number. Mar 22, 2012 at 12:18
  • @ChrisPoole, I have edited the answer accordingly.
    – user11232
    Mar 22, 2012 at 13:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .