1

So I'm working on a book, my document type is article and I used fancyhdrto customize my header.

  1. I want the section name in the right of the header on even page numbers.
  2. I want the subsection name in the left of the header on odd page numbers.

The subsection name appears perfectly fine on the left of the header on odd page numbers. My problem is with the section name that didn't appear at all. Here's what I have on the preamble:

\pagestyle{fancy}
\fancyhf{}
\fancyhead[RO,LE]{\thepage}
\fancyhead[RE]{\leftmark}
\fancyhead[LO]{\rightmark}


3
  • Did you assign two-sided pages for the class you are using, like this \documentclass[twoside]{article}?
    – hesham
    Mar 18 '20 at 22:21
  • @hesham Yes, I did. Actually what I realized is that only the number of the section appears. But, as I said I need the section title not the number. Mar 19 '20 at 9:22
  • 1
    Welcome to TeX.SE. For future questions (or even this one), it would be helpful if you provided a minimal working example that had everything from \documentclass through \end{document}. Then we wouldn't have to ask if you were using twoside, among other things.
    – Teepeemm
    Mar 19 '20 at 16:54
1

Here is a solution with the 'nameref' package.

However, atm there is one major downside: 'currentlabel' refers to titles of a subsection as well as any other section. Means, if you have multiple pages within a section it will use that title for the header - because no subsection was specified. Do you think my answer will help you nevertheless?

EDIT #1:

\documentclass[twoside]{article}
\usepackage{fancyhdr}
\usepackage{nameref}

\makeatletter
\newcommand*{\currentname}{\@currentlabelname}
\makeatother

\fancypagestyle{yourStyle}{
    \fancyhf{}
    \fancyhead[RO,LE]{\thepage}
    \fancyhead[RE]{\leftmark}
%   \fancyhead[LO]{\rightmark}
    \fancyhead[LO]{\MakeUppercase\currentname}
}

\begin{document}
\pagestyle{yourStyle}
\section{First level}
\newpage
\subsection{Second level}
\newpage
...
\newpage
...
\end{document}
5
  • Sorry, this one didn't help. More than that it made my header disappear. Mar 19 '20 at 14:32
  • Did you run the code as a MWE? Because for me it works fine and of course i do not know what packages you are using - so i have no idea if there are any complications between them. Or perhaps you still use \pagestyle{fancy} - for what i didn't define the style.
    – Venez
    Mar 19 '20 at 15:34
  • Well, as a MWE it worked. But, my type is article my thta has something to do with it. And, also there's no need for a footer because I don't actually want it. Thank you tho Mar 19 '20 at 15:52
  • Ah, okay ... i read only 'book' and forgot to keep the 'article' type in mind. So plz, see my edit. In this case however, 'left'- and 'rightmark' should be working fine.
    – Venez
    Mar 19 '20 at 16:13
  • Okay, this one worked. Thank you so much. Mar 19 '20 at 16:21

This site is temporarily in read only mode and not accepting new answers.

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