[Tex/LaTex] \rightmark not displaying subsection

fancyhdrheader-footersectioning

Current situation

My document (report) consists of chapters, sections and subsections. Now I want to display the section name and the subsection name on the left side of the footer. However if I use \rightmark I only get the section name, not the subsection one too.

A minimal example

I tried to reduce the problem to this minimal example which is basically the code from my actual document:

\documentclass[a4paper,12pt]{report}

% <header_footer>
    \usepackage{fancyhdr}
    \pagestyle{fancy}
    \fancyhf{}
    \fancyhead[L]{\rightmark}
% </header_footer>

% ...
% a lot of other stuff here
% ...

% <toc>
    \setcounter{secnumdepth}{0}

    \newcommand\invisiblesection[1]{%
        \refstepcounter{section}%
        \addcontentsline{toc}{section}{#1}%
        \sectionmark{#1}}

    \newcommand\invisiblesubsection[1]{%
        \refstepcounter{subsection}%
        \addcontentsline{toc}{subsection}{#1}%
        \subsectionmark{#1}}
% </toc>

\begin{document}
    \tableofcontents
    \newpage

    \chapter{a chapter}
    \newpage

    \invisiblesection{section}
    \invisiblesubsection{subsection}
    content to ensure the page is there
    \newpage

    \invisiblesection{another section}
    \invisiblesubsection{subsection 1}
    content to ensure the page is there 
    % the header should now display "another section - subsection 1"
    \newpage

    \invisiblesubsection{subsection 2}
    content to ensure the page is there 
    % the header should now display "another section - subsection 2"
    \newpage

    \invisiblesubsection{subsection 3}
    content to ensure the page is there 
    % the header should now display "another section - subsection 3"
\end{document}

What it looks like

image displaying what it looks like

What it should look like

image displaying what it should look like

What I tried so far

  • overriding both marks – didn't work as the \leftmark was defined as the chapter's name
  • getting the current section name with nameref – didn't work as it was fetching the chapter's name
  • \defining custom variables each time a \invisiblesection is created – didn't work either

Question

I suppose there's just one dead simple flag which I forgot but google didn't tell me. Does anybody have a solution to display both the section and subsection name in a header/footer?

Best Answer

The \subsectionmark command is defined to do nothing, so you need to change its definition if you want it issues a mark.

Similarly, you have to change \sectionmark so it doesn't issue a mark.

\documentclass[a4paper,12pt]{report}

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[L]{\rightmark}

\setlength{\headheight}{14.5pt}
\setcounter{secnumdepth}{0}

\newcommand\invisiblesection[1]{%
  \refstepcounter{section}%
  \addcontentsline{toc}{section}{#1}%
  \sectionmark{#1}%
}

\newcommand\invisiblesubsection[1]{%
  \refstepcounter{subsection}%
  \addcontentsline{toc}{subsection}{#1}%
  \subsectionmark{#1}%
}

\renewcommand{\sectionmark}[1]{%
  \gdef\currsection{#1}%
}
\renewcommand{\subsectionmark}[1]{%
  \markright{\currsection\ \textbullet\ #1}%
}

\begin{document}

\tableofcontents
\newpage

\invisiblesection{section}
\invisiblesubsection{subsection}
content to ensure the page is there
\newpage

\invisiblesection{another section}
\invisiblesubsection{subsection 1}
content to ensure the page is there 
% the header should now display "another section - subsection 1"
\newpage

\invisiblesubsection{subsection 2}
content to ensure the page is there 
% the header should now display "another section - subsection 2"
\newpage

\invisiblesubsection{subsection 3}
content to ensure the page is there 
% the header should now display "another section - subsection 3"

\end{document}

Here's the top of page 4

enter image description here

Related Question