1

So many questions around dealing with fancyhdr, but none works for me...

I have an article where I want my document title and ONLY THE SECTION NAME in the header, not the subsection, not the section number, nothing but the name. For this, I created a custom pagestyle (have different pagestyles in my document), see MWE below. But what ever I try, I still get the subsection in the title, even including its number. Why?

\documentclass{article}
    \newcommand{\maintitle}{MAIN TITLE}
    \newcommand{\docname}{My Document}
% tabular package
\usepackage{tabularx}
% header / footer
\usepackage{fancyhdr}
    \renewcommand{\sectionmark}[1]{\markright{#1}}
% define own pagestyle
\fancypagestyle{complete}{%
    \fancyhf{}%
    \lfoot{%
        \begin{tabularx}{\textwidth}{X X}
            \maintitle & Page~\thepage
        \end{tabularx}
    }
    \rhead{\docname~-~\rightmark}
}
% Let the madness begin...
\begin{document}
    \pagestyle{complete}
    \section{First Section}
        Blablubb ...
        \newpage
        \subsection{First Subsection}
            dumdidum ...
    \section{Second Section}
        Blablablubb ...
        \subsection{Second Subsection}
            dumdidumdidum ...
\end{document}
1

\subsection by default sets a right mark, too. If the subsection should never be in page header, you can redefine \subsectionmark to do nothing:

\renewcommand\subsectionmark[1]{}

Example:

\documentclass{article}
    \newcommand{\maintitle}{MAIN TITLE}
    \newcommand{\docname}{My Document}
% tabular package
\usepackage{tabularx}
% header / footer
\usepackage{fancyhdr}
    \renewcommand{\sectionmark}[1]{\markright{#1}}
    \renewcommand{\subsectionmark}[1]{}% <- added
% define own pagestyle
\fancypagestyle{complete}{%
    \fancyhf{}%
    \fancyfoot[L]{%
        \begin{tabularx}{\textwidth}{X X}
            \maintitle & Page~\thepage
        \end{tabularx}
    }
    \fancyhead[R]{\docname~-~\rightmark}
}
% Let the madness begin...
\begin{document}
    \pagestyle{complete}
    \section{First Section}
        Blablubb ...
        \newpage
        \subsection{First Subsection}
            dumdidum ...
    \section{Second Section}
        Blablablubb ...
        \subsection{Second Subsection}
            dumdidumdidum ...
\end{document}

Or you could use the left mark for the section title:

\renewcommand{\sectionmark}[1]{\markboth{#1}{}}% <- changed
\fancypagestyle{complete}{%
    ...
    \fancyhead[R]{\docname~-~\leftmark}% <- changed
}

Example:

\documentclass{article}
    \newcommand{\maintitle}{MAIN TITLE}
    \newcommand{\docname}{My Document}
% tabular package
\usepackage{tabularx}
% header / footer
\usepackage{fancyhdr}
    \renewcommand{\sectionmark}[1]{\markboth{#1}{}}
% define own pagestyle
\fancypagestyle{complete}{%
    \fancyhf{}%
    \fancyfoot[L]{%
        \begin{tabularx}{\textwidth}{X X}
            \maintitle & Page~\thepage
        \end{tabularx}
    }
    \fancyhead[R]{\docname~-~\leftmark}
}
% Let the madness begin...
\begin{document}
    \pagestyle{complete}
    \section{First Section}
        Blablubb ...
        \newpage
        \subsection{First Subsection}
            dumdidum ...
    \section{Second Section}
        Blablablubb ...
        \subsection{Second Subsection}
            dumdidumdidum ...
\end{document}
2
  • Both work great, thanks! Is there a difference between using \rhead{\docname~-~\leftmark} and \fancyhead[R]{\docname~-~\leftmark}?
    – s6hebern
    Feb 11 '21 at 14:58
  • 1
    \rhead is a deprecated command. \fancyhead[R] is the modern version, and it is easier to use if you would want to make different headers on odd and even pages. Feb 11 '21 at 16:38

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.