如何在同一页上开始章节并保留页眉和页脚(已有代码可用)

如何在同一页上开始章节并保留页眉和页脚(已有代码可用)

设置

目前,这是我的项目第一页的代码和总体布局:

\documentclass[12pt, oneside]{book}

\usepackage{sectsty,fancyhdr}
\sectionfont{\LARGE\bfseries}
\usepackage{geometry}

\usepackage{lipsum}%remove 


% *******************************Margin Settings*******************************

\geometry{% margin settings, has to go first before \pagestyle{}
    paper=a4paper, 
    inner=2.5cm, % Inner margin
    outer=2.5cm, % Outer margin
    bindingoffset=0.0cm, % Binding offset
    top=2.5cm, % Top margin
    bottom=2.5cm, % Bottom margin
    headsep=3.5mm,% controles header  
    footskip=6.4mm,% controles footer
    %showframe,% show how the type block is set on the page
}

% ***************************Header/Footer Settings****************************

\sectionfont{\LARGE\bfseries}%this will keep the formattings of sections
\pagestyle{fancy}
\fancyhf{} % sets all head and foot elements empty.
\rhead{} % right side of upper footer 
\lhead{Project title} %left side 
\renewcommand{\headrulewidth}{2.0pt} % Width of the top line
\renewcommand{\footrulewidth}{1.0pt} % Bottom line 
\setlength{\headheight}{14.5pt}%used to remove the \fancyhf{} \headheight error
\rfoot{Page \thepage} % vice versa with the page number 
\lfoot{\leftmark} % Should show the name of section 

