[Tex/LaTex] Section and subsection name in headings

fancyhdrsectioningsections-paragraphs

I'd like to make the headings of my article look like this:

<Section name>                        <Subsection number> <Subsection name> | <Page number>

and I wrote this code:

\pagestyle{fancy}

\fancyhf{}

\lhead{\fancyplain{} {\sectionmark}}
\rhead{\fancyplain{} {\subsectionmark} $|$ \thepage}
\renewcommand{\headrulewidth} {0pt}
\renewcommand{\footrulewidth} {0pt}

But it just doesn't show the names of sections and subsections.

This is what I get:

                                                                            | 2

How can I fix it?

Best Answer

For capturing the section unit titles and have fancyhdr-style headings, Javier Bezos' titleps package provides an easy alternative:

enter image description here

\documentclass{article}
\usepackage{titleps,lipsum}% http://ctan.org/pkg/{titleps,lipsum}
\newpagestyle{main}{%
  \setheadrule{0pt}%
  \sethead{\sectiontitle}                                 % header left
    {}                                                    % header center
    {\thesubsection~\subsectiontitle\quad$|$\quad\thepage}% header right
}
\pagestyle{main}
\begin{document}
\section{A section}\lipsum[1-2]
\subsection{A subsection}\lipsum[3-4]
\subsection{Another subsection}\lipsum[5-6]
\subsection{A subsection}\lipsum[7-8]
\subsection{Another subsection}\lipsum[9-10]
\section{Another section}\lipsum[1-2]
\subsection{A subsection}\lipsum[3-4]
\subsection{Another subsection}\lipsum[5-6]
\subsection{A subsection}\lipsum[7-8]
\subsection{Another subsection}\lipsum[9-10]
\end{document}

Within twoside mode, you could use

\sethead[<even-left>][<even-center>][<even-right>]
  {<odd-left>}{hodd-center>}{<odd-right>}
\setfoot[<even-left>][<even-center>][<even-right>]
  {<odd-left>}{<odd-center>}{<odd-right>}

for setting different headers/footers on even/odd pages.

You have to decide which \subsection to include in your headers. titleps uses the last-defined sectional heading. However, see the titleps documentation for more options (section 4. Marks, p 4 onwards).

Related Question