# LaTeX: \figure + \includegraphics unwanted page breaks and wrong spacing between caption

I'm implementing gallery of scanned documents and I have issues with using \figure + \includegraphics, see the screenshot below:

1) [violet comment] caption of current image and following image are (usually, see issue below) rendered over each other

2) [violet comment] space between caption of current image and following image is unpredictable - in most cases caption and following image are glued together, but sometimes extra line is inserted (this is expected behaviour for every image)

3) [blue comment] space between image and caption belonging to this image is to big

4) [red comment] unexpected page breaks are inserted in the middle of the page although there is still enough space - this issue is really critical

Full code:

\documentclass[a4paper]{book}

\usepackage{fontspec}

\usepackage{polyglossia}
\setmainlanguage{czech}

\usepackage{ulem}
\usepackage{spverbatim}
\usepackage[unicode]{hyperref}
\def\do#1{\appto\UrlSpecials{\do#1{\mathchar#1 \mskip 0mu plus 1mu\penalty100\relax}}}
\do\-\do\/\do\0\do\1\do\2\do\3\do\4\do\5\do\6\do\7\do\8\do\9\do\a\do\b\do\c\do\d\do\e\do\f\do\g\do\h\do\i\do\j\do\k\do\l\do\m\do\n\do\o\do\p\do\q\do\r\do\s\do\t\do\u\do\v\do\w\do\x\do\y\do\z\do\A\do\B\do\C\do\D\do\E\do\F\do\G\do\H\do\I\do\J\do\K\do\L\do\M\do\N\do\O\do\P\do\Q\do\R\do\S\do\T\do\U\do\V\do\W\do\X\do\Y\do\Z
\usepackage[]{graphicx}
\usepackage{grffile}
\usepackage{color}

\usepackage{wrapfig}
\usepackage{float}
\usepackage[section]{placeins}
\usepackage{hologo}

\usepackage{pdfpages}

\usepackage{needspace}
\intextsep=0pt

\DeclareTextFontCommand{\mytexttt}{\ttfamily\hyphenchar\font=45\relax}

\makeatletter
\renewcommand{\l@section}{\@dottedtocline{1}{1.5em}{2.6em}}
\renewcommand{\l@subsection}{\@dottedtocline{2}{4.0em}{3.6em}}
\renewcommand{\l@subsubsection}{\@dottedtocline{3}{7.4em}{4.8em}}
\setlength{\@fptop}{0pt}
\setlength{\@fpbot}{0pt plus 1fil}
\makeatother

\begin{document}

\phantomsection\label{ID_FIL_DISPLAY_DOC_507}
\begin{figure}[htp]
\centering
\rule{\textwidth}{0.231\textwidth}
\caption{\hyperref[ID_FIL_USAGELIST_DOC_507]{Dokument~508}}
\end{figure}
\FloatBarrier % Prevents error Too many unprocessed floats
\phantomsection\label{ID_FIL_DISPLAY_DOC_508}
\begin{figure}[htp]
\centering
\rule{\textwidth}{0.089\textwidth}
\caption{\hyperref[ID_FIL_USAGELIST_DOC_508]{Dokument~509}}
\end{figure}
\FloatBarrier % Prevents error Too many unprocessed floats
\phantomsection\label{ID_FIL_DISPLAY_DOC_509}
\begin{figure}[htp]
\centering
\rule{\textwidth}{0.105\textwidth}
\caption{\hyperref[ID_FIL_USAGELIST_DOC_509]{Dokument~510}}
\end{figure}
\FloatBarrier % Prevents error Too many unprocessed floats
\phantomsection\label{ID_FIL_DISPLAY_DOC_510}
\begin{figure}[htp]
\centering
\rule{\textwidth}{0.059\textwidth}
\caption{\hyperref[ID_FIL_USAGELIST_DOC_510]{Dokument~511}}
\end{figure}
\FloatBarrier % Prevents error Too many unprocessed floats
\phantomsection\label{ID_FIL_DISPLAY_DOC_511}
\begin{figure}[htp]
\centering
\rule{\textwidth}{0.264\textwidth}
\caption{\hyperref[ID_FIL_USAGELIST_DOC_511]{Dokument~512}}
\end{figure}
\FloatBarrier % Prevents error Too many unprocessed floats
\phantomsection\label{ID_FIL_DISPLAY_DOC_512}
\begin{figure}[htp]
\centering
\rule{\textwidth}{0.154\textwidth}
\caption{\hyperref[ID_FIL_USAGELIST_DOC_512]{Dokument~513}}
\end{figure}
\FloatBarrier % Prevents error Too many unprocessed floats
\phantomsection\label{ID_FIL_DISPLAY_DOC_513}
\begin{figure}[htp]
\centering
\rule{\textwidth}{0.329\textwidth}
\caption{\hyperref[ID_FIL_USAGELIST_DOC_513]{Dokument~514}}
\end{figure}
\FloatBarrier % Prevents error Too many unprocessed floats

