[Tex/LaTex] How to get image and text on same page

floatspositioning

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}

Best Answer

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

Related Question