2

I am writing a paper using an sty which puts the section title on top of each page together with the page number at the corner (using fancyhdr). The problem is that if the section title is too long and has to be split into 2 lines, the page number get aligned with the second line of the section title instead of the first. Please refer to the MWE below. How can I fix it so that the page number is always flush with the upper right corner? Thank you.

Bonus question: is it the right thing to do to have \setlength{\headheight}{27.16pt} in order to suppress the fancyhdr warning (when the section title is too long)? More thanks!

\documentclass[11pt]{article}

\usepackage{fancyhdr}
\setlength{\headheight}{27.16pt} % sometimes section title takes 2 lines
\pagestyle{fancyplain}
\lhead[\fancyplain{\thepage}{\thepage}]{\fancyplain{}{\scshape\rightmark}}
\rhead[\fancyplain{}{\scshape\leftmark}]{\fancyplain{\thepage}{\thepage}}
\chead{}
\cfoot{}
\lfoot{}
\rfoot{}

\def\chaptermark#1{%
    \markboth{\@chapapp\ \thechapter.~~  #1}{}}
\def\sectionmark#1{%
    \markright{Section \thesection.~~  #1}}

\begin{document}

\section{An unnecessarily long and wordy title for some stuff}

Words here

\end{document}

2 Answers 2

3

One solution would be to use a shorter title for the section. If this is not possible, a shorter (alternative) title could be used in conjunction with the regular long-length title:

\section[Short title for section]{An unnecessarily long and wordy title for some stuff}

This way you don't have to worry about a section title spreading over 2 (or more) lines by just supplying an adequately-descriptive, single-line alternative title. The added bonus is that you don't have to fiddle with \headheight.

1
  • Thanks! Didn't think of this... it is much simpler. Commented Aug 3, 2011 at 23:32
3

I agree with Werner's solution. However, if, for some reason, you need to use the long title in the heading, then you could use a \parbox in the definition of \sectionmark (you could reduce .97\linewidth to prevent ambiguity with the page number and the section title):

\documentclass[11pt]{article}

\usepackage{fancyhdr}
\setlength{\headheight}{27.16pt} % sometimes section title takes 2 lines
\pagestyle{fancyplain}
\lhead[\fancyplain{\thepage}{\thepage}]{\fancyplain{}{\scshape\rightmark}}
\rhead[\fancyplain{}{\scshape\leftmark}]{\fancyplain{\thepage}{\thepage}}
\chead{}
\cfoot{}
\lfoot{}
\rfoot{}

\def\chaptermark#1{%
    \markboth{\@chapapp\ \thechapter.~~  #1}{}}
\def\sectionmark#1{%
    \markright{\protect\parbox[t]{.97\linewidth}{\protect\raggedright Section \thesection.~~  #1\vskip0.2em}}}

\begin{document}

\section{An unnecessarily long and wordy title for some stuff}

Words here

\end{document}
2
  • if I do this, the line underneath the section title would clip the bottom of the second line... Commented Aug 3, 2011 at 23:35
  • @ceiling cat: fixed. Commented Aug 4, 2011 at 0:22

You must log in to answer this question.

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