12

When i run the following code i get my output on two pages. On first page the text line and my image on page 2. How can i get my output on the same page that is text and image on the same page. Please help.

\documentclass[a4paper,12pt]{article}
\usepackage{graphicx}
\begin{document}
Hi...My first test image
\begin{figure}[h]
\centering
\includegraphics[width=1\textwidth]{fig10.pdf}
\caption{My first image}
\end{figure}

\end{document}
2
  • 1
    Perhaps you want to take a look at this question. Sep 21, 2013 at 8:21
  • Will the figure fit on the same page as the text if you replace [h] with [h!]? If not, the image is probably too large if you insist on its width being 1\textwidth.
    – Mico
    Sep 21, 2013 at 9:21

2 Answers 2

16

Simply your image is too large to fit on the first page. Note that LaTeX consider not only if there is enough space among the margins, also make penalties for a bad design (for example, if the fraction of text with respect the floats is less than 20%) and considering this, LaTeX tries to do the best (print a format with less penalties) than could be different to what you expected. See What are penalties and which ones are defined? for more information a this respect. Follow also the Thorsten's advice and see also How to influence the position of float environments like figure and table in LaTeX?

Without knowing the size of the original figure is not possible to say exactly how to solve your minimal working example (MWE), but you can simply narrow the figure to some less than 1\textwidth. Or may help for a big figure add the option [h!] as this relax the LaTeX rules for this float. Or you can change the rules, writing in the preamble:

\renewcommand{\textfraction}{0.05} 

For example:

\documentclass[a4paper,12pt]{article}
\usepackage{graphicx}
\usepackage{mwe} % for the example image
\begin{document}
Hi...My first test image
\begin{figure}[h!]
\centering
\includegraphics[width=.8\textwidth]{example-image-10x16}
\caption{My first image}
\end{figure}
\end{document}

MWE

7
  • thank you so much...it really helped.. one more thing i wanted to ask caption appears very much below the figure. how can i show it just below the figure?
    – Misha
    Sep 21, 2013 at 9:55
  • 1
    You're welcome. Try some like \renewcommand{\abovecaptionskip}{1cm} in the preamble. You can change 1cm by any other measure, like 4em, or 3ex, 80pt, etc.
    – Fran
    Sep 21, 2013 at 10:22
  • hello...there is lot of space between the text and the image which i put up in my document and then again there is lot of space between the image and its caption..the above command which you mentioned is not working. is there any other way to reduce this unwanted space??
    – Misha
    Sep 27, 2013 at 9:40
  • The command worked for me. Without looking a MWE from you it is hard to imagine what is going on and how this can be solved. Please, edit your question to add a a new MWE if you think that you have still the same problem, or open a new question if you suspect that now there are a different problem.
    – Fran
    Sep 27, 2013 at 13:08
  • I ran into an obscure problem, nothing was working - the figure would always stay on a single by itself no matter what. So it turns out that I was compiling a file using pdflatex which used includegraphics on an .eps file. When it did so, it converted the .eps to a .pdf but in a buggy way which included a very large bounding box. I discovered this by using the draft option and inspecting the bounding boxes. What I did was to supply my own converted .pdf figure to pdflatex which was properly trimmed. Then the [h!] suggested here worked!
    – rfabbri
    Nov 21, 2015 at 15:44
1

THe best way to do it is using [H] while inserting a figure
For example

\begin{figure}[H]  
\centerline{\includegraphics[scale=0.65]{5.1.png}}  
\caption{Confusion matrix for PPMI dataset}.  
\label{I1}  
\end{figure}  
\begin{figure}[H]  
\centerline{\includegraphics[scale=0.65]{5.2.png}}  
\caption{Confusion matrix for BCCD dataset}.  
\label{I1}  
\end{figure}

So the above lines will place 2 images one below the other
The [H] also works for a table.

2
  • by default H will give an errror as an unknown float option Jun 19, 2023 at 6:10
  • yes. Please add the use of the package at the beginning to get rid of the error \usepackage{float} Jun 20, 2023 at 3:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .