自動刪除一頁文件中的頁碼

自動刪除一頁文件中的頁碼

我發現 LaTeX 在一頁文件中放置頁碼的選擇在風格上很奇怪,所以我想禁止這種行為。我以前做過這個手動,但最好不需要追蹤文件的長度。

問題已被問到,但 OP 的首選解決方案涉及玩\maketitle,這與我無關。所接受的回答建立一個需要遵循的宏\begin{doucument},但最好有一些可以放入我的序言或.sty文件中的東西,這樣我就不需要記住總是在我編寫的每個文件中放入一些自訂巨集。

微量元素:

\documentclass{article}
\usepackage{lipsum,fancyhdr}
    \pagestyle{fancy}
    \fancyhead[C]{Don't ever remove this header}
\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}

答案1

有些軟體包可以或多或少計算頁數(totpagescount1tolastpage、 ...)。此範例使用套件zref-totpages

\documentclass{article}

\usepackage{zref-totpages}

\usepackage{lipsum,fancyhdr}
\pagestyle{fancy}
\fancyhead[C]{Don't ever remove this header}
\fancyfoot[C]{%
  \ifnum\ztotpages=1 \else\thepage\fi
}

\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}

需要兩次 LaTeX 運行,因為當時,當第一頁被運出時,通常不知道接下來會有多少頁。

\ztotpageszref當標籤LastPage沒有記錄在檔案中時,第一次執行 LaTeX 時為零.aux

答案2

如果我理解正確的話,您想刪除第一頁上的頁碼,但保留標題。這可以透過定義與預設樣式相同fancy但沒有頁碼的自訂 fancyhdr 樣式來完成,並且僅將其用於第一頁。完整的 MWE:

\documentclass{article}
\usepackage{lipsum,fancyhdr}
    \pagestyle{fancy}
    \fancyhead[C]{Don't ever remove this header}
\fancypagestyle{mystyle}{
    \fancyhf{}
    \fancyhead[C]{Don't ever remove this header}
    \fancyfoot[C]{}
}
\begin{document}
\thispagestyle{mystyle}
\lipsum[1] % automatically remove page number in a document with this line
\pagestyle{fancy}
\lipsum[1-6] % automatically keep page numbers in a document with this line
\end{document}

勝利!使用lastpage和使自動化部分正常工作ifthen。簡而言之,它會檢查文件長度(由 確定lastpage)是否超過 1 頁。如果文件為一頁,則mystyle在有頁碼的地方使用該樣式。但是,如果文件長度超過一頁,則第一頁將使用該樣式mystyle,後續頁面將使用該fancy樣式。

\documentclass{article}
\usepackage{lipsum,fancyhdr,ifthen,lastpage}
\pagestyle{fancy}
\fancyhead[C]{Don't ever remove this header}
\fancypagestyle{mystyle}{
    \fancyhf{}
    \fancyhead[C]{Don't ever remove this header}
    \fancyfoot[C]{}
}
\begin{document}
\ifthenelse{\pageref{LastPage}=1}
{
    \pagestyle{mystyle}
}
{
    \thispagestyle{mystyle}
}
\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}

現在唯一的問題是它不能用在序言中...

答案3

帶包的範例lastpage

\documentclass{article}
\usepackage{lastpage}
\usepackage{refcount}
\usepackage{etoolbox}
\usepackage{lipsum,fancyhdr}
\pagestyle{fancy}
\fancyhead[C]{Don't ever remove this header}
\begin{document}
\ifnumcomp{\getpagerefnumber{LastPage}}{>}{2}{}{\fancyfoot{}} 
 % If document is longer than 2 pages there will be page numeration, if not there will     not be.
\lipsum[1] % automatically remove page number in a document with this line
\end{document}

答案4

晚上好,我們可以測試文件末尾的頁碼。如果只有一頁,它將清除中心的頁腳,否則它會繼續前進而不更改預設值,並且頁碼保持不變。

\documentclass{article}
\usepackage{lipsum,fancyhdr}
\pagestyle{fancy}
\fancyhead[C]{Don't ever remove this header}
\begin{document}
\lipsum[1]
%\lipsum[1-6] % Turn on/off this line...
\ifnum\thepage=1\fancyfoot[C]{}\fi
\end{document}

相關內容