\end{document}


1) how to force space between caption and following image?

2) how to force always the same space between caption and following image?

3) how to decrease space between image and its caption (I have thousands images in document, cca 4000 pages, and because at the end it will be printed to paper, every removed page from document is useful)?

4) how to fix unwanted page breaks in the middle of the page (of course, no image or caption shall overflow below text area, but I expect whole space on page shall be used if image + caption fits to it)?

Maybe \figure + \includegraphics is not convenient for image galleries?

• the captions and images should never over-print, that may be an issue with your images, if tex can not determine their size. – David Carlisle Jan 3 '19 at 18:40
• you do not want floating images here so really you not want floats at all, you could remove the figure environments, or equivalently use the [H] option from the float package, you could then remove the floatbarrier commands (that are forcing page breaks). – David Carlisle Jan 3 '19 at 18:47
• Images are correct - the same behaviour is with real images and also with \rule replacement with exactly specified dimensions. Regarding [H] see my comment under answer below. – Joseph Jan 3 '19 at 20:34
• your example shows no overprinting so I do not understand your comment (1) – David Carlisle Jan 3 '19 at 21:13

When you have lists of images, with no text then you do not want floats, so you should not use figure, or use [H] to suppress floating.

\documentclass[a4paper]{book}

\usepackage{fontspec}

\usepackage{polyglossia}
\setmainlanguage{czech}

\usepackage{ulem}
\usepackage{spverbatim}
\usepackage[unicode]{hyperref}
\def\do#1{\appto\UrlSpecials{\do#1{\mathchar#1 \mskip 0mu plus 1mu\penalty100\relax}}}
\do\-\do\/\do\0\do\1\do\2\do\3\do\4\do\5\do\6\do\7\do\8\do\9\do\a\do\b\do\c\do\d\do\e\do\f\do\g\do\h\do\i\do\j\do\k\do\l\do\m\do\n\do\o\do\p\do\q\do\r\do\s\do\t\do\u\do\v\do\w\do\x\do\y\do\z\do\A\do\B\do\C\do\D\do\E\do\F\do\G\do\H\do\I\do\J\do\K\do\L\do\M\do\N\do\O\do\P\do\Q\do\R\do\S\do\T\do\U\do\V\do\W\do\X\do\Y\do\Z
\usepackage[]{graphicx}
\usepackage{grffile}
\usepackage{color}

\usepackage{wrapfig}
\usepackage{float}
\usepackage[section]{placeins}
\usepackage{hologo}

\usepackage{pdfpages}

\usepackage{needspace}
%\intextsep=0pt

\DeclareTextFontCommand{\mytexttt}{\ttfamily\hyphenchar\font=45\relax}

\makeatletter
\renewcommand{\l@section}{\@dottedtocline{1}{1.5em}{2.6em}}
\renewcommand{\l@subsection}{\@dottedtocline{2}{4.0em}{3.6em}}
\renewcommand{\l@subsubsection}{\@dottedtocline{3}{7.4em}{4.8em}}
\makeatother