\makeatletter
\renewcommand{\sectionmark}[1]{%this avoids the \makecaptical of footers
  \markboth{\ifnum \c@secnumdepth>\z@ %this allowes symbols in foorters
      \thesection\hskip 1em\relax
    \fi #1}{}}
\makeatother


%****************************ADD NEW CODE HERE**********************



%***************************Title************************************
\newcommand{\mytitle}{
\begin{center}
{\Huge{\textbf{Project of...}}} 
\end{center}

\begin{center}
\textsc{\Large My name}\\[0.5cm] 
\end{center}

\begin{center}
{\large My ID number}\\[0.5cm] 
\end{center}
}

%------------------------------------------------------------------

% Begin 

%------------------------------------------------------------------

\begin{document}

\mytitle
\chapter{chapter 1}
\section{section 1.1}
\subsection{subection 1.1.1}
  \lipsum%remove

\chapter{chapter 2}
\section{section 2.1}
\subsection{subection 2.1.1}
  \lipsum%remove

%------------------------------------------------------------------

% end 

%------------------------------------------------------------------ 


\end{document}  

问题

这是“代码 1”(将添加到上面“新代码”部分中的序言中):

\fancypagestyle{chapterstart}{% 1st page of the chapters, i.e. no new page for chapter
}
\makeatletter

\def\chapter{
\pagestyle{chapterstart}
\secdef\@chapter\@schapter}

\renewcommand\thesection{\@arabic\c@section} 

\makeatother

此代码允许我在同一页上开始新的章节,并且还将页眉和页脚保留在同一页上。但是这会重置每个章节的章节编号。例如,“第 2 章,第 2.1 节”将改为“第 2 章,第 1.1 节”。

这是“代码 2”(与以前一样,将添加在上面):

\usepackage{etoolbox}
\makeatletter
\patchcmd{\chapter}{\if@openright\cleardoublepage\else\clearpage\fi}{}{}{}
\makeatother

此代码允许我在同一页上开始一个章节,并保留原始的章节编号,例如“第 2 章,第 2.1 节”将保留为“第 2 章,第 2.1 节”。但是我的页眉和页脚不会包含在同一页上

目标

如何结合“代码 1”和“代码 2”来实现:在同一页面上开始新的章节,将页眉和页脚保留在新的章节页面上,并保留章节编号的格式。

答案1

代码 1 无需 即可运行\renewcommand\thesection{\@arabic\c@section}。这省略了章节编号(原始内容为\thechapter.\@arabic\c@section)。

但是有一个缺点,它没有设置\@afterindentfalse,这会导致在章节标题后正常文本的第一个段落缩进。

这行\pagestyle{chapterstart}不是必须的。原始\chapter设置\thispagestyle{plain}。没有这个,页面样式不会改变。

在代码 2 中用\chapter进行更改\patchcmd,这样做的好处是,如果命令发生更改并且无法再修补,则可以发出警告或错误(最后一个参数,代码中的示例)。

\documentclass[12pt, oneside]{book}

\usepackage{sectsty,fancyhdr}
\sectionfont{\LARGE\bfseries}
\usepackage{geometry}

\usepackage{lipsum}%remove 


% *******************************Margin Settings*******************************

\geometry{% margin settings, has to go first before \pagestyle{}
    paper=a4paper, 
    inner=2.5cm, % Inner margin
    outer=2.5cm, % Outer margin
    bindingoffset=0.0cm, % Binding offset
    top=2.5cm, % Top margin
    bottom=2.5cm, % Bottom margin
    headsep=3.5mm,% controles header  
    footskip=6.4mm,% controles footer
    %showframe,% show how the type block is set on the page
}

% ***************************Header/Footer Settings****************************

\sectionfont{\LARGE\bfseries}%this will keep the formattings of sections
\pagestyle{fancy}
\fancyhf{} % sets all head and foot elements empty.
\rhead{} % right side of upper footer 
\lhead{Project title} %left side 
\renewcommand{\headrulewidth}{2.0pt} % Width of the top line
\renewcommand{\footrulewidth}{1.0pt} % Bottom line 
\setlength{\headheight}{14.5pt}%used to remove the \fancyhf{} \headheight error
\rfoot{Page \thepage} % vice versa with the page number 
\lfoot{\leftmark} % Should show the name of section 

\makeatletter
\renewcommand{\sectionmark}[1]{%this avoids the \makecaptical of footers
  \markboth{\ifnum \c@secnumdepth>\z@ %this allowes symbols in foorters
      \thesection\hskip 1em\relax
    \fi #1}{}}
\makeatother


%****************************ADD NEW CODE HERE**********************



%***************************Title************************************
\newcommand{\mytitle}{
\begin{center}
{\Huge{\textbf{Project of...}}} 
\end{center}

\begin{center}
\textsc{\Large My name}\\[0.5cm] 
\end{center}

\begin{center}
{\large My ID number}\\[0.5cm] 
\end{center}
}

%------------------------------------------------------------------
% Code 2
\usepackage{etoolbox}
\makeatletter
% no new page for \chapter
\patchcmd{\chapter}{\if@openright\cleardoublepage\else\clearpage\fi}{}{}{}
% don't change the pagestyle
\patchcmd{\chapter}{\thispagestyle{plain}}{}{}{%
    % example for a warning, 'Package' in text necessary to make TexStudio show it.
    \GenericWarning{(preamble)\@spaces\@spaces\@spaces\@spaces}{Package preamble Warning: patching \string\chapter\space did not work.}}
}
% allow floats on top of the page with a new chapter
\patchcmd{\chapter}{\global\@topnum\z@}{}{}{}
% if not commented out, first paragraph will be indented
%\patchcmd{\chapter}{\@afterindentfalse}{}{}{}
%\makeatother

%------------------------------------------------------------------

% Begin 

%------------------------------------------------------------------

\begin{document}

\mytitle
\chapter{chapter 1}
\section{section 1.1}
\subsection{subection 1.1.1}
  \lipsum%remove

\chapter{chapter 2}
\section{section 2.1}
\subsection{subection 2.1.1}
  \lipsum%remove

%------------------------------------------------------------------

% end 

%------------------------------------------------------------------ 


\end{document}  

相关内容