0

The figure I've inserted has a whole page to itself, it is in the middle of the page with two large blank gaps above and below. The text on the previous line ends near the bottom of the previous page so I assumed the figure would start at the top of the next page. Here is what I've written:

\begin{figure}
\captionsetup[subfigure]{justification=centering}
\begin{subfigure}{.5\textwidth}
  \centering
  \includegraphics[width=.4\linewidth]{images/a1.png}
  \caption{a}
  \label{subfig1}
\end{subfigure}%
\begin{subfigure}{.5\textwidth}
  \centering
  \includegraphics[width=.4\linewidth]{images/a2.png}
  \caption{b}
  \label{subfig2}
\end{subfigure}
\caption[abc]{abc}
\label{fig1}
\end{figure}

When I write text after \end{figure} it appears on the previous page next to the text I wrote before the figure. I want the figure to appear at the top of the page and any text I write after it to appear below the figure. I've tried \begin{figure}[h] and \begin{figure}[t] but neither works. Picture of whats happening is below, using report document class with no options. enter image description here

Heres a 'Minimum Working Example' as requested:

\usepackage[utf8]{inputenc}
\usepackage{titlesec}
\usepackage{hyperref}
\usepackage{array}
\usepackage{amsmath,amsthm}
\usepackage{cite}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{float}
\usepackage[parfill]{parskip}
\usepackage{lipsum}

\theoremstyle{definition} \newtheorem{definition}{Definition}[section] \newtheorem{theorem}{Theorem}[section]

\begin{document}

\begin{titlepage} \centering \vspace*{1cm}

   \Huge
   \textbf{Title}

   \vspace{1.5cm}

   \huge
   \textbf{Author}

   \vspace{5cm}

   \Large
   Supervised by Professor

   \vspace{1cm}

   \large
   Department\\
   University

\end{titlepage}

\addcontentsline{toc}{chapter}{Abstract} \newpage \chapter*{\centering Abstract}

\tableofcontents

\addcontentsline{toc}{chapter}{Introduction}

\listoffigures

\newpage \chapter*{Introduction}

\newpage \chapter{Chapter 1}

\lipsum[66]

\section{Section 1.1} \lipsum[66]

\lipsum[67]

We can see that the following two properties hold: \begin{enumerate} \item Property 1 \item Property 2 \end{enumerate}

\section{Section 1.2} \lipsum[66] \begin{definition}[Definition 1] etc. \end{definition}

Example:\ [ \begin{array}{c|cccc} \text{Bit}&2^3 &2^2 &2^1 &2^0\ \hline 8 &1 &0 &0 &0\ 9 &1 &0 &0 &1\ \hline 1 &0 &0 &0 &1\ \hline \end{array} ]

\lipsum[66]

Theorem below \begin{theorem} \label{thm1} Theorem \end{theorem} \begin{proof} Proof \begin{flalign} s &= 0 \oplus s \nonumber \ &= r \oplus r \oplus s && \text{(By first sentence)} \nonumber \ &= r \oplus (a_1 \oplus a_2 \oplus \cdots \oplus a_n) \oplus (b_1 \oplus b_2 \oplus \cdots \oplus b_n) \nonumber \ &= r \oplus (a_1 \oplus b_1) \oplus \cdots \oplus (a_n \oplus b_n) \nonumber \ &= r \oplus a_k \oplus b_k && \text{(Again by first sentence)} \nonumber \end{flalign}

\lipsum[66]

\begin{flalign} s &= r \oplus a_k \oplus b_k \nonumber &\ &= r \oplus a_k \oplus r \oplus a_k \nonumber &\ &= r \oplus r \oplus a_k \oplus a_k \nonumber &\ &= 0 \nonumber \end{flalign}

\lipsum[66] \end{proof}

\lipsum[66]

\chapter{Chapter 2}

\lipsum[66]

\section{Section 2.1}

\lipsum[66]

\lipsum[66]

\begin{figure}[ht] % \captionsetup[subfigure]{justification=centering} \centering \setkeys{Gin}{width=\linewidth}% \begin{subfigure}{.45\textwidth}% \includegraphics{image.png} \caption{a} \label{subfig1} \end{subfigure}% \hfil \begin{subfigure}{.45\textwidth}% \centering \includegraphics{image.png} \caption{b} \label{subfig2} \end{subfigure} \caption[abc]{abc} \label{fig1} \end{figure}

\bibliographystyle{plain} \bibliography{refs}

\end{document}