\begin{document}

\raggedbottom

\phantomsection\label{ID_FIL_DISPLAY_DOC_507}
\begin{figure}[H]
\centering
\rule{\textwidth}{0.231\textwidth}
\caption{\hyperref[ID_FIL_USAGELIST_DOC_507]{Dokument~508}}
\end{figure}

\phantomsection\label{ID_FIL_DISPLAY_DOC_508}
\begin{figure}[H]
\centering
\rule{\textwidth}{0.089\textwidth}
\caption{\hyperref[ID_FIL_USAGELIST_DOC_508]{Dokument~509}}
\end{figure}

\phantomsection\label{ID_FIL_DISPLAY_DOC_509}
\begin{figure}[H]
\centering
\rule{\textwidth}{0.105\textwidth}
\caption{\hyperref[ID_FIL_USAGELIST_DOC_509]{Dokument~510}}
\end{figure}

\phantomsection\label{ID_FIL_DISPLAY_DOC_510}
\begin{figure}[H]
\centering
\rule{\textwidth}{0.059\textwidth}
\caption{\hyperref[ID_FIL_USAGELIST_DOC_510]{Dokument~511}}
\end{figure}

\phantomsection\label{ID_FIL_DISPLAY_DOC_511}
\begin{figure}[H]
\centering
\rule{\textwidth}{0.264\textwidth}
\caption{\hyperref[ID_FIL_USAGELIST_DOC_511]{Dokument~512}}
\end{figure}

\phantomsection\label{ID_FIL_DISPLAY_DOC_512}
\begin{figure}[H]
\centering
\rule{\textwidth}{0.154\textwidth}
\caption{\hyperref[ID_FIL_USAGELIST_DOC_512]{Dokument~513}}
\end{figure}

\phantomsection\label{ID_FIL_DISPLAY_DOC_513}
\begin{figure}[H]
\centering
\rule{\textwidth}{0.329\textwidth}
\caption{\hyperref[ID_FIL_USAGELIST_DOC_513]{Dokument~514}}
\end{figure}

\end{document}

• Using [H] causes another issue which was described in thread tex.stackexchange.com/questions/468178/… - first image in chapter overflows the page and additionally chapter title is spread over the page. Originally, I started with [H] and solution for referenced issue was to use [htp]. So it seems I'm in a circle - one fix produces another issue and vice versa. Is there some solution with above output, but without page overflow and damaged chapter title described in referenced thread? – Joseph Jan 3 '19 at 20:32
• I don't think you should have a problem with chapter headings if you use the code here (using [H] and \raggedbottom) @Joseph – David Carlisle Jan 3 '19 at 21:11
• Unfortunately, this solution really doesn't work with chapters. If you add \chapter{Chapter}\section{Section} and define first image higher (in my real document \rule{\textwidth}{1.359\textwidth}, it's rendered below chapter title and overflows the page instead of page break - exactly as described in the link tex.stackexchange.com/questions/468178/… . This issue is fixed by using [htp] instead of [H] and in this case code must also contain \FloatBarrier to prevent compilation error Too many unprocessed floats. – Joseph Jan 4 '19 at 6:06
• So the question is still open: is there some solution to fix issue described in this thread and also don't produce another issue described in tex.stackexchange.com/questions/468178/… ? (I know it's difficult to debug it here, it would be better to place here full real document and not minimal working example, but my generated document is 8 MB long and attached images are 250 MB, usually with private confidential contents). – Joseph Jan 4 '19 at 6:11
• Just additional information: your solution is much better and I take it as current code. So open items are: 1) overflowing first image in chapter (other images are OK) and 2) how to decrease space between image and (its) caption - full line is waste of space, there are thousands of images in my real document. – Joseph Jan 4 '19 at 6:13