2

I have the following code

\documentclass[12pt,a4paper,openany]{book}
\usepackage[utf8]{inputenc}
\usepackage[
 left=1in, 
 right=1in, 
 top=1.25in, 
 bottom=1.25in,
 headsep=30pt,
 heightrounded
 ]{geometry}
 \usepackage[x11names, svgnames, dvipsnames]{xcolor}
 \usepackage{amsmath}
 \numberwithin{equation}{subsection}
 \usepackage{amsfonts}
 \usepackage{amssymb}
 \usepackage{amsthm}
 %==============================================================
 \usepackage{fancyhdr}
%\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
%\renewcommand{\sectionmark}[1]{\markright{#1}}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[LE,RO]{\thepage}
\fancyhead[LO]{\small\nouppercase{\rightmark}}
\fancyhead[RE]{\small\nouppercase{\leftmark}}
\renewcommand{\headrulewidth}{0pt}
%==============================================================
\newtheorem{theorem}{Theorem}[section]
 \usepackage{lipsum}

\begin{document}

\pagenumbering{roman} \thispagestyle{plain} \tableofcontents \addcontentsline{toc}{chapter}{Table of Contents} \thispagestyle{plain}

\newpage

\pagenumbering{arabic}

\chapter{Preliminaries} \section{Basics} \lipsum[1-15]

\chapter{$C3$-Modules} \lipsum[1-30]

\end{document}

As you see, chapter 3 has no sections. I need Chapter 3. $C3$-Modules appears in the header of every page of the chapter, not only in the even pages. I need this behaviour for those chapters which do not contain sections.

1 Answers1

0

You can test for an empty \rightmark and use \leftmark in this case (I've removed several packages not related to the problem):

\documentclass[12pt,a4paper,openany]{book}
\usepackage[utf8]{inputenc}% should also not be needed from LaTeX 2018-04-01
\usepackage[
 left=1in, 
 right=1in, 
 top=1.25in, 
 bottom=1.25in,
 headsep=30pt,
 heightrounded
 ]{geometry}
 %==============================================================
\usepackage{scrextend}% provides \Ifstr (and others)
\providecommand\Ifstr{\ifstr}% for historic LaTeX installations only.
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[LE,RO]{\thepage}
\fancyhead[LO]{\small\nouppercase{\Ifstr{\rightmark}{}{\leftmark}{\rightmark}}}
\fancyhead[RE]{\small\nouppercase{\leftmark}}
\renewcommand{\headrulewidth}{0pt}
%==============================================================
\usepackage{lipsum}

\begin{document}

\cleardoublepage% not needed in this case, but needed, e.g. if there is a % title before the following lines. \pagenumbering{roman} \thispagestyle{plain}% not needed, because default for chapters and therefore table of contents \addcontentsline{toc}{chapter}{Table of Contents} \tableofcontents

\newpage% should \cleardoublepage, or use \mainmatter instead of \pagenumbering

\pagenumbering{arabic}

\chapter{Preliminaries} \section{Basics} \lipsum[1-15]

\chapter{$C3$-Modules} \lipsum[1-30]

\end{document}

Note: You should use \cleardoublepage instead of \newpage before \pagenumbering. Otherwise it could happen, that two odd pages are following each other, which is problematic, when printing the document. With book I recommend to use \frontmatter and \mainmatter instead of \pagenumbering, because these automatically use \cleardoublepage, when needed.

Also note, that adding \addcontentsline after \tableofcontents would use the last page of the table of contents instead of the first one as reference in the table of contents. Best would be to use package tocbibind to add such entries (or use a class, that already supports such entries).

cabohah
  • 11,455
  • Excuse me, can you show me how to use \frontmatter and mainmatter ? Where to put these lines ? – Hussein Eid Mar 24 '24 at 19:46
  • I get an error when I run this code. Texmaker says that \Ifstr is undefined control sequence. However, egreg's solution (the accepted answer in the link you posted in the comment) worked for me. – Hussein Eid Mar 24 '24 at 19:48
  • It is irrelevant what Texmaker says. Relevant is, what LaTeX says. \Ifstr is defined in scrextend since version 3.28 (end of 2019). Before it was \ifstr. But if you are using a more than 4 years old LaTeX, you should consider an update. – cabohah Mar 25 '24 at 07:48
  • For using \frontmatter and \mainmatter just replace \pagenumbering{roman} by \frontmatter (and then you can remove the \cleardoublepage) and replace \pagenumbering{arabic} by \mainmatter (and then you can remove the \newpage). See a LaTeX introduction for more information. – cabohah Mar 25 '24 at 07:49
  • Excuse me, how can I update Latex? Is it possible to update or do you mean that I have to remove the old latex and download a new one?. If I downloaded a new version, will the old codes run properly? – Hussein Eid Mar 25 '24 at 21:31
  • @HusseinEid This is another question and the answer depends on the TeX distribution you are using. With MiKTeX see: How should one maintain and update a MiKTeX installation?. There are similar questions for TeX Live depending on Vanilla or Linux package versions. – cabohah Mar 26 '24 at 07:59