
В моем текущем проекте я хочу создать рабочие листы, которые я могу распечатать и распространить. Для этого я хочу сбросить номер страницы для каждого рабочего листа, но отображать номер страницы только в том случае, если рабочий лист имеет более одной страницы (например, если у него есть последняя страница). Если рабочий лист имеет только одну страницу, номер страницы должен быть скрыт, чтобы не было ненужной путаницы относительно того, где находится страница 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}