![书页标题的简单宏](https://linux22.com/image/451180/%E4%B9%A6%E9%A1%B5%E6%A0%87%E9%A2%98%E7%9A%84%E7%AE%80%E5%8D%95%E5%AE%8F.png)
这是昨天问题的重发。忘记了框代码;抱歉!
目标:构建简单的宏来指定我的第一本 LaTeX 书的页眉。
任务 1:使用低级宏检索章节、部分和小节名称。代码:
\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}}
快速检查:
\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}
效果很好(当然书中的标签会复杂得多)。
任务 2:指定页眉和页脚。目标格式:
偶数页:RJ“章节#”:章节标题。
奇数页:LJ“Chapter#.Section#:章节标题。
使用上面提取的标题。问题:我应该使用 \def 重做 \markleft 和 \markright 吗?
或者有更简单的方法吗?
在 1986 年的 PlainTeX 中,所有这些都是小菜一碟,只需 6 行代码。但我对 LaTeX 不太熟悉,除了 biblios 宏,十年前我不得不为一篇评论文章调整它。
答案1
尝试以下步骤,比你的 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}
据我所知,您不希望“章节”出现在标题中。上面的代码通过更改来实现这一点\chaptermark
,忽略了您在 MWE 中尝试的操作。
您说如果用 PlainTex 只需要 6 行代码就能完成“鸭汤”。鸭子一定很难做,因为用 LaTeX 只需要 1 行代码。--- GOM
编辑
更多信息请阅读第 4.3.4 节LaTeX 标记命令和 4.4页面样式在LaTeX 伴侣作者:Frank Mittlebach 和 Michel Goosens,第二版,Addison-Wesley,2004 年,ISBN 0-201-362999-6。