Search code examples
latexoverleaf

Figures all over the show in latex in overleaf


I am having issues with a report I am writing. First time using latex and after getting some help on here regarding some tables weirdness I again have a silly frustrating issue. Figures don't stay where I put them. I made a MWE Posted Below

\documentclass{article}
\usepackage{graphicx}
\graphicspath{ {./Figures/} }

%%%%%% Referencing %%%%%%
\usepackage{natbib}
\bibliographystyle{abbrvnat}
\setcitestyle{authoryear,open={(},close={)}}

\title{MWE}

\begin{document}

\maketitle

\section{Introduction}
The average sea surface temperature (SST) trend is shown in Figure \ref{fig:SST}.
\section{Figures}

\begin{figure}
  \includegraphics[width = \linewidth]{Figures/SST.png}
  \label{fig:SST}
\end{figure}
\section{References}
\bibliography{References.bib}
\bibliographystyle{agsm}
\end{document}

I expect, probably an error, for the figure to be inserted under the Figures subheading however it appears on the next page after references. I have to refer to my figure in the text, hence the \ref{fig:SST} so it is clickable. It's not a size issue as there is more than enough space on the page to accommodate the figure. Even if that was the issue I would expect the references subheading to be after it.


Solution

  • Most latex classes use so called floats for figures, tables etc. The idea is that latex will automatically find a good place for your images and avoid ugly white space.

    To make use of the abilities of latex to produce a good looking output, you must specify possible placements with floating specifier such as [htbp], which allows latex to place the image here, at the top, at the bottom or an a separate page.

    Also if you want to use the \label-\ref mechanism, your figure must have a caption (and the label inside or after the caption).

    \documentclass{article}
    \usepackage{graphicx}
    \graphicspath{ {./Figures/} }
    
    %%%%%% Referencing %%%%%%
    \usepackage{natbib}
    \bibliographystyle{abbrvnat}
    \setcitestyle{authoryear,open={(},close={)}}
    
    \title{MWE}
    
    \begin{document}
    
    \maketitle
    
    \section{Introduction}
    The average sea surface temperature (SST) trend is shown in Figure \ref{fig:SST}.
    \section{Figures}
    \begin{figure}[htbp]
      \includegraphics[width = \linewidth]{example-image-duck}
      \caption{test}
      \label{fig:SST}
    \end{figure}
    \section{References}
    \bibliography{References.bib}
    \bibliographystyle{agsm}
    \end{document}