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:

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

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.