如何在條件式中使用總頁數?

如何在條件式中使用總頁數?

我希望根據文件是否有一頁或多頁而有不同的頁腳或頁面樣式。我對 TeX 程式設計不太有經驗,所以答案關於“當參數是 etoolbox 命令的結果時,將參數與字串進行比較”並沒有給我太大幫助“何時使用 \edef、\noexpand 和 \expandafter?”對我來說第一次有點複雜。

我嘗試了這個,但沒有成功:

\documentclass{article}
\usepackage{lipsum}
\usepackage{etoolbox}
\usepackage{lastpage}
\begin{document}

\expandafter\ifstrequal{\pageref*{LastPage}}{1}{
  \pagestyle{empty}
  }{
  \pagestyle{plain}}

\lipsum[2-3]

\end{document}

我可能會繼續嘗試。

有人可以告訴我一個好方法嗎?

答案1

lastpage您可以使用totcount包代替。

\documentclass{article}
\usepackage{lipsum}
\usepackage{etoolbox}
\usepackage{totcount}
\regtotcounter{page}

\begin{document}

\ifnumcomp{\totvalue{page}}{>}{1}{\pagestyle{plain}}{\pagestyle{empty}}

\lipsum[2]

\end{document}

在此輸入影像描述

這需要運行 2 次編譯才能穩定下來。

答案2

我對文件中的頁碼有同樣的問題,並找到了幾種解決方案。

透過借用 Ulrike Fisher 的回答中的一些程式碼這個問題我能夠定義這個巨集:

\documentclass{article}
\usepackage{lipsum,ifthen}
\usepackage[lastpage]{zref}

\makeatletter
\zref@newprop*{numpage}{\the\value{page}}
\zref@addprop{main}{numpage}
\newcommand{\oneormorepages}%
    {\ifthenelse{\zref@extractdefault{LastPage}{numpage}{1}>1}%
        {\thispagestyle{plain}}%
        {\thispagestyle{empty}}%
    }
\makeatother

\title{Test}
\author{Test Testson}

\begin{document}
\maketitle
\oneormorepages
\lipsum[1-60] %More than one page
%\lipsum[1]   % One page
\end{document}

這是我今天使用的結果。我使用from\maketitle即時修補,這是我在“真實”文檔中出於其他目的加載的包:\patchcmdetoolbox

\documentclass{article}
\usepackage{lipsum,etoolbox}

%% No page number  if the document ai a onepager
\makeatletter
\AtEndDocument{%
  \ifnum\value{page} > \@ne
    \immediate\write\@auxout{\global\let\string\@multipage\relax}%
  \fi
}
\newcommand*{\oneormorepages}{%
    \ifdefined\@multipage
        \thispagestyle{plain}%
    \else
        \thispagestyle{empty}%
    \fi
 }
\patchcmd{\maketitle}
    {\thispagestyle{plain}}%
    {\oneormorepages}{}{}
%% Change `plain` to `title` if you are using a `memoir` class
\makeatother

\title{Test}
\author{Test Testson}

\begin{document}
\maketitle
\lipsum[1-60] % More than one page
%\lipsum[1]   % One page
\end{document}

正如egreg在下面的評論中所強調的那樣(以及在對原始答案的評論中),該解決方案並不是百分百的萬無一失(例如,它在 下不起作用scrartcl)。我現在已經糾正了 jfbu 指出的錯誤。

今天我甚至找到了兩個不需要修補的額外解決方案ETC.:

基於中的討論這個問題這個答案對於另一個問題,我已經修補了一個工作解決方案,不需要任何額外的包,並在 KOMAscript 和標準類別下工作。它倖存下來\pagenumbering{Roman}。正如egreg所指出的,它仍然不是萬無一失的,但我嘗試通過從-bundle加載-atendviatveryend-packagesoberdiek並使用這些包中的命令來推遲測試。然後測試失敗。所以對於下面的 MWE,我們必須信任\AtEndDocument

以下是 MWE:

\documentclass{article}
\usepackage{lipsum}
\makeatletter % You may remove this line if you change\@ne to 1
\AtEndDocument{\ifnum\value{page]=\@ne\thispagestyle{empty}{}\fi} % survives `\pagenumbering{Roman}`
\makeatother % You may remove this line if you change\@ne to 1
\title{Test}
\author{Test Testson}

\begin{document}
\maketitle
\lipsum[1]
\lipsum[1-6] % Turn on/off this line...
\end{document}

如果您需要羅馬編號,您也可以載入zref-totpages測試並將其變更為:

\AtEndDocument{\ifnum\ztotpages=\@ne\thispagestyle{empty}{}\fi}

基於這個答案,我找到了一個使用 和 的解決方案scrartclscrpage2zref-totpages也仍然存在\pagenumbering{Roman}。您可以在測試的false和部分中新增附加程式碼:true

\documentclass{scrartcl}
\usepackage{zref-totpages,lipsum,scrpage2}
\pagestyle{scrplain}
\clearscrheadfoot
% You may use \@ne instead of 1 if you enclose the line in a `\makeatletter\makeatother`
\cfoot[\ifnum\ztotpages=1 \else\pagemark\fi]{\pagemark}

\begin{document}

\lipsum[1] % automatically remove page number in a document with this line
%\lipsum[1-6] % automatically keep page numbers in a document with this line
\end{document}

希望它有用。

相關內容