1

I'm using fancyhdr, and following this answer I have redefined \sectionmark as:

\renewcommand{\sectionmark}[1]{\markboth{#1}{}}

so that

\fancyhead[RO,LE]{\leftmark}

gives the name of the current section in the header on the right for odd pages and left for even pages. This may sound strange, but I would additionally like the short name of the section to go on the other side of the header. So, for example, if I've declared

\section[short name]{long name}

then my pages would have headers with "short name" on one side and "long name" on the other. Can this be done? I assume that I would need to use \fancyhead[RE,LO]{\rightmark} having engineered things so that \rightmark produced the short name of the section. But I haven't had success with that. Also by providing a short name for \section[]{}, the short name is used in \leftmark instead.

(Why am I doing this? Each of these sections has a new name under a new naming convention, and an old name under an old naming convention. I am trying to cheat and use \section[]{} to declare both the new and old name, and have both appear in the header. There will be no table of contents in this document and I foresee no other use of short names.)

  • 1
    If there is no toc, why not simply use \section[shortname longname]{longname}? – Johannes_B Sep 26 '14 at 7:16
  • You can't use the optional argument like this. Also rightmark outputs the first mark, while leftmark the last mark so if there are two sections you would get confusing output if you use both. Better code the two names like this \section{\oldandnew{old name}{new name}}. You can then locally redefine the command \oldandnew so that it outputs either the one or the other argument. – Ulrike Fischer Sep 26 '14 at 8:17
1

It's ugly, but it works.

\documentclass{article}
\usepackage{everypage}
\usepackage{lipsum}

\newlength{\headeroffset}
\newcommand{\shortname}{}
\newcommand{\longname}{}

\newcommand{\myhead}[2]% #1 = short name, #2 = long name
{\def\shortname{#1}%
\def\longname{#2}%
\settodepth{\headeroffset}{{#1}{#2}}% distance from baseline to bottom
\global\headeroffset=\headeroffset}

\newcommand{\writeheader}{%
\begingroup% preserve global \headeroffset
\advance \headeroffset by -\topmargin% to top of header
\advance \headeroffset by -\headheight% to botom of header
\ifodd\value{page}\raisebox{\headeroffset}[0pt][0pt]{\hspace{\oddsidemargin}%
  \makebox[\textwidth][l]{{\shortname}\hfill{\longname}}%
  \hspace{-\textwidth}\hspace{-\oddsidemargin}}% return cursor to left
\else\raisebox{\headeroffset}[0pt][0pt]{\hspace{\evensidemargin}%
  \makebox[\textwidth][l]{{\longname}\hfill{\shortname}}%
  \hspace{-\textwidth}\hspace{-\evensidemargin}}% return cursor to left
\fi\endgroup}
\AddEverypageHook{\writeheader}

\begin{document}
\pagestyle{plain}
\myhead{Short name}{This is a long name}
\lipsum[1-12]
\end{document}

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.