0

I would like to get two different headings for the even and odd-numbered pages of the document: even-numbered pages should contain the page number of the book, followed by the prefix "Chapter" and the chapter number, aligned to the left; odd-numbered pages, on the other hand, the chapter nomer (without prefix) and the page number of the book aligned to the right

So far the best I've been able to get is this one, which, however, leaves my even-numbered page headings completely blank. How can I do this? Thank you to anyone who will answer me.

\documentclass[12pt, twoside]{book}
\usepackage{amssymb}
\usepackage{enumitem}
\usepackage{titlesec}
\usepackage{ragged2e}
\usepackage{lipsum}  
\usepackage{textcase}
\titleformat{\chapter}[display]
{\normalfont\huge\bfseries\raggedleft}
  {\chaptertitlename\ \thechapter}{20pt}
{\titlerule[1pt]\vspace{1ex}\Huge}
\usepackage{graphicx}
\usepackage{amsmath}%
\usepackage{floatrow}
\setcounter{MaxMatrixCols}{30}%
\usepackage{amsfonts}
\usepackage{geometry}
\geometry{
  b5paper,
  total={6.93in, 9.84in},
  top=1.26in,
  bottom=0.98in,
  left=0.95in,
  right=0.95in,
}
\usepackage{fancyhdr}
\fancypagestyle{ChapterStyle}{
    \fancyhf{}
    \fancyhead[RO]{\normalsize\nouppercase{\leftmark}\ \thepage}
    \renewcommand{\headrulewidth}{0.4pt}
}
\fancypagestyle{ChapterStyleEven}{
    \fancyhf{}
    \fancyhead[LE]{\footnotesize\textbf{Chapter \thechapter}\ \thepage}
    \renewcommand{\headrulewidth}{0.4pt}
}
\titleformat{\chapter}[display]
    {\normalfont\huge\bfseries}{\chaptertitlename\ \thechapter}{20pt}{\Huge\filleft}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}

\fancypagestyle{plain}{
    \fancyhf{}
    \renewcommand{\headrulewidth}{0pt}
    \renewcommand{\footrulewidth}{0pt}
}
\makeatletter
\renewcommand{\@makechapterhead}[1]{%
  \vspace*{50\p@}%
  {\parindent \z@ \raggedleft \normalfont
    \ifnum \c@secnumdepth >\m@ne
        \huge\bfseries \thechapter
    \fi
    \par\nobreak
    \interlinepenalty\@M
    \Huge\bfseries #1\par\nobreak
    \vskip 20\p@
    \hrule
    \vskip 40\p@
  }}
\makeatother

\newenvironment{proof}[1][Proof]{\textbf{#1.} }{\  \rule{0.5em}{0.5em}}
\begin{document}
\frontmatter
\pagestyle{fancy}
\fancyhf{}
\fancyhead[R]{\thepage}
\renewcommand{\headrulewidth}{0pt}
\begin{titlepage}
\title{Example Document}
\end{titlepage}

\date{}
\maketitle
\newpage
\setcounter{page}{1}

\tableofcontents
\newpage

\noindent {\LARGE Preface}.

\bigskip

\noindent 
\lipsum[2-4]

\mainmatter
\setcounter{chapter}{0} 
\thispagestyle{plain}
\newpage
\chapter{Example chapter}
\noindent\fbox{%
    \parbox{\textwidth}{%
\begin{minipage}{0.99\textwidth} 
\vspace{5pt}
\justifying 
\begin{itemize}[leftmargin=15pt]
\item 
      
\item 
      
\item 
      
\item 
      
\item 
\end{itemize}
\vspace{1pt}
\end{minipage}%
    }%
}
\newpage
\pagestyle{ChapterStyle}
\section{introduction}
\vspace*{-10pt}
\lipsum[2-20]
\end{document}
5
  • 1
    are you using [oneside] or [twoside] class option? Commented Jun 1, 2023 at 14:34
  • the twoside one (\documentclass[12pt, twoside]{book}). thanks
    – Alessandro
    Commented Jun 1, 2023 at 14:35
  • 2
    it would help if you showed a small complete document. You show two page style definitions, one has blank heading on even pages, one does not, but you do not show which page style you use, (the headings should use \leftmark and \rightmark not \thechapter but worry about that later) Commented Jun 1, 2023 at 14:51
  • I have added a complete example, forgive me for the poor use of latex :)
    – Alessandro
    Commented Jun 1, 2023 at 16:21
  • This is the result i attempt to get: ibb.co/711XrNc
    – Alessandro
    Commented Jun 1, 2023 at 16:40

1 Answer 1

0

There are quite a few warnings and errors in the code you provided; I will not attempt to fix them all for you. You are however encouraged to fix them (either read through the package documentations to find out how, or failing that, ask questions about them on this site).

For the style issue, you don't define the page style separately for even and odd pages. The issuance of [RO] and [LE] in the \fancyhead command tells it whether it should be applied to the odd or even pages. So something like

\fancypagestyle{ChapterStyle}{
    \fancyhf{}
    \fancyhead[RO]{\footnotesize\nouppercase{\leftmark} \quad \thepage}
    \fancyhead[LE]{\footnotesize\thepage \quad\textbf{Chapter \thechapter}}
    \renewcommand{\headrulewidth}{0.4pt}
}

would set both sides appropriately. (I've taken the liberty to make it so that on the even pages the page number appears on the outside, and not following the Chapter number; I've also adjusted the spacing and sizes a bit so that the two sides match.)

Your call to \geometry also should be fixed:

\geometry{
  b5paper,
  top=1.26in,
  bottom=0.98in,
  left=0.95in,
  right=0.95in,
  headheight=15pt
}

Your original version specified the paper size, the size of the printable area, and the size of the margins. This is overspecifying things (any two determines the third); don't do that. I've also added a specification of headheight to create a big more room for your headers (mostly because your original version used \normalsize for the headers and the default space reserved is not enough).

2
  • Thanks, your solution is perfect. I still have one last problem with the bibliography, not showing properly since the first number of the list is always overlaid with another number (ibb.co/P4hSwNr). What could this be due to? One suspicion I have is some conflict with the document styles, since even the "bibliography" wording is aligned to the right when I would like to align it to the left.
    – Alessandro
    Commented Jun 2, 2023 at 12:16
  • @Alessandro: given the image you posted you probably have already a minimal example prepared showing that problem. I would suggest asking a new Q on this site with that minimal example included. Just from the output it is very hard to diagnose what is wrong. Commented Jun 2, 2023 at 13:20

You must log in to answer this question.

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