I have a document written in Markdown, and converted to PDF using pandoc - where suddenly a very stubborn and unwanted clear page has been inserted.
I managed to obtain the final .tex output from pandoc --verbose, and clean it up a bit (the preamble is a mess between what Pandoc inserts automatically, and some of my custom edits through the years), and still show the problem - here it is:
First, let's say, I've generated single.pdf image using the below single.tex via xelatex single.tex:
\documentclass[a4paper]{article}
\usepackage{xcolor}
\begin{document}
\pagecolor{yellow}
...
\end{document}
Then, the actual document demonstrating the problem is this, blankpage.tex:
% Options for packages loaded elsewhere
\PassOptionsToPackage{unicode}{hyperref}
\PassOptionsToPackage{hyphens}{url}
\PassOptionsToPackage{dvipsnames,svgnames,x11names}{xcolor}
%
\documentclass[
12pt,
a4paper,
table]{extarticle}
\usepackage{amsmath,amssymb}
\usepackage{iftex}
\ifPDFTeX
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{textcomp} % provide euro and other symbols
\else % if luatex or xetex
\usepackage{unicode-math} % this also loads fontspec
\defaultfontfeatures{Scale=MatchLowercase}
\defaultfontfeatures[\rmfamily]{Ligatures=TeX,Scale=1}
\fi
\usepackage{lmodern}
\ifPDFTeX\else
% xetex/luatex font selection
\fi
% Use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\IfFileExists{microtype.sty}{% use microtype if available
\usepackage[]{microtype}
\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\makeatletter
\@ifundefined{KOMAClassName}{% if non-KOMA class
\IfFileExists{parskip.sty}{%
\usepackage{parskip}
}{% else
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}}
}{% if KOMA class
\KOMAoptions{parskip=half}}
\makeatother
\usepackage{xcolor}
\usepackage[margin=2cm]{geometry}
\usepackage{listings}
\newcommand{\passthrough}[1]{#1}
\usepackage{graphicx}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
\makeatother
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
\makeatletter
\def\fps@figure{htbp}
\makeatother
\setlength{\emergencystretch}{3em} % prevent overfull lines
\usepackage{pifont}
\usepackage{fontspec}
\setmainfont[Ligatures=TeX]{CMU Serif}
\unimathsetup{bold-style=ISO}
\usepackage[skip=4pt,font=small]{caption}
\usepackage{lastpage}
\setcounter{tocdepth}{4}%
\setcounter{secnumdepth}{4}%
\usepackage{fancyhdr}
\fancypagestyle{plain}{%
\renewcommand{\headrulewidth}{0pt}%
\fancyhf{}%
\fancyfoot[LE,RO]{\footnotesize Page \thepage\ of \pageref*{LastPage}}%
}
\pagestyle{fancy}
\fancyfoot{}
\fancyfoot[LE,RO]{\footnotesize Page \thepage\ of \pageref*{LastPage}}
\usepackage[fit]{truncate}
\fancyhead[LE,RO]{\nouppercase{\truncate{0.5\headwidth}{\rightmark}}}
\fancyhead[LO,RE]{\nouppercase{\truncate{0.5\headwidth}{\leftmark}}}
\lstset{% for listings
basicstyle=\ttfamily,
breaklines=true,
postbreak=\mbox{\textcolor{red}{$\hookrightarrow$}\space},
}
\colorlet{dollarcol}{red}
\usepackage{pmboxdraw}
\usepackage{newunicodechar}
\newunicodechar{■}{$\blacksquare$}
\usepackage[breakable, skins]{tcolorbox}
\usepackage[toctitles]{titlesec}
\titlelabel{\thetitle.\quad}
\usepackage[dotinlabels]{titletoc}
\usepackage[toc,page,title,titletoc,header]{appendix}
\makeatletter
\renewcommand*\l@section{\@dottedtocline{1}{0em}{1.5em}}
\makeatother
\setlength\itemsep{0.5\baselineskip}
\usepackage{enumitem}
\usepackage{comment}
\title{Example doc}
\author{Me}
\date{Wednesday, March 13, 2024}
\begin{document}
\maketitle
{
\setcounter{tocdepth}{3}
\tableofcontents
}
\clearpage
\section{First section}
Testing ....
\clearpage
\subsection{Second section}
Testing ....
\clearpage
\subsection{Third section}
Testing ....
\clearpage
\section{Fourth section}
\begin{center}
\includegraphics{single.pdf}
\end{center}
\begin{center}
\includegraphics{single.pdf}
\end{center}
\clearpage
\section{Fifth section}
Testing ....
\clearpage
\end{document}
So, I compile this with xelatex blankpage.tex, and I see:
... a blank page, page number 5, being inserted between page 4 with \subsection{Third section}, and page 6 with \section{Fourth section}.
It's almost as if the blank was inserted to ensure next \section falls on an even page - but this is extarticle class, which as far as I know has the same settings as article class - and that one is not twoside? So as per removing an unwanted page between two chapters class options oneside and openany (LaTeX Warning: Unused global option(s): [openany].) do not help - the extra blank page is still there.
So what is the cause of this extra blank page - and how to I remove it?
(Also irritating, that no matter how many times I run xelatex blankpage.tex, I always get:
LaTeX Warning: Reference `*' on page 7 undefined on input line 142.
...
LaTeX Warning: Reference `*' on page 8 undefined on input line 148.
[8]
AED: lastpage setting LastPage
(./blankpage.aux)
LaTeX Warning: There were undefined references.
)
(see the transcript file for additional information)
Output written on blankpage.pdf (8 pages).
Transcript written on blankpage.log.
... and LastPage clearly does not work in this example - but never mind that, just need to vent)

\enlargethispage{5\baselineskip}worked to get rid of the extra page (as I posted below) – sdbbs Mar 13 '24 at 18:24