# \section and \subsection alternating in the header

Right now, I'm trying to finalize the layout of my diploma thesis - unfortunately I cannot figure out how to achieve the heading I want. I'm using the amsart document class and try to achieve the following by using the fancyhdr package:

• On even pages I want for the header: on the left, pagenumber on the right
• On odd pages: pagenumber on the left, on the right

Here is the relevant LaTeX code:

\documentclass[twoside,12pt]{amsart}
\usepackage{amssymb,amsthm,array,arydshln}
\usepackage[toc,page]{appendix}
\usepackage{multicol}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tikz}
\usepackage{graphicx}
\usepackage{hyperref}
\usepackage{fancyhdr}
\usepackage{geometry}
\geometry{a4paper}

\begin{document}
\pagestyle{fancyplain}
\fancyhf{}
\title{test}
\maketitle
\end{document}


I achieved two different outputs, depending on what I put in

\lhead[\thepage]{\leftmark}


\leftmark as well as \rightmark will display the title instead of the title of the (sub)section; and so far, using anything else results in the display of nothing

I read quite a few articles, but I am still totally lost, because none of them was written for my usage case, i.e. the amsart document class

I appreciate any help and will provide further information if needed

-

I removed all the unnecessary packages for the task at hand; just use the settings for fancyhdr in your document after setting the page geometry.

Remember also that hyperref should be loaded last, with your choice of packages (very few packages should be loaded after hyperref).

One has to patch the \@sect command provided by amsart, because this command does not issue the needed marks.

\documentclass[twoside,12pt]{amsart}
\usepackage{geometry}

\usepackage{fancyhdr}
\pagestyle{fancy} % choose the "fancy" pagestyle
\fancyhf{}        % clear all headers and footers

%%% This code should go after hyperref, if loaded
\usepackage{xpatch}
\makeatletter
\xapptocmd{\@sect}{\csname #1mark\endcsname{#7}}{}{}
\makeatother

\usepackage{kantlipsum} % just for adding mock text

\begin{document}

\title{test}
\maketitle

\section{Test section}
\subsection{Test subsection}
\kant[1-12]

\end{document}

-
@egreg first, thanks for the tip concerning hyperref. second, i compiled your code and the outcome is the exact same, i.e. in the header i get the title of the document instead of the title of the section –  user24719 Jan 22 '13 at 13:24
thanks, this seems to go in the right direction, however, there is one big and one minor problem. 1. the big one: if i have something like \section{test 1} <text> \newpage \section{test 2} <text>, then the heading on the first page of the second section will be the title of the first section - allthough the numbering is okay. after the first page, everything is fine. 2. any chance of doing something similar to also get the subsection title into the header (of every second page)? –  user24719 Jan 22 '13 at 14:12
@user24719 The problem of sections at the start of a page seems quite hard to solve. :( amsart makes a substantial change in the usual macros for section titles and marks. My best advice is not to use it if you want changing headers. –  egreg Jan 22 '13 at 14:49
@egreg -- amsart was written to conform to long-standing ams journal specs, which are different from what the basic latex article class provides, so it's a bit unfair to say that it "doesn't issue the correct marks". unfortunately, this has the effect that fancyhdr isn't fully compatible with amsart. i will add a request to the ams list to consider compatibility, but can't promise anything. –  barbara beeton Jan 22 '13 at 15:04
@barbarabeeton I changed "correct" into "needed". I meant "correct for putting what the OP wanted in the header", not that amsart does the wrong thing. –  egreg Jan 22 '13 at 15:07