不要计算插入的空白页,但要跟踪它们

不要计算插入的空白页,但要跟踪它们

我有一个问题MWE,嗯,更确切地说WE,因为最低限度不再适用于它了......

首先让我告诉你我期望的输出是什么。因此,对于文档twoside,我需要文档的前几页(标题页、摘要、目录等)是单面的,这意味着内容应该打印到正确的页面上(背面),然后是空白页(右页)。对于文档的结尾,我需要从背面. 附录的每一节也同样如此。

我能够通过定义一些自定义命令(如\generateblanks和 重新定义 )来实现所有这些功能\cleardoublepage。我仍然遇到的问题是,从逻辑上讲,空白页会被计算在内。由于多种原因,我希望页面输出仅考虑包含内容的页面。我可以简单地添加\addtocounter{page}{-1}生成空白页的相应命令(\generateblanks\cleardoublepage),但由于这会修改实际的页计数器,TeX 在解析文档时会感到困惑。这将导致 出现故障\cleardoublepage

这个想法是只修改输出的页码。在研究过程中,我发现TeX.SE 上的这个主题。这里,David 建议定义一个blankpages计数器,当插入空白页时,计数器的值会增加。然后,重新定义命令以从计数器中\thepage减去的值,这将导致打印所需的页码,同时仍跟踪“真实”页码。blankpagespage

不幸的是,这在我的例子中不起作用。TeX 对这些命令非常不感兴趣,似乎只是忽略了它们。我怀疑这是由于使用\pagenumbering{}似乎\thepage也重新定义的命令造成的。

您将如何解决这个问题?是否可以定义第二个页面计数器,该计数器每次实际page计数器增加时都会增加?这样我就可以摆弄第二个计数器,而不会干扰 TeX。


我们

\documentclass[twoside]{article}

% %%%%%%%%
% Packages
% %%%%%%%%

\usepackage{lipsum}
\usepackage{appendix}
\usepackage{fancyhdr}
\usepackage{xparse}
\usepackage{atbegshi}
\usepackage{geometry}
\usepackage[backend = biber]{biblatex}



% %%%%%%%%%%%%%%%%%%%%%%%%%%
% Set internal two-side Bool
% %%%%%%%%%%%%%%%%%%%%%%%%%%

\newbool{twosideDocument}
{\makeatletter
\if@twoside
    \global\setbool{twosideDocument}{true}
\else
    \global\setbool{twosideDocument}{false}
\fi
\makeatother}



% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Accentuated geometry to easily
% distinguish between left and right
% (even / odd) pages.
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\geometry{%
    a4paper,%
    top = 3cm,%
    bottom = 3.5cm,%
    outer = 2.5cm,%
    nomarginpar,%
    showframe = false%
}

\ifbool{twosideDocument}{%
    \geometry{%
        inner = 4.5cm%
    }%
}{%
    \geometry{%
        inner = 2.5cm%
    }%
}



% %%%%%%%%%%%%%%%%%%%%%%%%%%
% Define a custom command to
% generate blank pages
% %%%%%%%%%%%%%%%%%%%%%%%%%%

\newbool{nextblank}
\newcounter{blankpages}

\newcommand{\generateblanks}{%
    \ifbool{nextblank}%
    {%
        \thispagestyle{empty}%
        \setcounter{blankpages}{1}
        \mbox{}%
        \clearpage%
        \global\setbool{nextblank}{false}
    }%
    {%
        \thispagestyle{noheader}
        \global\setbool{nextblank}{true}
    }%
}



% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Redefine the cleardoublepage command
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\makeatletter
\renewcommand*{\cleardoublepage}{%
    \clearpage%
    \ifbool{twosideDocument}{%
        \ifodd%
            \c@page%
        \else%
            \hbox{}%
            \thispagestyle{empty}%
            \setcounter{blankpages}{1}
            \hbox{}%
            \newpage%
            \if@twocolumn%
                \hbox{}%
                \newpage%
            \fi%
        \fi%
    }{}%
}
\makeatother



% %%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Define different page styles
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%

\fancypagestyle{noheader}{
    \fancyhf{} % Clean fields
    \renewcommand{\headrulewidth}{0pt}
    \renewcommand{\footrulewidth}{0.4pt}
    \ifbool{twosideDocument}{
        \fancyfoot[LE, RO]{\thepage}
    }{
        \fancyfoot[R]{\thepage}
    }
}

\fancypagestyle{general}{
    \fancyhf{} % Clean fields
    \renewcommand{\headrulewidth}{0.4pt}
    \renewcommand{\footrulewidth}{0.4pt}
    \ifbool{twosideDocument}{
        \fancyhead[LE,RO]{\itshape\nouppercase{\rightmark}}
        \fancyfoot[LE,RO]{\thepage}
    }{
        \fancyhead[R]{\itshape\nouppercase{\rightmark}}
        \fancyfoot[R]{\thepage}
    }
}

\fancypagestyle{appendix}{%
    \fancyhf{} % Clean fields
    \renewcommand{\headrulewidth}{0.4pt}
    \renewcommand{\footrulewidth}{0.4pt}
    \ifbool{twosideDocument}{
        \fancyhead[LE,RO]{\itshape\nouppercase{\leftmark}}
        \fancyfoot[LE,RO]{\thepage}
    }{
        \fancyhead[R]{\itshape\nouppercase{\leftmark}}
        \fancyfoot[R]{\thepage}
    }
}



% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Define some page numbering commands
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcommand{\pagenumberAlph}{%
    \newpage%
    \thispagestyle{empty}%
    \pagestyle{empty}%
    \pagenumbering{Alph}%
}

\newcommand{\pagenumberRoman}{%
    \newpage%
    \global\setbool{nextblank}{true}%
    \ifbool{twosideDocument}{%
        \AtBeginShipout{\generateblanks}%
    }{}%
    \thispagestyle{noheader}%
    \pagestyle{noheader}%
    \pagenumbering{roman}%
}

\newcommand{\pagenumberArabic}{%
    \newpage%
    \global\let\generateblanks\relax % Relax the blank pages function
    \thispagestyle{general}%
    \pagestyle{general}%
    \pagenumbering{arabic}%
    \setcounter{section}{0}%
}



% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Redefine some commands to start them
% on the verso page
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\let\tableofcontentsCopy\tableofcontents
\renewcommand{\tableofcontents}{%
    \cleardoublepage%
    \tableofcontentsCopy%
    \clearpage%
}

\let\printbibliographyCopy\printbibliography
\renewcommand{\printbibliography}{%
    \cleardoublepage%
    \addcontentsline{toc}{section}{\refname}%
    \pagenumbering{Roman}%
    \printbibliographyCopy%
}

\AtBeginEnvironment{appendices}{%
    \cleardoublepage%
    \thispagestyle{appendix}%
    \pagestyle{appendix}%
    \let\oldSection\section%
    \renewcommand{\section}[1]{%
        \cleardoublepage%
        \oldSection{#1}%
    }%
}



% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Redefine the \thepage command in order 
% to output the desired page number
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\renewcommand\thepage{\the\numexpr\value{page}-\value{blankpages}\relax}



% %%%%%%%%%%%%%%%
% Add a Reference
% %%%%%%%%%%%%%%%

\begin{filecontents}{references.bib}
    @misc{Test,
        author = {John Doe},
        title = {{This is a test title}},
        year = {2019},
        url = {https://example.com},
        urldate = {2019-05-21}
    }
\end{filecontents}
\addbibresource{references.bib}



% %%%%%%%%%%%%%%%%%
% Add document info
% %%%%%%%%%%%%%%%%%

\title{Test Document}
\author{John Doe}
\date{\today}



% %%%%%%%%%%%%%%%%%%
% Start the document
% %%%%%%%%%%%%%%%%%%

\begin{document}

    \pagenumberAlph
    \maketitle

    \pagenumberRoman
    \begin{abstract}
        \lipsum[1-2]
    \end{abstract}

    \tableofcontents

    \pagenumberArabic
    \section{Foo}
    \lipsum[1-4]\cite{Test}

    \section{Bar}
    \lipsum[5-9]

    \printbibliography

    \begin{appendices}
        \section{Foo}
        \lipsum[1]

        \section{Bar}
        \lipsum[2-10]
    \end{appendices}

\end{document}

答案1

这是一个解决方案。这个想法是使用类似于abspage来自zref包、模块:abspage 的计数器。

\xpatchcmd{\@outputpage}{\count\z@}{\c@abspage}{}{}有了这个,边距和标题是正确的,我们就可以正确地定义命令\cleartoright

\newcommand*{\cleartoright}{%
    \clearpage%
    \ifodd\c@abspage\else
        \hbox{}
        \addtocounter{page}{-1}
        \thispagestyle{empty}
        \clearpage
    \fi}

以下是完整的代码

\documentclass[twoside]{article}

\usepackage{appendix}
\usepackage{biblatex}
\usepackage{fancyhdr}
\usepackage{xpatch}
\usepackage[user,abspage]{zref}

\fancypagestyle{noheader}{
    \fancyhf{} % Clean fields
    \renewcommand{\headrulewidth}{0pt}
    \renewcommand{\footrulewidth}{0.4pt}
    \fancyfoot[R]{\thepage}
}

\fancypagestyle{general}{
    \fancyhf{} % Clean fields
    \renewcommand{\headrulewidth}{0.4pt}
    \renewcommand{\footrulewidth}{0.4pt}
    \fancyhead[LE,RO]{\itshape\nouppercase{\rightmark}}
    \fancyfoot[LE,RO]{\thepage}
}

\fancypagestyle{appendix}{%
    \fancyhf{} % Clean fields
    \renewcommand{\headrulewidth}{0.4pt}
    \renewcommand{\footrulewidth}{0.4pt}
    \fancyhead[LE,RO]{\itshape\nouppercase{\leftmark}}
    \fancyfoot[LE,RO]{\thepage}
}

\usepackage{mwe}

\makeatletter 
\newcommand*{\cleartoright}{%
    \clearpage%
    \ifodd\c@abspage\else
        \hbox{}
        \addtocounter{page}{-1}
        \thispagestyle{empty}
        \clearpage
    \fi}
\xpatchcmd{\@outputpage}{\count\z@}{\c@abspage}{}{}
\makeatother
\xapptocmd{\appendices}{\newpage\thispagestyle{appendix}\pagestyle{appendix}}{}{}
\xapptocmd{\endappendices}{\newpage}{}{}
\xpretocmd{\section}{\presection}{}{\err}
\newcommand*{\Romanpages}{%
\cleartoright
\pagenumbering{roman}
\thispagestyle{noheader}
\pagestyle{noheader}
\let\presection\cleartoright}
\newcommand*{\Arabicpages}{%
\clearpage % or something
\pagenumbering{arabic}
\thispagestyle{general}
\pagestyle{general}
\let\presection\empty}

\begin{filecontents}{references.bib}
    @misc{Test,
        author = {John Doe},
        title = {{This is a test title}},
        year = {2019},
        url = {https://example.com},
        urldate = {2019-05-21}
    }
\end{filecontents}
\addbibresource{references.bib}

\title{Test Document}
\author{John Doe}
\date{\today}

\begin{document}
\maketitle
%------------------------
\Romanpages
%------------------------
\begin{abstract}
\lipsum[1-2]
\end{abstract}

\tableofcontents
%------------------------
\Arabicpages
%------------------------
\blinddocument
\lipsum
\cite{Test}
\blinddocument

%------------------------
\Romanpages
%------------------------
\printbibliography

\begin{appendices}
\section{Foo}
\lipsum[1]

\section{Bar}
\lipsum[2-10]
\end{appendices}

\end{document}

相关内容