这是一个 MWE
\documentclass[paper=a4,10pt, twoside]{scrbook}%
\usepackage{tgtermes}
\usepackage[titles]{tocloft}
\setcounter{tocdepth}{3}
\makeatletter
\renewcommand{\@tocrmarg}{4em}
\makeatother
\makeatletter
\renewcommand*\l@section[2]
{
\addpenalty{-\@highpenalty}%
\vskip 0.1em \@plus\p@
\@dottedtocline{1}{1.5em}{2.5em}{\normalfont #1}{\normalfont #2}
}
\makeatother
\makeatletter
\renewcommand*\l@chapter[2]
{
\addpenalty{-\@highpenalty}%
\vskip 0.5em \@plus\p@
\@dottedtocline{0}{0em}{1em}{\large #1}{\large #2}
}
\makeatother
\makeatletter
\newcommand*\l@problem[2]
{
\addpenalty{-\@highpenalty}%
\vskip 0.1em \@plus\p@
\@dottedtocline{1}{1.5em}{2.5em}{\normalfont #1}{\normalfont #2}
}
\makeatother
\usepackage[noindentafter, pagestyles, explicit, extramarks]{titlesec}
\newpagestyle{main}{
\sethead[\thepage ][][\chaptername \, \thechapter. \chaptertitle]% even
{\ifthesection{\thesection \, \sectiontitle }{\theproblem \, \problemtitle}}{}{\thepage}} % odd
%chapter
\titleformat{\chapter}{\vspace{75pt}\Large} {\makebox[25 pt]{\raggedright\thechapter}}{8pt}{\hspace*{-6pt}\raggedright\Large #1}[\vspace{0.3pc} {\titlerule[3.5pt]} \vspace{50pt}]
\titlespacing{\chapter}{0 pt}{0 pt}{0 pt}[0 pt]
%section
\titleformat{\section}{\large}{\makebox[20 pt]{\large\thesection}}{15 pt}{#1}
\titlespacing{\section}{-35 pt}{3ex plus .1ex minus .2ex}{3ex plus .1ex minus .2ex}
%problem
\newcounter {problem}[chapter]
\makeatletter
\renewcommand {\theproblem}{P. \thechapter.\@arabic\c@problem}
\makeatother
\newcommand{\problemtitle}{}
\titleclass{\problem}{straight}[\chapter]
\titleformat{name=\problem}{\large }{\makebox[25 pt]{\large\theproblem}}{15 pt}{#1}[]
\titlespacing{name=\problem}{-40 pt}{4ex plus .1ex minus .2ex}{3ex plus .1ex minus .2ex}
\usepackage{lipsum}
\begin{document}
\cleardoublepage
\phantomsection
\tableofcontents
\pagestyle{main}
\chapter{This is the first chapter}
\lipsum[1]
\section{This is the first section}
\lipsum[2-20]
\problem{This is the first problem}
\lipsum[20-30]
\section{This is the second section}
\lipsum[2-10]
\end{document}
我想修改第七页的页眉,并添加类似这样的内容,\theproblem \problemtitle
因为我已定义了一个新的分段命令problem
。我不知道如何实现。
答案1
希望我理解得没错。搜索% NEW CONTENT START
/% NEW CONTENT END
如下:
\documentclass[paper=a4,10pt, twoside]{scrbook}%
\usepackage{tgtermes}
\usepackage[titles]{tocloft}
\setcounter{tocdepth}{3}
\makeatletter
\renewcommand{\@tocrmarg}{4em}
\makeatother
\makeatletter
\renewcommand*\l@section[2]
{
\addpenalty{-\@highpenalty}%
\vskip 0.1em \@plus\p@
\@dottedtocline{1}{1.5em}{2.5em}{\normalfont #1}{\normalfont #2}
}
\makeatother
\makeatletter
\renewcommand*\l@chapter[2]
{
\addpenalty{-\@highpenalty}%
\vskip 0.5em \@plus\p@
\@dottedtocline{0}{0em}{1em}{\large #1}{\large #2}
}
\makeatother
\makeatletter
\newcommand*\l@problem[2]
{
\addpenalty{-\@highpenalty}%
\vskip 0.1em \@plus\p@
\@dottedtocline{1}{1.5em}{2.5em}{\normalfont #1}{\normalfont #2}
}
\makeatother
\usepackage[noindentafter, pagestyles, explicit, extramarks]{titlesec}
%chapter
\titleformat{\chapter}{\vspace{75pt}\Large} {\makebox[25 pt]{\raggedright\thechapter}}{8pt}{\hspace*{-6pt}\raggedright\Large #1}[\vspace{0.3pc} {\titlerule[3.5pt]} \vspace{50pt}]
\titlespacing{\chapter}{0 pt}{0 pt}{0 pt}[0 pt]
%section
\titleformat{\section}{\large}{\makebox[20 pt]{\large\thesection}}{15 pt}{#1}
\titlespacing{\section}{-35 pt}{3ex plus .1ex minus .2ex}{3ex plus .1ex minus .2ex}
%problem
\newcounter {problem}[chapter]
\makeatletter
\renewcommand{\theproblem}{P. \thechapter.\@arabic\c@problem}
\let\problemtitle\@empty
\makeatother
\titleclass{\problem}{straight}[\chapter]
% NEW CONTENT START
\titleformat{name=\problem}{\large }{\makebox[25 pt]{\large\theproblem}}{15 pt}{#1\gdef\problemtitle{#1}}[]
% NEW CONTENT START
\titlespacing{name=\problem}{-40 pt}{4ex plus .1ex minus .2ex}{3ex plus .1ex minus .2ex}
% NEW CONTENT START
% page styles
\newpagestyle{chapter}{
\sethead%
[\thepage][][\chaptername \, \thechapter. \chaptertitle]% even
{}{}{\thepage}% odd
}
\newpagestyle{section}{
\sethead%
[\thepage][][\chaptername \, \thechapter. \chaptertitle]% even
{\thesection \, \sectiontitle}{}{\thepage}% odd
}
\newpagestyle{problem}{
\sethead%
[\thepage][][\chaptername \, \thechapter. \chaptertitle]% even
{\theproblem \, \problemtitle}{}{\thepage}% odd
}
\makeatletter
\let\chapter@without@pagestyle\chapter
\def\chapter{\pagestyle{chapter}\chapter@without@pagestyle}
\let\section@without@pagestyle\section
\def\section{\pagestyle{section}\section@without@pagestyle}
\let\problem@without@pagestyle\problem
\def\problem{\pagestyle{problem}\problem@without@pagestyle}
\makeatother
% NEW CONTENT END
\usepackage{lipsum}
\begin{document}
\cleardoublepage
\phantomsection
\tableofcontents
% NEW CONTENT START
\pagestyle{empty}
\chapter{This is the first chapter}
\lipsum[1-20]
\section{This is the first section}
\lipsum[2-20]
\problem{This is the first problem}
\lipsum[20-30]
\section{This is the second section}
\lipsum[2-15]
\section{This is the third section}
\lipsum[16]
\problem{This is the second problem}
\lipsum[17]
% NEW CONTENT END
\end{document}
结果如下:
- 第 1 章在第 3 页定义。因此,从第 3 页(实际上是第 5 页)开始,奇数页上不存在与章节/问题相关的标题。
- 第 1.1 节在第 6 页定义。因此,从第 6 页开始,将节相关标题添加到奇数页(实际上是第 7 页)。
- 问题 1.1 在第 9 页定义。因此,从第 9 页开始将问题相关页眉添加到奇数页(实际上是在第 9 页)。
- 第 1.2 节在第 11 页定义。因此,从第 6 页开始(实际上是在第 11 页)将节相关标题添加到奇数页。
- 第 1.3 节在第 13 页定义。因此,从第 13 页开始,与节相关的标题被添加到奇数页中(实际上无处不在)。
- 问题 1.2 在第 13 页定义。因此,从第 13 页开始,将问题相关页眉添加到奇数页(实际上是在第 13 页)。
最后,\chapter
取消奇数页页眉。然后由\section
和再次设置\problem
,以较晚者为准。