1

I would like to put in the headers the section in the left header of odd (right-side) pages, and the subsection in the right header of even (left-side) pages (unless there is no subsection in which case I want the section in both places). Thank you

\documentclass{book}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{textcomp}
\usepackage{sidenotes}
\usepackage{graphicx}
\usepackage{float}
\usepackage{esint}
\usepackage{lipsum}
\usepackage{booktabs}
\usepackage{wrapfig}
\usepackage{multirow}
\usepackage{enumitem}
% \usepackage{parskip}
\usepackage{emptypage}
\usepackage{subcaption}
\usepackage{fontawesome,blindtext}
\usepackage{multicol}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{amsmath, amsfonts, mathtools, amsthm, amssymb}
\usepackage{mathrsfs}
\usepackage{marginnote}
\usepackage{placeins}
\usepackage{cancel}
\usepackage{bm}
% horizontal rule
\newcommand\hr{
    \noindent\rule[0.5ex]{\linewidth}{0.5pt}
}



\usepackage{fancyhdr}
\pagestyle{fancy}
\fancypagestyle{plain}{%
    \fancyhf{} % 
    \renewcommand{\headrulewidth}{0pt}
    \renewcommand{\footrulewidth}{0pt}}
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\subsectionmark}[1]{\markright{\thesubsection. #1}}
\fancyhf{}
\fancyhead[LEH,ROH]{\small\bfseries\thepage} %numeri pagina m.est.
\fancyhead[REH]{\bfseries\small\nouppercase{\thesubsection.}} %capitoli m.int.sx
\fancyhead[LOH]{\bfseries\small\nouppercase{\rightmark}} %sezioni m.int.dx
\fancyfoot[C]{\bfseries\nouppercase{\leftmark}}
\makeatletter








\begin{document}
\tableofcontents
\chapter{Hello}
\section{section}
\lipsum\lipsum\lipsum\lipsum\lipsum\lipsum 

\chapter{Hello2}
\section{section}
\subsection{subsec}
\lipsum\lipsum\lipsum\lipsum\lipsum\lipsum 
\end{document}
1
  • It would be better to ask a clear concrete question rather than just state some goals. It's hard to give advice when we don't know what document class, packages, etc., you're using, and what you've tried so far. Try to provide a minimal working example that comes to close to doing what you want and then ask a concrete question about how to fix any problems with it.
    – frabjous
    Sep 14, 2022 at 18:03

1 Answer 1

0

The question is a little confusing because you say "the section on the right and the subsection on the left". I take it you mean you want the section in the left header of odd (right-side) pages, and the subsection in the right header of even (left-side) pages (unless there is no subsection in which case you want the section in both places).

If I misunderstood, I think it'll be easy to change, however.

While it may be possible to do this using the builtin marking mechanisms (not sure), since you also need a mark for chapters (at the bottom), I think you'll want to load fancyhdr's companion extramarks package as well so you have more than two marks to use.

This defines a command \extramarks that works like \markboth but for differently named marks (leftxmark, rightxmark).

Here I suggest setting the right extra mark inside both \sectionmark and \subsectionmark commands, and then use \lastrightxmark in the headers on the right of even pages. It'll use whatever right extra mark was given last, so if a subsection command is not given after section, the section's marking will apply, but if a subsection title is given, the subsection's marking will apply.

Here's my suggested code.

\documentclass{book}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath, mathtools, amsthm, amssymb}
\usepackage{textcomp}
\usepackage{sidenotes}
\usepackage{graphicx}
\usepackage{float}
\usepackage{esint}
\usepackage{lipsum}
\usepackage{booktabs}
\usepackage{wrapfig}
\usepackage{multirow}
\usepackage{enumitem}
% \usepackage{parskip}
\usepackage{emptypage}
\usepackage{subcaption}
\usepackage{fontawesome,blindtext}
\usepackage{multicol}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{mathrsfs}
\usepackage{marginnote}
\usepackage{placeins}
\usepackage{cancel}
\usepackage{bm}
% horizontal rule
\newcommand\hr{
    \noindent\rule[0.5ex]{\linewidth}{0.5pt}
}


\usepackage{fancyhdr,extramarks}
% plain style for first page of chapters
\fancypagestyle{plain}{%
    \fancyhf{} % 
    \renewcommand{\headrulewidth}{0pt}
    \renewcommand{\footrulewidth}{0pt}
}

% fancy style elsewhere
\pagestyle{fancy}
\renewcommand{\headrulewidth}{0.4pt}
\fancyhf{}
% page numbers, even pages left, odd pages right
\fancyhead[LEH,ROH]{\small\bfseries\thepage} %numeri pagina m.est.
% use the extra mark (could be section or subsection) on right of even pages
\fancyhead[REH]{\bfseries\small\nouppercase{\lastrightxmark}} %capitoli m.int.sx
% use the left mark (section) on left of odd pages
\fancyhead[LOH]{\bfseries\small\nouppercase{\rightmark}} %sezioni m.int.dx
% chapter in footer center
\fancyfoot[C]{\bfseries\nouppercase{\leftmark}}

% marking commands

% subsections change the right extra mark
\renewcommand{\subsectionmark}[1]{%
    \extramarks{}{#1\ \thesubsection}
}
% sections change the right mark, and right extra mark
\renewcommand{\sectionmark}[1]{%
    \markright{\thesection\ #1}
    \extramarks{}{#1\ \thesection}
}
% chapters set left mark and reset others
\renewcommand{\chaptermark}[1]{%
    \markboth{\chaptername\ \thechapter. #1}{}
    \extramarks{}{}
}

\begin{document}
\tableofcontents
\chapter{First Chapter}
\section{section without subsection}
\lipsum[1-20]

\chapter{Second Chapter}
\section{section with subsection}
\subsection{my subsection}
\lipsum[21-40]

\end{document}

In the chapter without subsections you get the section title and number on even pages:

without subsections

In the chapter with subsections you get the subsection title and number there.

with subsections

I think that's what you wanted if I understood you right.

Unrelated but I also moved the ams packages further up in your list, to avoid problems with the same command being defined multiple times, and removed amsfonts (since amssymb loads that anyway). I also removed the stray \makeatletter, which wasn't doing anything useful, and isn't good to have without \makeatother.

EDIT: If you want the subsection numbers on the left of the title instead of having the numbers always on the outside as I have them, you can use:

% subsections change the right extra mark
\renewcommand{\subsectionmark}[1]{%
    \extramarks{}{\thesubsection\ #1}
}
% sections change the right mark, and right extra mark
\renewcommand{\sectionmark}[1]{%
    \markright{\thesection\ #1}
    \extramarks{}{\thesection\ #1}
}

In all these commands, the #1 represents where the title goes, and the commands starting with \the (e.g., \thesubsection) represent the numbers.

1
  • Thank you very much for the solution and for having improved my code !! Sep 15, 2022 at 19:47

You must log in to answer this question.

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