在我当前的项目中,我想创建可以打印和分发的工作表。为此,我想重置每个工作表的页码,但仅在工作表有多页时才显示页码(例如,如果它有后页)。如果工作表只有一页,则应隐藏页码,以免对第 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
由于您的\section
s 是在新页面上设置/开始的,因此您可以根据页脚中的页码进行条件设置,因为这些是在页面发送时设置的。因此,我将进行更新\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}