2

In a document, I have an appendix with several screenshots, i.e., the different figures are displayed without any descriptive text right after the section header.

I notice, however, that in several cases, the figure environment and the header section are separated on different pages. I would like to avoid this, i.e., ideally, the header should then be moved to the next page.

Is there a generic solution for this? Ideally, I would have to renew the command for the section header, so that figures (or other blocks) and headers are not separated. I am stuck at this point though

Mini working example:

\documentclass[]{article}

\usepackage[centerlast]{caption}
\usepackage{float}
\usepackage{graphicx}
\usepackage{placeins}
\usepackage{tikz}

\title{}

\begin{document}

\maketitle

\section{Section 1}
%
\begin{figure}[tbh]
        \fbox{
            \includegraphics[width=0.48\textwidth]{example-image-b}
        }
        \fbox{
            \includegraphics[width=0.48\textwidth]{example-image-b}
        }
    \caption{Front and Back Side of the Device}
\end{figure}
%
\FloatBarrier
%
\section{Section 2}
%
\begin{figure}[tbh]
        \fbox{
            \includegraphics[width=0.48\textwidth]{example-image-b}
        }
        \fbox{
            \includegraphics[width=0.48\textwidth]{example-image-b}
        }
        \fbox{
            \includegraphics[width=0.48\textwidth]{example-image-b}
        }
    \caption{Main Interface of the Device}
\end{figure}
%
\FloatBarrier

\end{document}

Any suggestions or help with this matter are greatly appreciated.

  • 2
    I bet the figre box is just too big. Make the pics smaller. – Johannes_B Oct 5 '15 at 13:10
  • 1
    the only reason to put the \includegraphics inside a figure environment is to allow latex to move it, if you want it definitely to come after the heading, don't do that, just use \includegraphics directly. – David Carlisle Oct 5 '15 at 13:29
  • you can use \includegraphics[..]{example-image} in your MWE that is already included in most distributions for exactly this purpose, to save people downloading images. – David Carlisle Oct 5 '15 at 13:31
  • David, yes, I realized this as well that the figure environment could be omitted. But what about the caption then? I would still like to include a caption. – Steve Oct 5 '15 at 13:42
  • Johannes_B, we are not talking of pictures with a specific size. I potentially do not know the true dimensions of the pictures that will get inserted. I use a script to generate most parts of the text body automatically, including the appendix with various figures/pictures – Steve Oct 5 '15 at 13:44
3

For such an appendix you don't really want the figures to float at all so rather than specifying that they float and hoping they don't, use [H] to tell latex not to float them, then the usual rules about keeping section headings with teh first text in the section will kick in.

I also added some % to ends of lines to avoid the overfull box warnings in the original.

\documentclass[]{article}

\usepackage[centerlast]{caption}
\usepackage{float}
\usepackage{graphicx}
\usepackage{placeins}
\usepackage{tikz}

\title{}

\begin{document}

\maketitle

\section{Section 1}
%
\begin{figure}[H]
        \fbox{%
            \includegraphics[width=0.48\textwidth]{example-image-b}%
        }\hfill
        \fbox{%
            \includegraphics[width=0.48\textwidth]{example-image-b}%
        }
    \caption{Front and Back Side of the Device}
\end{figure}
%
\FloatBarrier
%
\section{Section 2}
%
\begin{figure}[H]
        \fbox{%
            \includegraphics[width=0.48\textwidth]{example-image-b}%
        }\hfill
        \fbox{%
            \includegraphics[width=0.48\textwidth]{example-image-b}%
        }\hfill
        \fbox{%
            \includegraphics[width=0.48\textwidth]{example-image-b}%
        }
    \caption{Main Interface of the Device}
\end{figure}
%
\FloatBarrier

\end{document}
  • I use [H] and I still have a section on the end of one page and the figure on the next page. – Martin Thoma Sep 22 '17 at 14:03
  • @MartinThoma tex must have been desperate then and had no legal page break I guess, just put \clearpage before the section heading (assuming the heading and the figure fit on one page) – David Carlisle Sep 22 '17 at 14:06
  • The problem is that I'm automatically generating LaTeX. I think I rather put all sections which contain only one figure / table in \begin{minipage}{\linewidth} <my content> \end{minipage}. This should prevent them from getting separated and they still can be on one page. – Martin Thoma Sep 22 '17 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.