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}
Misha
  • 569

2 Answers2

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

Austin Downey
  • 514
  • 4
  • 20
Fran
  • 80,769
  • 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 '13 at 09: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 '13 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 '13 at 09: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 '13 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 '15 at 15:44
  • I also had to increase the floatpagefraction as follows: \renewcommand{\floatpagefraction}{.8} – gizzmole Oct 02 '20 at 09:30
  • You are a life saver! Wish I could give more upvotes :-) – Abu Shoeb Apr 01 '21 at 15:42
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.

KersouMan
  • 1,850
  • 3
  • 13
  • 15