0

I worked a bit with commands the last few weeks, but only to format little things or to format certain passages in my document uniformly.

Now i wanted to know if there is the option to include the \rhead command of fancyhdr into the \section command itself. With that i want to achive that i don' have to type both commands at the beginning of each new chapter.

My current idea doesn't work due to a "TeX capacity exceeded" - Error.

\renewcommand \section[1] {
    \rhead{#1}
    \section{#1}
}

So maybe there is a simpler solution that i haven't found yet. Thanks in advance

1
  • 2
    You have to set up fancyhdr correctly and there will absolutely no need to connect both. It is the standard.
    – Johannes_B
    Jul 8, 2019 at 9:26

1 Answer 1

1

The default fancy pagestyle of fancyhdr already automatically puts the section title in the header. MWE:

\documentclass{article}
\usepackage{fancyhdr}
\pagestyle{fancy}
\usepackage{lipsum}
\begin{document}
\section{First section}
\lipsum[1-10]
\section{Second section}
\lipsum[1-10]
\end{document}

enter image description here

If you don't want the number to be printed, you can redefine the \sectionmark command to only a left mark (which is printed on the top right of the page) containing the section title, and an empty rightmark (on the left of the page), which is more or less what you tried with the \rhead command if I understand correctly.

This redefinition is done in the preamble (before \begin{document}) and affects the entire document, so you have to set it only once and you don't need to repeat it for every section. MWE:

\documentclass{article}
\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\sectionmark}[1]{\markboth{#1}{}}
\usepackage{lipsum}
\begin{document}
\section{First section}
\lipsum[1-10]
\section{Second section}
\lipsum[1-10]
\end{document}

Result:

enter image description here

If you don't like the italic font then you can set the \rhead, again globally in the preamble, to contain the \leftmark (which defaults to the section title in most documentclasses without chapters). You can format it to be upper case, smallcaps, bold, or any other style you prefer, or just plain roman as in the following MWE:

\documentclass{article}
\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\sectionmark}[1]{\markboth{#1}{}}
\rhead{\leftmark}
\usepackage{lipsum}
\begin{document}
\section{First section}
\lipsum[1-10]
\section{Second section}
\lipsum[1-10]
\end{document}

Result:

enter image description here

You must log in to answer this question.

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