0

This is a repost of yesterday's question. Forgot to box code; my apologies!
Objective: build simple macros to specify page headers for my first LaTeX book.

Task 1: retrieve chapter, section and subsection names with low level macros. Code:

\usepackage{amsmath}
\let\Chaptermark\chaptermark%
\def\chaptermark#1{\def\Chaptername{#1}\Chaptermark{#1}}%
\let\Sectionmark\sectionmark%
\def\sectionmark#1{\def\Sectionname{#1}\Sectionmark{#1}}%
\let\Subsectionmark\subsectionmark%
\def\subsectionmark#1{\def\Subsectionname{#1}\Subsectionmark{#1}}

Quick check:

\chapter{First Chapter}\label{ch:first}
This is Chapter~\ref{ch:first} with title `\Chaptername''.
\section{First Section}\label{sec:first}
This is Section~\ref{sec:first} with title ``\Sectionname''.
\subsection{First Subsection}\label{subsec:first}
This is Subsection~\ref{subsec:first} with title ``\Subsectionname''.
\newpage
\chapter{Second Chapter}\label{ch:second}
This is Chapter~\ref{ch:second} with title ``\Chaptername''.
\section{First Section of Chapter 2}\label{sec:second}
This is Section~\ref{sec:second} with title ``\Sectionname''.
\subsection{First Subsection of Section 1 of Chapter 2}\label{subsec:second}
This is Subsection~\ref{subsec:second} with title ``\Subsectionname''.
\newpage
\chapter{Third Chapter}\label{ch:third}
This is Chapter~\ref{ch:third} with title ``\Chaptername''.
\section{First Section of Chapter 3}\label{sec:third}
This is Section~\ref{sec:third} with title ``\Sectionname''.
\subsection{First Subsection of Section 1 of Chapter 3}\label{subsec:third}
This is Subsection~\ref{subsec:third} with title ``\Subsectionname''.
\end{document} 

Works fine (of course labels will be far more complex in the book).

Task 2: specify header and footer. Target format:

even pages: RJ "Chapter#": chapter title.

odd pages: LJ "Chapter#.Section#: section title.

using the above-extracted titles. Question: should I redo \markleft and \markright using \def?
Or is there a simpler way?

In PlainTeX all of this was duck soup in 1986, 6 lines of code. But I am not that familiar with LaTeX, except for the biblios macros, which I had to tweak a decade ago for a review article.

3
  • 2
    you can edit posts, no need to make new question Jun 10, 2022 at 19:17
  • Why do you want the subsection titles, as you don't use them in the headers? Jun 11, 2022 at 8:35
  • Tried this code in TeXShop on a Mac. Stops immediately with a missing docstyle error. Jun 13, 2022 at 15:59

1 Answer 1

0

Try the following, much simpler than your MWE.

% bookheadersprob.tex  SE 647340

\documentclass{book}

\usepackage{comment}

\usepackage{amsmath}


\begin{comment}    %%% don't need next bunch of code
\let\Chaptermark\chaptermark%
\def\chaptermark#1{\def\Chaptername{#1}\Chaptermark{#1}}%
\let\Sectionmark\sectionmark%
\def\sectionmark#1{\def\Sectionname{#1}\Sectionmark{#1}}%
\let\Subsectionmark\subsectionmark%
\def\subsectionmark#1{\def\Subsectionname{#1}\Subsectionmark{#1}}
\end{comment}

%% remove \chaptername beforethe number in the headings
\renewcommand{\chaptermark}[1]{\markboth{\thechapter. #1}{}}

%%% define \...name (but only because they occur in the MWE)
\newcommand{\Chaptername}{Chapter} %%%%\chaptername
\newcommand{\Sectionname}{Section}
\newcommand{\Subsectionname}{Subsection}

  
\begin{document}

\chapter{First Chapter}\label{ch:first}
This is Chapter~\ref{ch:first} with title `\Chaptername''.
\newpage
\section{First Section}\label{sec:first}
This is Section~\ref{sec:first} with title ``\Sectionname''.
\subsection{First Subsection}\label{subsec:first}
This is Subsection~\ref{subsec:first} with title ``\Subsectionname''.

\newpage

\section{A section}
\subsection{A subsection}

\newpage
\chapter{Second Chapter}\label{ch:second}
This is Chapter~\ref{ch:second} with title ``\Chaptername''.
\section{First Section of Chapter 2}\label{sec:second}
This is Section~\ref{sec:second} with title ``\Sectionname''.
\subsection{First Subsection of Section 1 of Chapter 2}\label{subsec:second}
This is Subsection~\ref{subsec:second} with title ``\Subsectionname''.
\newpage
\chapter{Third Chapter}\label{ch:third}
This is Chapter~\ref{ch:third} with title ``\Chaptername''.
\section{First Section of Chapter 3}\label{sec:third}
This is Section~\ref{sec:third} with title ``\Sectionname''.
\subsection{First Subsection of Section 1 of Chapter 3}\label{subsec:third}
This is Subsection~\ref{subsec:third} with title ``\Subsectionname''.
\end{document}

As far as I could make out you did not want "Chapter" to appear in the headers. The above does that by changing the \chaptermark, ignoring what you have tried in your MWE.

You say that it would have been "duck soup" in PlainTex requiring only 6 lines of code. The duck must have been rather tough as using LaTeX it only requires 1 line of code. --- GOM

EDIT

For more information read sections 4.3.4 LaTeX mark commands and 4.4 Page styles in the LaTeX Companion by Frank Mittlebach and Michel Goosens, Second Edition, Addison-Wesley, 2004, ISBN 0-201-362999-6.

You must log in to answer this question.

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