![ページ番号が4の倍数になるように交互にページ数をカウントする](https://rvso.com/image/476229/%E3%83%9A%E3%83%BC%E3%82%B8%E7%95%AA%E5%8F%B7%E3%81%8C4%E3%81%AE%E5%80%8D%E6%95%B0%E3%81%AB%E3%81%AA%E3%82%8B%E3%82%88%E3%81%86%E3%81%AB%E4%BA%A4%E4%BA%92%E3%81%AB%E3%83%9A%E3%83%BC%E3%82%B8%E6%95%B0%E3%82%92%E3%82%AB%E3%82%A6%E3%83%B3%E3%83%88%E3%81%99%E3%82%8B.png)
ページ数が4の倍数であることを確認したいです。4の倍数でない場合は、\newpage\begin{center}\textbf{BLANK PAGE}\end{center}
ページ数が4の倍数になるまで追加したいと思います。私は原始的に、ドキュメントに記載されている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}
最初にどのエンジンでもコンパイルすると、1 ページ (つまり、「Test」という 1 ページ) の出力が生成されます。2 回目にコンパイルすると、目的の出力である 4 ページの出力が生成されます。
しかし、.aux ファイルを削除せずに 3 回目のコンパイルを続けると、ページ番号は 1 に戻ります。
これは、最初のコンパイル時に、LaTeX が .aux ファイルに次の内容を書き込むためです。
\relax
\newlabel{LastPage}{{}{1}}
\xdef\lastpage@lastpage{1}
\gdef\lastpage@lastpageHy{}
\gdef \@abspage@last{1}
したがって、2 回目のコンパイルでは、\getpagerefnumber{LastPage}
「1」が見つかり、\ensurefour
3 ページを追加して合計 4 ページになります。しかし、4 ページが生成されることがわかったので、aux ファイルに次のように書き込みます。
\relax
\newlabel{LastPage}{{}{4}}
\xdef\lastpage@lastpage{4}
\gdef\lastpage@lastpageHy{}
\gdef \@abspage@last{4}
これにより、3 番目のコンパイルではすでに 4 ページあると判断され、それ以上ページは追加されず、1 ページだけが残ります... というように続きます。
質問: ページ番号を 1 と 4 を交互に表示せずに 4 に収束させる方法はありますか?
答え1
残念ながら、パッケージはlastpage
ラベルの管理方法を変更し、現在はフックを使用しています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
レジスタを使用して、必要なバンク ページを追加できます。
このソリューションのもう 1 つの利点は、正しいページ数を得るために 2 回目の 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
私は、ページ数が 4 の倍数で、最後の 1 ページまたは最後の 2 ページに特定のコンテンツを配置する印刷ドキュメントを作成します。この目的のために作成したコマンドは次のとおりです。
現在の物理ページ数を取得します\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]
ページ スタイルを使用して、4 の倍数から 2 ページを引いたページ数を取得するための空白ページ数を生成しますplain
。
完全な例:
\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}