重新定义 \thepage 并与独立版本兼容

重新定义 \thepage 并与独立版本兼容

背景故事

我目前正在处理一个文档,这个文档太大了,所以把章节分成了单独的文档,使用standalone。在原始文档中,我使用的是章节页码。

问题

为了\thepage使用章节页面格式,我使用:

%% Makes the Page Numbers Chapter-Page
\makeatletter
\appto{\mainmatter}{\renewcommand*{\thepage}{\thechapter-\arabic{page}}}
\patchcmd{\@chapter}{\if@mainmatter}{\if@mainmatter\setcounter{page}{1}}{}{}
% If \backmatter is used
\appto{\backmatter}{%
  \renewcommand*{\thepage}{\arabic{page}}%
  \setcounter{page}{1}%
}
\makeatother

当编译独立章节时,这可以正常工作,但是主文档由于“不完整的 \iffalse”而无法编译,我找不到它,但已将范围缩小到页码修改。

我尝试将 \thepage 修改放在里面\ifstandalone ... \fi,但是在编译 master 时也失败了,但在独立章节中却运行良好。

我正在使用 pdflatex。

任何帮助或建议都将不胜感激!我意识到我可以删除 mod 并在编译主文件时获得独立章节的正常页码和章节页码。这现在更像是一件令人好奇的事情……

主人的 MWE:

\documentclass[11pt, a4paper,titlepage,twoside,openleft]{book}
\usepackage[subpreambles=false]{standalone}
\usepackage[headsep=0.2cm,outer=2.5cm,inner=3.5cm]{geometry}
\usepackage{blindtext}
\usepackage{amsmath}
\usepackage{etoolbox}
\usepackage{fancyhdr}
\usepackage{ifthen}

%% Makes the Page Numbers Chapter-Page
\makeatletter
\appto{\mainmatter}{\renewcommand*{\thepage}{\thechapter-\arabic{page}}}
\patchcmd{\@chapter}{\if@mainmatter}{\if@mainmatter\setcounter{page}{1}}{}{}
% If \backmatter is used
\appto{\backmatter}{%
  \renewcommand*{\thepage}{\arabic{page}}%
  \setcounter{page}{1}%
}
\makeatother

\begin{document}
\mainmatter
\include{standalone_chapter1}
\include{standalone_chapter2}
\end{document}

MWE 独立章节:

\documentclass[class=book,crop=false,float=false]{standalone}
\usepackage[headsep=0.2cm,outer=2.5cm,inner=3.5cm]{geometry}
\usepackage{blindtext}
\usepackage{amsmath}
\usepackage{etoolbox}
\usepackage{fancyhdr}
\usepackage{ifthen}


%% Makes the Page Numbers Chapter-Page
\makeatletter
\appto{\mainmatter}{\renewcommand*{\thepage}{\thechapter-\arabic{page}}}
\patchcmd{\@chapter}{\if@mainmatter}{\if@mainmatter\setcounter{page}{1}}{}{}
% If \backmatter is used
\appto{\backmatter}{%
  \renewcommand*{\thepage}{\arabic{page}}%
  \setcounter{page}{1}%
}
\makeatother
\begin{document}
\pagestyle{fancy}
\fancyhead[LO,RE]{\leftmark}
\fancyfoot[C]{\thepage}
\chapter{Interesting topic}
Some really interesting content that won't make you yawn.
\end{document}

答案1

这是一个 hack,但你可以移动有问题的代码 \begin{document}在包含的文件中。

我用来filecontents*使示例自成一体。

\begin{filecontents*}{\jobname_chapter1}
\documentclass[class=book,crop=false,float=false]{standalone}
\usepackage[headsep=0.2cm,outer=2.5cm,inner=3.5cm]{geometry}
\usepackage{blindtext}
\usepackage{amsmath}
\usepackage{etoolbox}
\usepackage{fancyhdr}
\usepackage{ifthen}


\begin{document}
%% Makes the Page Numbers Chapter-Page
\makeatletter
\appto{\mainmatter}{\renewcommand*{\thepage}{\thechapter-\arabic{page}}}
\patchcmd{\@chapter}{\if@mainmatter}{\if@mainmatter\setcounter{page}{1}}{}{}
% If \backmatter is used
\appto{\backmatter}{%
  \renewcommand*{\thepage}{\arabic{page}}%
  \setcounter{page}{1}%
}
\makeatother

\pagestyle{fancy}
\fancyhead[LO,RE]{\leftmark}
\fancyfoot[C]{\thepage}
\chapter{Interesting topic}
Some really interesting content that won't make you yawn.
\end{document}
\end{filecontents*}


\documentclass[11pt, a4paper,titlepage,twoside,openleft]{book}
\usepackage[subpreambles=false]{standalone}
\usepackage[headsep=0.2cm,outer=2.5cm,inner=3.5cm]{geometry}
\usepackage{blindtext}
\usepackage{amsmath}
\usepackage{etoolbox}
\usepackage{fancyhdr}
\usepackage{ifthen}

%% Makes the Page Numbers Chapter-Page
\makeatletter
\appto{\mainmatter}{\renewcommand*{\thepage}{\thechapter-\arabic{page}}}
\patchcmd{\@chapter}{\if@mainmatter}{\if@mainmatter\setcounter{page}{1}}{}{}
% If \backmatter is used
\appto{\backmatter}{%
  \renewcommand*{\thepage}{\arabic{page}}%
  \setcounter{page}{1}%
}
\makeatother

\begin{document}

\mainmatter

\include{\jobname_chapter1}

\end{document}

或者,按照建议,隐藏条件

%% Makes the Page Numbers Chapter-Page
\makeatletter
\appto{\mainmatter}{\renewcommand*{\thepage}{\thechapter-\arabic{page}}}
\expandafter\patchcmd\expandafter\@chapter\expandafter
  {\csname if@mainmatter\expandafter\endcsname\expandafter}\expandafter
  {\csname if@mainmatter\endcsname\setcounter{page}{1}}{}{}
% If \backmatter is used
\appto{\backmatter}{%
  \renewcommand*{\thepage}{\arabic{page}}%
  \setcounter{page}{1}%
}
\makeatother

最简单的方法是建立一个简单的mypatches.sty文件

\ProvidesPackage{mypatches}
\RequirePackage{etoolbox}

\appto{\mainmatter}{\renewcommand*{\thepage}{\thechapter-\arabic{page}}}
\patchcmd{\@chapter}{\if@mainmatter}{\if@mainmatter\setcounter{page}{1}}{}{}
% If \backmatter is used
\appto{\backmatter}{%
  \renewcommand*{\thepage}{\arabic{page}}%
  \setcounter{page}{1}%
}

并加载它。

相关内容