[Tex/LaTex] \clearpage produces an extra blank page

page-breakingrotating

I'm trying to insert some sidewaysfigure environments between segments of text. The output should be like

  1. Title Page
  2. Text (ends about halfway down the page)
  3. Sideways Figure
  4. Sideways Figure
  5. Text

The problem I'm having is there's an extra blank page inserted before the figures and at the end of the output, like this

  1. Title Page
  2. Text
  3. Blank Page
  4. Sideways Figure
  5. Sideways Figure
  6. Text
  7. Blank Page

here's my code:

%--- document type, don't touch this ------
\documentclass[a4paper,11pt]{article}


%--- packages used, may need to add to this -----
\usepackage{fancyhdr}
\usepackage{lastpage}
\usepackage[top=1in, bottom=1.25in, left=1.25in, right=1.25in]{geometry}
\usepackage{pdflscape}
\usepackage{graphicx}
\usepackage{rotating}
\usepackage[labelfont=bf]{caption}
\usepackage{float}


%---- Change these to reflect your experiment -----
\providecommand{\expname}{Experiment Name}%Name of the Experiment
\providecommand{\expdate}{Experiment Date}%Date Experiment was done



%---- This is the definition of \pefig, do not alter it ------
\newcommand{\pefig}[3]{
\let\cleardoublepage\clearpage
\begin{sidewaysfigure}[ht]
\pagestyle{empty}
\centering
\includegraphics[width=0.9\linewidth,keepaspectratio]{#1}
\caption{#2}
\label{#3}
\end{sidewaysfigure}
}



%--- these commands control the header, don't touch 'em -------
\setlength{\headheight}{25.3pt}
\renewcommand{\headrulewidth}{0pt}
\lhead{\iffloatpage{}{Your Name}}
\chead{\iffloatpage{}{PEGN 308: Reservoir Rock Properties\\
\expname}}
\rhead{\iffloatpage{}{\thepage\\\expdate}}
\cfoot{}


%--- You can make this whatever you want, it doesn't affect anything, just names it on Overleaf
\title{Paper Title}



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%            Actual Content           %%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\pagestyle{empty}
\topskip0pt
\vspace*{\fill}
\vspace*{-15pt}
\begin{center}
PEGN 308: Reservoir Rock Properties\\
\expname\\
\textbf{Your Name}\\
Section X Group X1\\
Instructor:\\
Al Sami\\
Lab Group Members:\\
Their\\
Names\\
Here\\
\pageref{LastPage} pages\\
\expdate
\end{center}
\vspace*{\fill}
\newpage

%--- Report ------
\setcounter{page}{1}
\pagestyle{fancy}
a buncha text here
\clearpage

%blank page appears here for some reason

%--- Figures -----
%Syntax is: \pefig{filename}{Caption}{Label}

\pefig{Sample.jpg}{Sample Figure}{fig:sample1}

\pefig{Sample.jpg}{Sample Figure}{fig:sample2}

\clearpage



a bunch more text here

%Blank page here too

\end{document}

The trailing blank page was actually there before I inserted the images, I think it has something to do with the way I made my title page

Please help, I think my problems stem from a poor understanding of \newpage and \clearpage

EDIT: For the record, all of those comments are from me to the eventual user, a student in PEGN 308, so when something says "don't modify this" that absolutely doesn't apply to me.

Best Answer

The immediate problem is caused by the global redefinition

\topskip0pt

which, apart from anything else, geometry knows nothing about.

Removing this removes the additional pages. Wrapping the title page code in titlepage also works because it limits the scope of the setting. I would recommend something like this:

\begin{titlepage}
  \vspace*{\fill}
  \vspace*{-15pt}
  \centering
    PEGN 308: Reservoir Rock Properties\\
    Experiment Title\\
    \textbf{My Name}\\
    Section X Group X1\\
    Instructor:\\
    Al Sami\\
    Lab Group Members:\\
    Their\\
    Names\\
    Here\\
    \pageref{LastPage} pages\\
    \today
  \vspace*{\fill}
\end{titlepage}

Usually it is bad to end lines with \\ but it is OK when text is centred as it is equivalent to ending the paragraph.

However, definitions like the following

\newcommand{\pefig}[3]{
  \let\cleardoublepage\clearpage
  \begin{sidewaysfigure}[ht]
    \pagestyle{empty}
    \centering
    \includegraphics[width=0.9\linewidth,keepaspectratio]{#1}
    \caption{#2}
    \label{#3}
  \end{sidewaysfigure}
}

are problematic because they insert spurious spaces. In many cases, this won't matter because the content is sandwiched by page breaks anyway. But in some cases, it will matter.

A line ending is a space.

an
apple

is equivalent to

an apple

To avoid this in macro definitions, you can comment line endings. For example,

\newcommand{\pefig}[3]{%

Moreover, redefining \cleardoublepage is pointless here. You are typesetting a one-sided article.

Are pages with figures supposed to have page numbers? If so, setting the page style to empty is incorrect. If not, setting the page style in this way does nothing. That is.

\pagestyle{empty}

should either be deleted (for page numbers) or changed to

\thispagestyle{empty}%

(for no page numbers on these pages).

Setting ht for a sidewaysfigure makes little sense. So I'd drop that, too, and use something like

\newcommand{\pefig}[3]{%
  \begin{sidewaysfigure}
    %\thispagestyle{empty}% uncomment for unnumbered pages
    \centering
    \includegraphics[width=0.9\linewidth,keepaspectratio]{#1}%
    \caption{#2}%
    \label{#3}%
  \end{sidewaysfigure}%
}

Then the result is

fewer spurious pages

\documentclass[a4paper,11pt]{article}
\usepackage{fancyhdr}
\usepackage{lastpage}
\usepackage[top=1in, bottom=1.25in, left=1.25in, right=1.25in]{geometry}
\usepackage{pdflscape}
\usepackage{graphicx}
\usepackage{rotating}
\usepackage[labelfont=bf]{caption}
\usepackage{float}
\newcommand{\pefig}[3]{%
  \begin{sidewaysfigure}
    %\thispagestyle{empty}%
    \centering
    \includegraphics[width=0.9\linewidth,keepaspectratio]{#1}%
    \caption{#2}%
    \label{#3}%
  \end{sidewaysfigure}%
}
\begin{document}
\begin{titlepage}
  \vspace*{\fill}
  \vspace*{-15pt}
  \centering
    PEGN 308: Reservoir Rock Properties\\
    Experiment Title\\
    \textbf{My Name}\\
    Section X Group X1\\
    Instructor:\\
    Al Sami\\
    Lab Group Members:\\
    Their\\
    Names\\
    Here\\
    \pageref{LastPage} pages\\
    \today
  \vspace*{\fill}
\end{titlepage}

lorem ipsum other gobbledegook

\clearpage
\pefig{example-image-a}{Sample Caption}{label}
\pefig{example-image-b}{Another Sample Caption}{label}
\clearpage

now more text down here
\end{document}