1

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?

  • The default definition of \subsectionmark is “do nothing”. – egreg Jan 5 '16 at 21:23
  • Some more details are needed: do you always have a subsection starting after a section title? If not, what should the header display? – egreg Jan 5 '16 at 21:28
  • How would I go about appending something to the \rightmark then? I've read that only the first occurence of of it will be used. I updated the question to show how it's in the actual document. – michaeln Jan 5 '16 at 21:31
  • Edited the code and added what it should look like. – michaeln Jan 5 '16 at 21:33
  • The addition is quite unclear; you can typically have only the first subsection that starts in the current page in the header. Do you need chapter titles in the header or footer? – egreg Jan 5 '16 at 21:36
1

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

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.