セクションにページが 1 つしかない場合はページ番号を削除します。

セクションにページが 1 つしかない場合はページ番号を削除します。

現在のプロジェクトでは、印刷して配布できるワークシートを作成したいと考えています。これを行うには、各ワークシートのページ番号をリセットしますが、ワークシートに複数のページがある場合 (たとえば、裏ページがある場合) にのみページ番号を表示します。ワークシートに 1 ページしかない場合は、ページ番号を非表示にして、ページ 2 がどこにあるかなどについて不要な混乱がないようにする必要があります。 でこれを行うことはできますかfancyhdr?

これが MWE です。各ワークシートは独自のセクションから始まります。LuaLaTeX を使用していることに注意してください。

\documentclass[a4paper, twoside, 12pt]{extarticle}
\usepackage{graphicx}
\usepackage[left=2.5cm,right=2.5cm,top=3cm,bottom=4cm,headheight=0mm, footskip=10mm]{geometry}
\usepackage[T1]{fontenc}
\usepackage[dvipsnames]{xcolor}

\usepackage{fancyhdr}
\fancyhead{} 
\fancyhead[RO,RE]{Bla}
\fancyhead[LO,LE]{Bla}
\fancyfoot{} 
\fancyfoot[LE,RO]{}
\fancyfoot[LO,CE]{}
\fancyfoot[CO,CE]{\thepage}
\renewcommand{\headrulewidth}{0pt}
\pagestyle{fancy}
\renewcommand{\thesection}{\arabic{section}.}

