如果節中只有一頁,則刪除頁碼

如果節中只有一頁,則刪除頁碼

在我目前的專案中,我想建立可以列印和分發的工作表。為此,我想重置每個工作表的頁碼,但僅在工作表有多於一頁(例如,如果它有背面)時才顯示頁碼。如果工作表只有一頁,則應隱藏頁碼,以免對第 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

用於\label儲存剛發布後的頁碼\clearpage,下次執行時間測試頁數是否為1會成功並\thepage跳過。

我修好headheight不能如果您想要標題,請設定為零。也不fontenc應該與 一起加載fontspec。我重新組織了序言,先載入包,然後進行設定。

使用\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

由於您的\sections 是在新頁面上設定/開始的,因此您可以根據頁腳內的頁碼進行調整,因為這些頁碼是在頁面出貨時設定的。因此,我將更新\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}%
}

也捕捉 Final\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}

相關內容