6

I have a header, prepared using fancyhdr. I would like the first row of the left header to be right-aligned with the second row of the left header. In the following MWE I would like "My City" to be right aligned with "University of somewhere" (so that "City" is just above "somewhere"). How do I achieve that?

\documentclass{article}
\usepackage{fancyhdr}
\usepackage{fancyheadings}

\pagestyle{fancy}
\rhead{\textsc{Tutorial\\The periodic table}}
\lhead{\textsc{My city\\University of somewhere}}
\cfoot{\thepage}

\begin{document}
    hello
\end{document}
4

You can box the contents of the widest element and use this to width-adjust the shorter one:

enter image description here

\documentclass{article}
\usepackage{fancyhdr}% http://ctan.org/pkg/fancyhdr
\newsavebox{\myheadbox}% Heading storage box

\pagestyle{fancy}
\rhead{\textsc{Tutorial\\The periodic table}}
\lhead{\textsc{\savebox{\myheadbox}{University of somewhere}\makebox[\wd\myheadbox][r]{My city}\\\usebox{\myheadbox}}}
\cfoot{\thepage}

\begin{document}
    hello
\end{document}​

\myheadbox contains the "University of somewhere" (in the appropriate font, \scshape). Then, "My city" is right-aligned in a \makebox of width \wd\myheadbox (width of \myheadbox).

8

Don't load fancyheadings, which is the obsolete version of fancyhdr.

The easiest way is to use tabular which will take care of the alignment:

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{} % clear all fields
\fancyhead[R]{%
  \scshape
  \begin{tabular}[t]{@{}r@{}}
  Tutorial\\The periodic table
  \end{tabular}}
\fancyhead[L]{%
  \scshape
  \begin{tabular}[t]{@{}r@{}}
  My city\\University of somewhere
  \end{tabular}}
\fancyfoot[C]{\thepage}

Set the \headheight to the value that fancyhdr will show you in the .log file. For a 10pt document it says to do

\setlength{\headheight}{24pt}

enter image description here

5

You may typeset the left header \raggedleft inside a minipage with a width of the widest element (calculated with the help of \widthof, which is provided by the calc package).

\documentclass{article}
\usepackage{fancyhdr}
\usepackage{calc}

\pagestyle{fancy}
\rhead{\textsc{Tutorial\\The periodic table}}
\lhead{%
  \begin{minipage}[b]{\widthof{\textsc{University of somewhere}}}
  \raggedleft
  \textsc{My city\\University of somewhere}%
  \end{minipage}
}
\cfoot{\thepage}

\begin{document}
    hello
\end{document}

EDIT: As pointed out by egreg in chat, one may replace the minipage with a varwidth environment "whose resulting width is the natural width of its contents" (package manual).

\documentclass{article}
\usepackage{fancyhdr}
\usepackage{varwidth}

\pagestyle{fancy}
\rhead{\textsc{Tutorial\\The periodic table}}
\lhead{%
  \begin{varwidth}[b]{10cm}
  \raggedleft
  \textsc{My city\\University of somewhere}%
  \end{varwidth}
}
\cfoot{\thepage}

\begin{document}
    hello
\end{document}

Output for both examples:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.