Max
  • 29
  • 1
    Did you try \begin{figure}[!tp]? If it doesn't work, it means that the figure is really too high for being placed top. – egreg Jan 31 '23 at 15:20
  • Note that if the text written after I end the figure is long enough it does force the image to the top of the page and continues below the figure. The first ~3 lines available at the end of the page continue from the previous text though and I would like them to start on the new page after the figure – Max Jan 31 '23 at 15:21
  • @egreg yeah that doesn't work. What do you mean by too high to be placed at the top? Its two small square subfigures placed side by side. – Max Jan 31 '23 at 15:23
  • 1
    Sorry, but it's impossible to answer without knowing more of your setup: what document class, what options? Are you using one-column format or two-column? – egreg Jan 31 '23 at 15:27
  • I had the same problem and fixed it thanks to https://tex.stackexchange.com/questions/68516/avoid-that-figure-gets-its-own-page. I hope this help. – LatexNoob Jan 31 '23 at 15:47
  • Thanks @LatexNoob, unfortuntately none of those seem to work. – Max Feb 01 '23 at 17:48
  • @egreg I'm using a report class with no options and one column format, see picture in edited post – Max Feb 01 '23 at 17:48
  • @Max Is there other material in the same chapter after this figure? – egreg Feb 01 '23 at 17:55
  • @egreg Not yet. When I add material I want it to start immediately after the figure but it starts at the bottom of the previous page. Once the writing is long enough to go onto the next page it continues from below the figure and pushes the figure to the top as required. But the initial few lines are on the previous page rather than below the figure – Max Feb 01 '23 at 18:12
  • @Max That's perfectly normal. The cross-reference to the caption helps the reader to find it. Remove “below” or use varioref that might add “on the facing page” or similar, if you prefer. Don't worry about the placement of figures until the text is in final form: you just lose time, because typically changes to the text destroy that work. – egreg Feb 01 '23 at 18:17
  • Ah ok thank you @egreg, but is it not possible to force the image to be in this position in the text, i thought that is what [H] etc. was for but they don't work here? maybe if I just add a few line breaks and then write my text that will appear as I described? – Max Feb 01 '23 at 18:29
  • a simple \\\\ on the last line appears as I wanted, thanks! – Max Feb 01 '23 at 18:37
  • 1
    @Max Don't do that. Wait when the text is in final form. You'll regret having lost time for such jobs before that point. – egreg Feb 01 '23 at 21:24

1 Answers1

2

Inserting your code fragment into simple article document

\documentclass{article}
\usepackage{caption, subcaption}
\usepackage{graphicx}

\usepackage{lipsum}

\begin{document}

\begin{figure} \captionsetup[subfigure]{justification=centering} \begin{subfigure}{.5\textwidth} \centering \includegraphics[width=.4\linewidth]{example-image-duck} \caption{a} \label{subfig1} \end{subfigure}% \begin{subfigure}{.5\textwidth} \centering \includegraphics[width=.4\linewidth]{example-image-duck} \caption{b} \label{subfig2} \end{subfigure} \caption[abc]{abc} \label{fig1} \end{figure}

\lipsum[1] \end{document}

I get the following result:

enter image description here

So, it is unclear, what you doing in your real document. Apparently you should provide an MWE (Minimal Working Example) which reproduce what you claim in question.

However, your image I would like in the following format:

enter image description here

for which the MWE is:

\documentclass{article}
\usepackage{caption, subcaption}
\usepackage{graphicx}

\usepackage{lipsum}

\begin{document} \lipsum[66] \begin{figure}[ht] % <--- \captionsetup[subfigure]{justification=centering} \centering \setkeys{Gin}{width=\linewidth} % <--- \begin{subfigure}{.45\textwidth} % <--- \includegraphics{example-image-duck}%{images/a1.png} \caption{a} \label{subfig1} \end{subfigure}% \hfil \begin{subfigure}{.45\textwidth} % <--- \centering \includegraphics{example-image-duck}%{images/a1.png} \caption{b} \label{subfig2} \end{subfigure} \caption[abc]{abc} \label{fig1} \end{figure}

\lipsum[1] \end{document}

Diference in comparison to your code fragment are marked by <---.

Zarko
  • 296,517
  • I'm really curious what is wrong with my answer that it was downvoted twice? – Zarko Jan 31 '23 at 19:28
  • I didn't downvote so I'm not sure either, thanks for your answer. I prefer the format that you have given but I still have the same problem in my document. I am using the report class, I will upload a screenshot of the situation I have. – Max Feb 01 '23 at 17:38
  • @Max, with using report documentclass I get the same result as shown in my answer. Do you test my MWE )Minimal Working Example). Is result different as I aghow in answer? Please, edit your question and provide MWE, which reproduce your problem. Without it we can't imagine, what is going wrong in your document. – Zarko Feb 01 '23 at 17:46
  • Hi @Zarko, please see my edit to the question with my attempt at a MWE. I've included quite a lot incase any of it causes the problem? As you say, when I just load in the packages as normal into a report class I don't have the problem so it must be from something in the document. Also see my latest comment to egreg on what happens when I write text after the figure – Max Feb 01 '23 at 18:19
  • thanks for your help, I've now found a simple workaround. – Max Feb 01 '23 at 18:38