
LaTeXが1ページの文書にページ番号を入れるという選択はスタイル的に奇妙だと思うので、その動作を抑制したい。私は以前これをやったことがある。手動でただし、文書の長さを自分で追跡する必要がない方がよいでしょう。
質問は出されました前にですが、OPが推奨する解決策は で遊ぶことですが\maketitle
、これはここでは私にとっては関係ありません。受け入れられた答えは、の後に必要なマクロを作成します\begin{doucument}
が、作成するすべてのドキュメントに常にカスタム マクロを配置することを覚えておく必要がないように、プリアンブルまたはファイルに配置できるものがあったほうがよい.sty
でしょう。
MWE:
\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
ページ数を多かれ少なかれカウントできるパッケージがいくつかあります ( totpages
、、、...)。例ではパッケージ を使用しcount1to
ます:lastpage
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 の実行は 2 回必要です。
\ztotpages
zref
ラベルがファイル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 ページを超えているかどうかを確認します。ドキュメントが 1 ページの場合、ページ番号がある場所で スタイルmystyle
が使用されます。ただし、ドキュメントが 1 ページより長い場合は、最初のページが を使用し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
こんばんは。ドキュメントの最後にあるページ番号をテストできます。ページが 1 ページしかない場合は、中央のフッターがクリアされます。それ以外の場合は、デフォルト値を変更せずに進み、ページ番号はそのまま残ります。
\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}