Remover o número da página, se houver apenas uma página em uma seção

Remover o número da página, se houver apenas uma página em uma seção

No meu projeto atual, quero criar planilhas que possa imprimir e distribuir. Para fazer isso, quero redefinir o número da página de cada planilha, mas exibir o número da página apenas se a planilha tiver mais de uma página (por exemplo, se tiver um verso). Se a planilha tiver apenas uma página, o número da página deverá ser ocultado para que não haja confusão desnecessária sobre onde está a página 2, etc. Posso fazer isso com fancyhdr?

Aqui está o MWE. Cada planilha começa com sua própria seção. Observe que eu 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}

Eu sei que posso fazer isso manualmente definindo um novo estilo de página, mas isso seria muito tedioso para mim. Já criei muitas planilhas que gostaria de mesclar em um projeto e levaria muito tempo para ver se todas cabem em uma página. Alguém tem idéias do que eu poderia fazer?

Responder1

Use \labelpara armazenar o número da página logo após a emissão \clearpagee na próxima execução o teste se o número de páginas é 1 será bem-sucedido e \thepageserá ignorado.

Eu consertei headheightissonão podeser definido como zero se você quiser cabeçalhos. Também fontencnão deve ser carregado junto com fontspec. Reorganizei o preâmbulo para primeiro carregar os pacotes e depois fazer a configuração.

Use \titlespacingem vez de \vspaceao redor \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}

insira a descrição da imagem aqui

Responder2

Como seus \sectionarquivos são definidos/iniciados em uma nova página, você pode definir o número da página no rodapé, pois eles são definidos à medida que as páginas são enviadas. Então, eu atualizaria \headpara capturar o (último) número da página da seção atual:

\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 também o \sectionnúmero da última página do final:

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

E então condicione a configuração do número da 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}

Aqui está um exemplo completo e 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}

informação relacionada