# How to put page numbers flush with the upper right corner when the section title is long?

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}
\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}
-

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.

-
Thanks! Didn't think of this... it is much simpler. –  ceiling cat Aug 3 '11 at 23:32

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}
\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}
-
if I do this, the line underneath the section title would clip the bottom of the second line... –  ceiling cat Aug 3 '11 at 23:35
@ceiling cat: fixed. –  Gonzalo Medina Aug 4 '11 at 0:22
that looks good, thanks! –  ceiling cat Aug 4 '11 at 3:27