1

I have a question about the header of a latex document (article class). I want to create a header like

Section 1. {section name}

on the left of my header line, but when I use \leftmark command, it returns error. I tried to use \rightmark command but the header just have the title of the subsection. Could some one help me to solve this problem? Thank you very much!

1 Answer 1

1

I don't know the error message, so I cannot say, why \leftmark does not work in your code. As usual a minimal working example would have been useful, to see what you've tried and why it does not work.

Usually it does work:

\documentclass{article}

\usepackage{fancyhdr}
\fancyhead[L]{\bfseries\leftmark}
\fancyhead[R]{\thepage}
\fancyfoot[C]{}
\renewcommand*{\headrulewidth}{0pt}
\renewcommand*{\sectionmark}[1]{\markboth{Section~\thesection.~#1}{}}
\pagestyle{fancy}

\usepackage{mwe}

\begin{document}
\blinddocument
\end{document}

But as an alternative, using scrlayer-scrpage you don't need it in this case:

\documentclass{article}

\usepackage[automark,markcase=used]{scrlayer-scrpage}
\renewcommand*{\sectionmarkformat}{Section~\thesection.~}% Format of the number
\setkomafont{pageheadfoot}{\bfseries}% Don't use slant, but bold

\usepackage{mwe}

\begin{document}
\blinddocument
\end{document}

In other cases, you could simply use \headmark with scrlayer-scrpage.

Both examples with the same result:

result using \leftmark

With fancyhdr also \rightmark could be correct:

\documentclass{article}

\usepackage{fancyhdr}
\fancyhead[L]{\bfseries\rightmark}
\fancyhead[R]{\thepage}
\fancyfoot[C]{}
\renewcommand*{\headrulewidth}{0pt}
\renewcommand*{\sectionmark}[1]{\markright{Section~\thesection.~#1}}
\pagestyle{fancy}

\usepackage{mwe}

\begin{document}
\blinddocument
\end{document}

Whether \leftmark or \rightmark should be used, depends on the expected result with more than one \section on the same page.

1
  • Your codes work for me. Thank you so much!
    – Tom Howard
    Commented Nov 20, 2022 at 12:02

You must log in to answer this question.

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