1

The usual way of getting section names in fancyhdr headers is by doing: \renewcommand{\sectionmark}[1]{\markright{\thesection.\ #1}}

However, this doesn't get subsections, subsubsections, paragraphs, nor subparagraphs. I'd like the header to show the name of the last section created up to the current page, no matter if it's a section or a subparagraph.

This of course seems to match \currenttitle from the titleref package.

But unfortunately this doesn't work for getting \currenttitle in the header: \renewcommand{\currenttitle}[1]{\markright{\thesection.\ #1}} (yes, I know \thesection is wrong, but I'm first trying to get the current title name, fixing the number will come later).

Are there any workarounds for getting the current title (and its number) in a header by fancyhdr?

Example which I'd like to modify so that the header displays the last section/subsection/subsubsection/paragraph/subparagraph in the page:

\documentclass[11pt,a4paper]{article}
\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\sectionmark}[1]{\markright{\thesection\ #1}}
\fancyhf{}
\fancyhead[LE,RO]{\bfseries\thepage}
\fancyhead[LO]{\bfseries\rightmark}
\fancyhead[RE]{\bfseries\leftmark}
\renewcommand{\headrulewidth}{0.5pt}
\renewcommand{\footrulewidth}{0pt}
\addtolength{\headheight}{14pt}
\fancypagestyle{plain}{
   \fancyhead{}
   \renewcommand{\headrulewidth}{0pt}
}
\setcounter{secnumdepth}{5}
\setcounter{tocdepth}{5}
\usepackage{blindtext}
\begin{document}
\blinddocument
\end{document}
2
  • Can you post an example? titleref doesn't use the titles of subsection etc.
    – user31729
    Oct 3, 2015 at 23:42
  • Example added to the post. It shows the usual fancyhdr behavior. I wish to change it so that it honors all hierarchical levels of sections. Oct 4, 2015 at 0:11

1 Answer 1

1

Something like this?

many levels

\documentclass[11pt,a4paper]{article}
\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\sectionmark}[1]{\markright{\thesection\ #1}}
\renewcommand{\subsectionmark}[1]{\markright{\thesubsection\ #1}}
\renewcommand{\subsubsectionmark}[1]{\markright{\thesubsubsection\ #1}}
\renewcommand{\paragraphmark}[1]{\markright{\theparagraph\ #1}}
\renewcommand{\subparagraphmark}[1]{\markright{\thesubparagraph\ #1}}
\fancyhf{}
\fancyhead[LE,RO]{\bfseries\thepage}
\fancyhead[LO]{\bfseries\rightmark}
\fancyhead[RE]{\bfseries\leftmark}
\renewcommand{\headrulewidth}{0.5pt}
\renewcommand{\footrulewidth}{0pt}
\addtolength{\headheight}{14pt}
\fancypagestyle{plain}{%
   \fancyhead{}%
   \renewcommand{\headrulewidth}{0pt}%
}
\setcounter{secnumdepth}{5}
\setcounter{tocdepth}{5}
\usepackage{kantlipsum}
\begin{document}
  \section{section}
  \kant[1-4]
  \subsection{subsection}
  \kant[5-7]
  \subsubsection{subsubsection}
  \kant[8-9]
  \paragraph{paragraph}
  \kant[10-12]
  \subparagraph{subparagraph}
  \kant[13]
  \section{another section}
  \kant[14-15]
\end{document}

However, you should use geometry to set the page dimensions:

adjusted page layout

\documentclass[11pt,a4paper]{article}
\usepackage{fancyhdr}
\usepackage[headheight=14pt,vcentering]{geometry}
\pagestyle{fancy}
\renewcommand{\sectionmark}[1]{\markright{\thesection\ #1}}
\renewcommand{\subsectionmark}[1]{\markright{\thesubsection\ #1}}
\renewcommand{\subsubsectionmark}[1]{\markright{\thesubsubsection\ #1}}
\renewcommand{\paragraphmark}[1]{\markright{\theparagraph\ #1}}
\renewcommand{\subparagraphmark}[1]{\markright{\thesubparagraph\ #1}}
\fancyhf{}
\fancyhead[LE,RO]{\bfseries\thepage}
\fancyhead[LO]{\bfseries\rightmark}
\fancyhead[RE]{\bfseries\leftmark}
\renewcommand{\headrulewidth}{0.5pt}
\fancypagestyle{plain}{%
   \fancyhead{}%
   \renewcommand{\headrulewidth}{0pt}%
}
\setcounter{secnumdepth}{5}
\setcounter{tocdepth}{5}
\usepackage{kantlipsum}
\begin{document}
  \section{section}
  \kant[1-4]
  \subsection{subsection}
  \kant[5-7]
  \subsubsection{subsubsection}
  \kant[8-10]
  \paragraph{paragraph}
  \kant[11-14]
  \subparagraph{subparagraph}
  \kant[15]
  \section{another section}
  \kant[16-18]
\end{document}

Note that fancyhdr is warning you that the use of E is useless in a single-sided document.

2
  • Thanks a lot!! Yes, I'm aware of the single-sided warning, and, BTW, when changing it to two-sided, the line \fancyhead[RE]{\bfseries\leftmark}must be changed to \fancyhead[RE]{\bfseries\rightmark} if I want this on both even and odd pages. Also, one question: Is it correct the result from moving the headheight setting to the geometry package? Looking at the second result, I see the white space at the bottom unpleasant if compared to the top of the page. However, on my original code, the top and the bottom are more balanced, and I tend to believe they're more correct there. Oct 4, 2015 at 11:24
  • @user2811963 Yes, it is correct. But geometry adjusts other things. If you prefer a more even top/bottom balance, you can add vcentering. Please see edited answer above for an example modified in this way. And, yes, with twoside you'd need that. I left it from your original example because I wasn't sure what exactly you wanted.
    – cfr
    Oct 4, 2015 at 13:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .