試著確保頁碼是四的倍數時交替頁數

試著確保頁碼是四的倍數時交替頁數

我想確保我的頁數是四的倍數。如果它不是四的倍數,我想添加,\newpage\begin{center}\textbf{BLANK PAGE}\end{center}直到頁數達到四的倍數。我最初嘗試使用文件中記錄的 expl3 解決方案這個答案具有以下 MWE:

\documentclass{article}
\usepackage{refcount, lastpage}
\newcommand{\blankpage}{\newpage\begin{center}\textbf{BLANK PAGE}\end{center}}
\ExplSyntaxOn
\NewDocumentCommand{\ensurefour} { }
 {
  \prg_replicate:nn
   { \int_mod:nn { 4 - \int_mod:nn { \getpagerefnumber{LastPage} } { 4 } } { 4 } }
   { \blankpage }
 }
\ExplSyntaxOff
\begin{document}
Test\newpage

\ensurefour
\end{document}

當我第一次使用任何引擎編譯它時,它會產生一個頁面的輸出,也就是一個顯示「測試」的頁面。當我第二次編譯它時,它會產生四頁的輸出,這是所需的輸出。

但當我第三次繼續編譯時,沒有刪除.aux文件,頁碼又回到了1。

這是因為,在第一次編譯期間,LaTeX 會將以下內容寫入 .aux 檔案:

\relax 
\newlabel{LastPage}{{}{1}}
\xdef\lastpage@lastpage{1}
\gdef\lastpage@lastpageHy{}
\gdef \@abspage@last{1}

因此,在第二次編譯中,\getpagerefnumber{LastPage}看到“1”,並\ensurefour繼續添加3頁,使總共4頁。但隨後它發現生成了四個頁面,因此它寫入 aux 檔案:

\relax 
\newlabel{LastPage}{{}{4}}
\xdef\lastpage@lastpage{4}
\gdef\lastpage@lastpageHy{}
\gdef \@abspage@last{4}

這導致第三次編譯認為已經有四頁,因此不再添加更多頁面,只剩下一頁......等等。

問題:有沒有辦法讓頁碼收斂到4,而不需要在1和4之間交替?

答案1

不幸的是,該套件lastpage改變了管理標籤的方式,現在它使用了 hook afterlastpage

您可以發行\clearpage並使用 的當前值page

\documentclass{article}

\newcommand{\blankpage}{\begin{center}\textbf{BLANK PAGE}\end{center}\clearpage}

\ExplSyntaxOn
\NewDocumentCommand{\ensurefour} { }
 {
  \clearpage
  \prg_replicate:nn
   { \int_mod:nn { 4 - \int_mod:nn { \value{page}-1 } { 4 } } { 4 } }
   { \blankpage }
 }
\ExplSyntaxOff
\AtEndDocument{\ensurefour}

\begin{document}

Test
%\newpage
test
%\newpage
test
%\newpage
test

\end{document}

取消註釋\newpage命令將顯示正確的結果。

答案2

您無需將標籤新增到最後一頁。巨集\ensurefor可以\vfill\break先運行(或者\vfill\supereject如果文件中有浮動)。然後最後一個真實頁面完成,您確定它\pageno等於 n+1,其中 n 是真實頁面的數量。您可以使用暫存器來新增所需的銀行頁面\pageno

此解決方案的另一個優點是您無需等待第二次 TeX 運行即可獲得正確的頁數。

我可以向您展示如何在 OpTeX 中執行此操作。 LaTeX 解決方案可以類比。

\newcount\restpages
\def\ensurefour{\vfill\break
   \restpages = \expr[0]{ 4 - (\the\pageno-1)\%4 }\relax
   \ifnum\restpages=4 \restpages=0 \fi
   \fornum 1..\restpages \do{\null\vfill\break}
}

Test:
\lipsum[1-10]

\ensurefour

\bye

答案3

一個快速但骯髒的解決方案是標記 LastPage (或等效的) \ensurefour產生額外的頁面。例如:

\documentclass{article}
\usepackage{refcount}
\newcommand{\blankpage}{\newpage\begin{center}\textbf{BLANK PAGE}\end{center}}
\ExplSyntaxOn
\NewDocumentCommand{\ensurefour} { }
        {
                \label{LastPageBeforeEnsureFour}
                \prg_replicate:nn
                { \int_mod:nn { 4 - \int_mod:nn { \getpagerefnumber{LastPageBeforeEnsureFour} } { 4 } } { 4 } }
                { \blankpage }
        }
\ExplSyntaxOff
\begin{document}
Test

\ensurefour
\end{document}

答案4

我產生頁數為四的倍數的列印文檔,並且我想在最後一頁或最後兩頁上放置一些特定內容。以下是我為此目的編寫的命令。

我透過取得目前實體頁面的數量\ReadonlyShipoutCounter(已建立的完整頁面的數量,新增 1 以考慮目前頁面)

\newcommand{\theabsolutepage}{\inteval{\ReadonlyShipoutCounter+1}}

我定義了一個\blankpage允許設定頁面樣式的命令(\blankpage[plain]如果您想要plain空白頁面的頁面樣式)

\NewDocumentCommand{\blankpage}{ O{empty} }
% #1: page style for the blank page
{
  \clearpage{}\thispagestyle{#1}\null\clearpage{}
}

\clearpagestoend以及取得所需空白頁數的命令

\NewDocumentCommand{\clearpagestoend}{ O{0} O{empty} }
% #1: number of reserved pages at end
% #2: page style for blank pages
{
  \clearpage{\pagestyle{#2}}
  \count255=\theabsolutepage \advance \count255 by #1 \advance \count255 by -1
  \count254=\count255 
  \divide \count254 by 4 \multiply \count254 by 4
  \advance \count255 by -\count254
  \ifcase\count255  % pages nb % 4 == 0: do nothing
    \relax
  \or               % pages nb % 4 == 1: skip 3 pages
    \blankpage[#2]  \blankpage[#2] \blankpage[#2]
  \or               % pages nb % 4 == 2: skip 2 pages
    \blankpage[#2]  \blankpage[#2]
  \or               % pages nb % 4 == 3: skip 1 page
    \blankpage{\pagestyle{#2}}
  \fi
}

例如

\clearpagestoend[2][plain]

產生空白頁數,以獲得plain頁面樣式的頁數為 4 的倍數減 2 頁。

完整範例:

\documentclass{article}

\usepackage{lipsum}

\newcommand{\theabsolutepage}{\inteval{\ReadonlyShipoutCounter+1}}

\NewDocumentCommand{\blankpage}{ O{empty} }
% #1: page style for the blank page
{
  \clearpage{}\thispagestyle{#1}\null\clearpage{}
}

\NewDocumentCommand{\clearpagestoend}{ O{0} O{empty} }
% #1: number of reserved pages at end
% #2: page style for blank pages
{
  \clearpage{\pagestyle{#2}}
  \count255=\theabsolutepage \advance \count255 by #1 \advance \count255 by -1
  \count254=\count255
  \divide \count254 by 4 \multiply \count254 by 4
  \advance \count255 by -\count254
  \ifcase\count255  % pages nb % 4 == 0: do nothing
    \relax
  \or               % pages nb % 4 == 1: skip 3 pages
    \blankpage[#2]  \blankpage[#2] \blankpage[#2]
  \or               % pages nb % 4 == 2: skip 2 pages
    \blankpage[#2]  \blankpage[#2]
  \or               % pages nb % 4 == 3: skip 1 page
    \blankpage{\pagestyle{#2}}
  \fi
}

\begin{document}

\lipsum

\clearpagestoend

\end{document}

相關內容