同じページで章を開始し、ヘッダーとフッターを維持する方法 (すでにコードが使用可能)

同じページで章を開始し、ヘッダーとフッターを維持する方法 (すでにコードが使用可能)

設定

現時点では、私のプロジェクトの最初のページと全体的なレイアウトのコードは次のとおりです。

\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}  

関連情報