2

I want to insert figures between text just like this:

enter image description here.

So I use the following code:

**
\begin{document}
Text here.
\begin{figure}
    \centering
    \begin{subfigure}[!h]{0.3\textwidth}
        \includegraphics[width=\textwidth]{graph01.png}
        \caption{A tree.}
    \end{subfigure}
~
    \begin{subfigure}[!h]{0.3\textwidth}
        \includegraphics[width=\textwidth]{graph02.png}
        \caption{Petersen's graph}
    \end{subfigure}
    \caption{Examples of a graph}
\end{figure}
Text here again.
\end{document}

**

The image was not exact in the position where I want it to be. Instead, the image is placed on another page. How can I change that?

naphaneal
  • 2,614
  • welcome to tex.se! just add option [htb] to figure environment, like \begin{figure}[htb] . if between paragraphs will be enough place, figures will appear there. – Zarko May 22 '18 at 19:28
  • An in depth discussion on how floats are placed can be found here: How to influence the position of float environments like figure and table in LaTeX? – leandriis May 22 '18 at 19:33
  • 2
    note the only reason to use a figure enviornment is to allow it to be moved in that way. If you use \includegraphics directly it is not moved. – David Carlisle May 22 '18 at 19:41
  • 1
    Supplement to @DavidCarlisle’s comment: for best results, you can include the \includegraphics command in a center environment. If, in addition, you want to caption the figure, you can do so by means of the caption package and its \captionof command. An alternative solution is to load the float package and use the H (capital) placement specifier. – GuM May 22 '18 at 23:46
  • subfigure doesn't use positioning at all, so the [!h} is at best useless. [htp] for the figure means here if possible, top of the next page if not, or in a page of nothing but floats. I have yet to find a case where adding ! did anything useful. – John Kormylo May 23 '18 at 16:02

0 Answers0