
En mi proyecto actual, quiero crear hojas de trabajo que pueda imprimir y distribuir. Para hacer esto, quiero restablecer el número de página para cada hoja de trabajo, pero solo mostrar el número de página si la hoja de trabajo tiene más de una página (por ejemplo, si tiene una última página). Si la hoja de trabajo tiene solo una página, el número de página debe ocultarse para que no haya confusión innecesaria sobre dónde está la página 2, etc. ¿Puedo hacer esto con fancyhdr
?
Aquí está MWE. Cada hoja de trabajo comienza con su propia sección. Tenga en cuenta que uso 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}
Sé que puedo hacer esto manualmente definiendo un nuevo estilo de página, pero sería demasiado tedioso para mí. Ya he creado muchas hojas de trabajo que me gustaría fusionar en un solo proyecto y me llevaría mucho tiempo ver si todas caben en una sola página. ¿Alguien tiene ideas de lo que podría hacer?
Respuesta1
Úselo \label
para almacenar el número de página justo después de la emisión \clearpage
y en la siguiente ejecución la prueba para determinar si el número de páginas es 1 será exitosa y \thepage
se omitirá.
headheight
lo arregléno puedodebe establecerse en cero si desea encabezados. Tampoco fontenc
debe cargarse junto con fontspec
. Reorganicé el preámbulo para cargar primero los paquetes y luego realizar la configuración.
Usar \titlespacing
en lugar de \vspace
alrededor \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}
Respuesta2
Dado que sus \section
anuncios se configuran/inician en una nueva página, puede condicionar el número de página dentro del pie de página, ya que estos se configuran a medida que se envían las páginas. Por lo tanto, actualizaría \head
para capturar el número de página (última) de la sección actual:
\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}%
}
Capture también el \section
número de la última página del final:
\AtEndDocument{\label{last-page-for-\thesection}}% Capture last page of final section
Y luego condicione la configuración del número de página usandorefcount
:
\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}
Aquí hay un ejemplo completo y mínimo:
\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}