\makeatletter
\renewcommand*{\@seccntformat}[1]{\csname the#1\endcsname\hspace{0.25cm}}
\makeatother

\definecolor{mainblue}{rgb}{0.0, 0.28, 0.67}
\usepackage[math-style=ISO, bold-style=ISO]{unicode-math}
\setmathfont{Garamond-Math.otf}[StylisticSet={7,9}]

\setlength{\parindent}{0em}
\setlength{\parskip}{0.25\baselineskip}


\usepackage{titlesec}
\titleformat*{\section}{\LARGE\bfseries\sffamily\color{mainblue}\centering}

\usepackage{fontspec}
\setmainfont{EB Garamond} 
\defaultfontfeatures{Scale=MatchUppercase}
\setsansfont{Quattrocento Sans}

\newcommand{\head}[1]{%
\clearpage
\noindent
\vspace*{0.3cm}
\section{#1}
\vspace*{0.3cm}
\setcounter{page}{1}
}

\begin{document}

\head{Only one page}
Please hide page number below.

\head{Two pages}
First page. Please don't hide page number below.

\clearpage

Second page

\end{document}

新しいページスタイルを定義して手動でこれを行うことはできますが、面倒すぎると思います。 すでに 1 つのプロジェクトに統合したいワークシートをたくさん作成していますが、すべてが 1 ページに収まるかどうかを確認するのに時間がかかります。 何かできるアイデアはありますか?

答え1

\label発行直後にページ番号を保存するために使用し\clearpage、次回実行時にページ数が 1 であるかどうかのテストが成功し、\thepageスキップされます。

headheightそれを直しましたできないヘッダーが必要な場合はゼロに設定してください。また、fontencと一緒にロードしないでくださいfontspec。最初にパッケージをロードしてから設定を行うようにプリアンブルを再編成しました。

around\titlespacingではなくを使用します。\vspace\section

\documentclass[a4paper, twoside, 12pt]{article}
\usepackage{graphicx}
\usepackage[
  left=2.5cm,
  right=2.5cm,
  top=3cm,
  bottom=4cm,
  headheight=14.5pt,% <-- as recommended by fancyhdr
  footskip=10mm
]{geometry}
%\usepackage[T1]{fontenc}% NOT with fontspec
\usepackage[dvipsnames]{xcolor}
\usepackage{refcount}

\usepackage{fancyhdr}
\usepackage{titlesec}
\usepackage{fontspec}
\usepackage[math-style=ISO, bold-style=ISO]{unicode-math}

%%% fancyhdr

\fancyhead{} 
\fancyhead[RO,RE]{Bla}
\fancyhead[LO,LE]{Bla}
\fancyfoot{} 
\fancyfoot[LE,RO]{}
\fancyfoot[LO,CE]{}
\fancyfoot[CO,CE]{\maybethepage}
\renewcommand{\headrulewidth}{0pt}
\pagestyle{fancy}


%%% colors
\definecolor{mainblue}{rgb}{0.0, 0.28, 0.67}

%%% fontspec and unicode-math
\setmathfont{Garamond-Math.otf}[StylisticSet={7,9}]
\setmainfont{EB Garamond} 
\defaultfontfeatures{Scale=MatchUppercase}
\setsansfont{Quattrocento Sans}


%%% generic
\makeatletter
\renewcommand*{\@seccntformat}[1]{\csname the#1\endcsname\hspace{0.25cm}}
\makeatother
\renewcommand{\thesection}{\arabic{section}.}
\setlength{\parindent}{0em}
\setlength{\parskip}{0.25\baselineskip}

%%% titlesec
\titleformat*{\section}{\LARGE\bfseries\sffamily\color{mainblue}\centering}

%%% personal commands
\makeatletter
\newcommand{\head}[1]{%
  \label{NPAGES\arabic{section}}%
  \clearpage
  \setcounter{page}{1}%
  \section{#1}
}
\AtEndDocument{%
  \label{NPAGES\arabic{section}}%
}
\newcommand{\maybethepage}{%
  \ifnum\getpagerefnumber{NPAGES\arabic{section}}=1
  \else
    \thepage
  \fi
}
\makeatother

\begin{document}

\head{Only one page}
Please hide page number below.

\head{Two pages}
First page. Please don't hide page number below.

\clearpage

Second page

\end{document}

ここに画像の説明を入力してください

答え2

セクションは新しいページで設定/開始されるため\section、フッター内のページ番号を条件にすることができます。これは、ページが送信されるときに設定されるからです。したがって、\head現在のセクションの (最後の) ページ番号を取得するように更新します。

\newcommand{\head}[1]{%
  \label{last-page-for-\thesection}% Store last page of current section
  \clearpage
  \vspace*{0.3cm}
  \section{#1}
  \vspace*{0.3cm}
  \setcounter{page}{1}%
}

最終ページ\sectionの最後のページ番号もキャプチャします。

\AtEndDocument{\label{last-page-for-\thesection}}% Capture last page of final section

そしてページ番号の設定条件はrefcount:

\usepackage{refcount}
% Condition on placing footer page number; if \section has more than 1 page
\fancyfoot[CO,CE]{\ifnum\getpagerefnumber{last-page-for-\thesection}>1 \thepage\fi}

完全かつ最小限の例を次に示します。

\documentclass[twoside]{extarticle}

\usepackage[dvipsnames]{xcolor}

\usepackage{fancyhdr}
\fancyhf{}% Clear header/footer
\fancyhead[RO,RE]{Right header}
\fancyhead[LO,LE]{Left header}
% Condition on placing footer page number; if \section has more than 1 page
\fancyfoot[CO,CE]{\ifnum\getpagerefnumber{last-page-for-\thesection}>1 \thepage\fi}
\renewcommand{\headrulewidth}{0pt}% Remove page header rule
\pagestyle{fancy}

\makeatletter
\renewcommand*{\@seccntformat}[1]{\csname the#1\endcsname.\hspace{0.25cm}}
\makeatother

\definecolor{mainblue}{rgb}{0.0, 0.28, 0.67}

\usepackage{titlesec}
\titleformat*{\section}{\LARGE\bfseries\sffamily\color{mainblue}\centering}

\usepackage{refcount}
\AtEndDocument{\label{last-page-for-\thesection}}% Capture last page of final section
\newcommand{\head}[1]{%
  \label{last-page-for-\thesection}% Store last page of current section
  \clearpage
  \vspace*{0.3cm}
  \section{#1}
  \vspace*{0.3cm}
  \setcounter{page}{1}%
}

\begin{document}

\head{Only one page}
Please hide page number below.

\head{Two pages}
First page. Please don't hide page number below.

\clearpage

Second page

\end{document}

関連情報