我正在进一步关注以下问题: 在页眉中插入预定义段落编号 我不被允许对这个问题发表评论所以不得不提出一个新问题。
@Andrew。@Sujato 已经离线几个月了,他要求我接手这个问题,因为我有主要文档。
我还有一些问题。其中之一是你在第 6 点上的修改在章节标题和第一节之间创建了一个新的分页符,而之前并没有这个分页符。有什么办法可以解决这个问题吗?
编辑:新的 MWE-希望现在没问题。
\documentclass[11pt,openany]{book}
\usepackage{lipsum}
\setlipsumdefault{1}
\usepackage{sectsty}
\usepackage[explicit,raggedright]{titlesec}
\usepackage[tocflat]{tocstyle}
\usepackage{tocloft}
\usepackage{soulutf8}
\usepackage{hanging}
%TABLE OF CONTENTS%
\settocstylefeature[]{leaders}{\hfill}
\settocstylefeature[]{entryhook}{}
\settocstylefeature[]{raggedhook}{\raggedright}
\settocstylefeature[]{entryvskip}{0.2em}
\settocstylefeature[0]{entryvskip}{1em}
\settocstylefeature[0]{entryhook}{\bfseries}
\settocstylefeature[0]{pagenumberbox}{\csname @gobble\endcsname}
\renewcommand*{\cfttoctitlefont}{\Large}
%TABLE OF CONTENTS%
\titleformat{\chapter}[display]
{\filcenter}
{}
{1ex}
{\Huge {#1}
\addcontentsline{toc}{chapter}{#1}
\markboth{#1}{#1}}
\titleformat{\section}
{\centering\Large\mdseries}{\thesection}{1em}{\caps{#1}}
\titleformat{\subsection}
{\mdseries\large\raggedright}{}{}{#1}
\titleformat{\subsubsection}
{\mdseries\raggedright}{}{}{#1}
%HEADER and FOOTER%
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{} % clear all header and footer fields
\fancyfoot[LE,RO]{\thepage}
\renewcommand{\headrulewidth}{0pt}
\fancyhead[CO]{\headcaps{\MakeUppercase{\rightmark}}}
\renewcommand{\sectionmark}[1]{%
\markright{#1}%
\def\firstpara{0}\def\midpara{0}\def\lastpara{0}
}
\fancyhead[CE]{\headcaps{\MakeUppercase{\leftmark}}}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\fancyfoot[LO,RE]{\ifnum\firstpara>0\relax\footnotesize MN \thesection.\theparagraph\fi}
\newcommand\headcaps[1]{{ #1}}
\let\realSection\section
\renewcommand\section[2][\relax]{\clearpage%
\thispagestyle{plain}%
\ifx#1\relax\relax\realSection{#2}%
\else\realSection[#1]{#2}%
\fi
}
\renewcommand\thesection{\arabic{section}}
\def\firstpara{0}%
\def\midpara{0}%
\def\lastpara{0}%
\def\setparagraph#1-#2-#3|{\def\midpara{#1}%
% if \firstpara=0 => new page => set \firstpara
\ifnum\firstpara=0\relax\def\firstpara{#1}\fi%
% if #2=0 => only #1 is meaningful to set \lastpara to this
% otherwise use #2
\ifnum#2=0\relax\def\lastpara{#1}%
\else\def\lastpara{#2}%
\fi%
}
\def\paragraph#1{\textbf{#1}\space%
\setparagraph#1-0-|%
}
\def\theparagraph{
% if \firstpara=\lastpara print only \firstpara otherwise print a range
\ifnum\firstpara=\lastpara\relax\firstpara\else\firstpara-\lastpara\fi
% reset firstpara and lastpara
\xdef\firstpara{\midpara}%
%\let\lastpara\relax%
}
% need to reset \firstpara on pages without headers
\fancypagestyle{plain}{
\fancyhf{}
\fancyfoot[LE,RO]{\thepage}
\fancyfoot[LO,RE]{\ifnum\firstpara>0\relax\footnotesize MN \thesection.\theparagraph\fi}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
}
%HEADER & FOOTER%
\setcounter{section}{0} %This can be a higher number f.i. in volume 2
%WIDOWS & ORPHANS%
\widowpenalty=1000
\clubpenalty=1000
%WIDOWS & ORPHANS%
\date{}
\title{Document Title}
\author{Author Name}
\begin{document}
\frontmatter
\pagestyle{empty}
\maketitle
\newpage
\newpage
\tableofcontents
\newpage
\pagestyle{fancy}
\chapter*{Preface}
\lipsum
\mainmatter
\chapter*{The First chapter}
\section{First section}
\paragraph{1}\lipsum
\paragraph{2}\lipsum
\paragraph{3-5}\lipsum
\paragraph{6}\lipsum
\paragraph{7-17}\lipsum
\paragraph{18}\lipsum
Paragraph without number
\paragraph{19-20}\lipsum
\paragraph{21-28}\lipsum
\paragraph{29}\lipsum
\paragraph{30}\lipsum
\section{Second Section}
\paragraph{1}\lipsum
\paragraph{2}\lipsum
\paragraph{3-5}\lipsum
\paragraph{6}\lipsum
\subsection*{Some subsection}
\paragraph{7-17}\lipsum
\paragraph{18}\lipsum
\paragraph{19-20}\lipsum
\paragraph{21-28}\lipsum
\paragraph{29}\lipsum
\paragraph{30}\lipsum
\chapter*{Second Chapter}
\section{Third Section}
\paragraph{1}\lipsum
\paragraph{2}\lipsum
\paragraph{3}\lipsum
\subsection*{Some subsection}
\paragraph{4}\lipsum
\paragraph{5}\lipsum
\subsubsection*{Some subsubsection}
\paragraph{6}\lipsum
\paragraph{7}\lipsum
\paragraph{8}\lipsum
\paragraph{9}\lipsum
\paragraph{10}\lipsum
\paragraph{11}\lipsum
\paragraph{12}\lipsum
\paragraph{13}\lipsum
\paragraph{14}\lipsum
\paragraph{15}\lipsum
One more line
\paragraph{16}\lipsum
\paragraph{17}\lipsum
\paragraph{18}\lipsum
\paragraph{19}\lipsum
\paragraph{20}\lipsum
\end{document}
正如我们在下面讨论的那样,这个 MWE 中存在两个问题:
- 章节标题现在位于单独的页面上,而不是与章节后面的节标题位于同一页面上。
- 段落编号并不总是正确反映该页面上的数字 - 在第 4 页底部的屏幕截图中,数字为 7-20,而应为 7-18
答案1
以下是代码的修改版本在页眉中插入预定义段落编号。我认为这就是你想要的。
我所设置的是\chapter
标志\IsNewChaptertrue
,然后命令使用该\section
标志来确定是否应该使用开始一个新页面\clearpage
。
我认为我在回答上一个问题时测试过这一点,并确定标题中章节编号不正确的问题是由\sectionbreak
来自的命令 引起的titlesec 包,但也许我误解了一些事情。
无论如何,这里是完整的代码:
\documentclass{book}
\usepackage{lipsum}
\setlipsumdefault{1}
\usepackage{titlesec}
%\usepackage[hmargin=63mm,vmargin=68mm]{geometry}
% fancy headings as defined by the OP except we need to be more careful
% with plain headings
\usepackage{fancyhdr}
\fancypagestyle{empty}{
\fancyhf{}
\renewcommand{\headrulewidth}{0pt}
}
\fancypagestyle{plain}{
\fancyhf{}% clear all header and footer fields
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
\fancyfoot[C]{\bfseries \thepage}%
% need to reset \firstpara on pages without headers
\xdef\firstpara{\midpara}%
}
\fancypagestyle{paragraphs}{
\fancyhead[LE,RO]{\thepage}
\fancyhead[CO]{\rightmark}
\fancyhead[CE]{\leftmark}
\fancyhead[LO,RE]{\ifnum\firstpara>0\relax\thesection.\theparagraph\fi}
}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{%
\markright{#1}%
% reset paragraph counters in each section
\def\firstpara{0}\def\midpara{0}\def\lastpara{0}
}
\newif\ifIsNewChapter\IsNewChapterfalse
\let\realChapter\chapter
\renewcommand\chapter[2][\relax]{%
\IsNewChaptertrue%
\ifx#1\relax\relax\realChapter{#2}%
\else\realChapter[#1]{#2}%
\fi
}
\let\realSection\section
\renewcommand\section[2][\relax]{\ifIsNewChapter\relax\else\clearpage\fi
\IsNewChapterfalse%
\thispagestyle{plain}%
\ifx#1\relax\relax\realSection{#2}%
\else\realSection[#1]{#2}%
\fi
}
\renewcommand\thesection{\arabic{section}}
% now the macros for tracking the paragraph numbers
\def\firstpara{0}% set to 1 by default
\def\midpara{0}% set to 1 by default
\def\lastpara{0}% set to 1 by default
\def\setparagraph#1-#2-#3|{\def\midpara{#1}%
% if \firstpara=0 => new page => set \firstpara
\ifnum\firstpara=0\relax\def\firstpara{#1}\fi%
% if #2=0 => only #1 is meaningful to set \lastpara to this
% otherwise use #2
\ifnum#2=0\relax\def\lastpara{#1}%
\else\def\lastpara{#2}%
\fi%
}
\def\paragraph#1{\textbf{#1}\space
\setparagraph#1-0-|%
}
\def\theparagraph{%
% if \firstpara=\lastpara print only \firstpara otherwise print a range
\ifnum\firstpara=\lastpara\relax\firstpara\else\firstpara--\lastpara\fi
% reset firstpara and lastpara
\xdef\firstpara{\midpara}%
%\let\lastpara\relax%
}
% back to the MWe
\begin{document}
\null
\pagestyle{empty}
\newpage
\newpage
\pagestyle{paragraphs}
\chapter{A Chapter}
\section{First Section}
\paragraph{1}\lipsum
\paragraph{2}\lipsum
\paragraph{3-5}\lipsum
\paragraph{6}\lipsum
\paragraph{7-17}\lipsum
\paragraph{18}\lipsum
\paragraph{19-20}\lipsum
\paragraph{21-28}\lipsum
\paragraph{29}\lipsum
\paragraph{30}\lipsum
\section{Second Section}
\paragraph{1}\lipsum
\paragraph{2}\lipsum
\paragraph{3-5}\lipsum
\paragraph{6}\lipsum
\paragraph{7-17}\lipsum
\paragraph{18}\lipsum
\paragraph{19-20}\lipsum
\paragraph{21-28}\lipsum
\paragraph{29}\lipsum
\paragraph{30}\lipsum
\section{Third Section}
\paragraph{1}\lipsum
\paragraph{2}\lipsum
\paragraph{3}\lipsum
\paragraph{4}\lipsum
\paragraph{5}\lipsum
\paragraph{6}\lipsum
\paragraph{7}\lipsum
\paragraph{8}\lipsum
\paragraph{9}\lipsum
\paragraph{10}\lipsum
\paragraph{11}\lipsum
\paragraph{12}\lipsum
\paragraph{13}\lipsum
\paragraph{14}\lipsum
\paragraph{15}\lipsum
\paragraph{16}\lipsum
\paragraph{17}\lipsum
\paragraph{18}\lipsum
\paragraph{19}\lipsum
\paragraph{20}\lipsum
\end{document}