# Undesired behavior: compiler skips figure onto the next page no matter what

This one is driving me crazy, I really hope somebody can give me a hint about what's going on...

In this print-screen you can see the code compared to the compiled result:

No matter how small I make the image (0.1\textwidth, 0.01\textwidth ...) I can't make it appear in the right page. The image goes alone to the next page.

The image is 2506x2014 pixels, so with 0.3\textwidth and two lines of text under the figure there is plenty of space at the bottom of the page.

If you need any other piece of info about the code please tell me.

Edit: Uploaded the simplified version of .tex projects with the images (it compiles just fine and skips the image just as before), it can be downloaded here: http://www.mediafire.com/download/uztssz91s9yyyru/latex_test.zip

\documentclass[12pt]{book}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}

\renewcommand{\familydefault}{\sfdefault} % modern style
\usepackage{graphicx}

\usepackage{multicol}
\setlength\columnsep{20pt}

\pagenumbering{alph}
\pagestyle{plain} % page numbers but no headers

\usepackage[hmargin=1.9cm,vmargin=1.9cm,footskip=1cm]{geometry}

\begin{document}\large

\section{La columna vertebral}

\begin{figure}[!htb]
\centering
\rule{3cm}{13cm}

\caption[Columna vertebral adulta] % index text
{Columna vertebral adulta. 7 vértebras cervicales, 12 torácicas, 5
lumbares, 5 sacras y las 4 últimas, fusionadas, forman el
cóccix.} % text under figure
\label{} % for ref and pageref
\end{figure}

\begin{multicols}{2}

\textbf{La columna vertebral adulta} tiene curvas en forma de
intervertebrales cartilaginosos. Si las curvas están bien
estructuradas, la musculatura del tronco trabaja de una manera
armónica y habitualmente se adoptan posturas correctas, los discos

En el caso de \textbf{la columna vertebral infantil}, dentro del útero
materno, el feto descansa con la columna vertebral flexionada y las
piernas dobladas. Por ello, al principio, el recién nacido tiene la
columna vertebral doblada hacia delante y no tiene las curvas en S
como el adulto. Poco a poco, se va formando la curvatura lumbar cuando
el bebé yace estirado de espalda sobre una superficie semidura. Más
adelante, cuando se pone en la posición denominada de la esfinge, boca
abajo y apoyado sobre los antebrazos estira la cabeza (ver Fig.5.3),
se va reforzando la musculatura que recubre la columna y se va
formando la curvatura de la zona cervical.

Las curvas en S van alcanzando su mayor desarrollo cuando el niño
empieza a andar y no están bien formadas hasta los cuatro años,
aproximadamente. (Fig. \ref{006}).La forma definitiva de la columna
vertebral se alcanza entre los cinco y los siete años.

\end{multicols}

\begin{figure}[!htb]
\centering
\rule{2cm}{5cm}

\caption[Columna vertebral bien formada] % index text
{Columna vertebral bien formada: A: curva cervical / B: curva
torácica / C: curva lumbar / D: curva sacra.} % text under figure
\label{006} % for ref and pageref
\end{figure}

% New subsection:
\subsection{Principales causas de los problemas de columna}

\textbf{a) Falta de entrenamiento de la musculatura}

\begin{figure}[!htb]
\centering
\rule{1cm}{1cm}

\caption[Bebé con el tronco inmovilizado] % index text
{Es aconsejable tener poco tiempo al bebé con el tronco
\label{008} % for ref and pageref
\end{figure}

\end{document}

• Word of advice: Instead of using the center environment, use \centering, it does not introduces extra white space. Omit the optional argument for the table and figure environments (i.e. [!htbp]) and let LaTeX do it's job. you can influence the output when polishing the document at the very end, but not at the writing stage. – Johannes_B Aug 18 '15 at 7:54
• @Johannes_B Thanks. Tried the suggestion on the three figures of this \section (because I assume that the problem has to be enclosed between the two \clearpage surrounding this section) and LaTeX did a messy job even placing one figure before the \section tag (??) which is not acceptable. I already am at the polishing stage, preparing the document to be printed. I really did not expect this kind of error, i makes no sense to me... there is enough space, I should be able to place it there. – Xavier Peña Aug 18 '15 at 8:04
• All we have right now is a screenshot, no code to test. Providing testable code (i.e. a minimal working example) might be quite hard to ge in your case. You can leave out the figure environment completely and use package capt-of to get a caption. – Johannes_B Aug 18 '15 at 8:14
• Might be an idea to provide the document and the figures for others to download and test. – daleif Aug 18 '15 at 9:38
• it is a bug in multicol it seems: \@textfloatsheight is not properly managed. Will sort this out further during the day – Frank Mittelbach Aug 19 '15 at 7:25

This is a bug in multicol similar to the one recently found about marginpars. At the end of a page LaTeX's normal output routine resets the variables \@mparbottom and \@textfloatsminheight. The multicol package neglected to do so, thus \@textfloatsminheight still contained the height from the here float from the first page --- as a result the available space calculations got off the track.

So the problem only appears when there is a "here" float on the page where multicols starts and another one on the page where it ends.

Try the following fix placed after loading the multicol package until I'm able to upload a new version to CTAN (you need version 1.8m for the patch to succeed):

\usepackage{etoolbox}

\makeatletter
\patchcmd{\multi@column@out}
{ \global \@mparbottom \z@}
{ \global \@mparbottom \z@ \global\@textfloatsheight\z@}
{\typeout{patched}}
{\ERROR}              % the patch failed for some reason
\makeatother

• Thank you very much! Excuse me but I am trying to make this patch work and I haven't succeeded yet, most likely due to my lack of expertise on the matter... I presume this patch goes in the definition section on the top, but I get this error: ! Undefined control sequence. <argument> \ERROR . Otherwise I will wait for the upload. By the way, based on your answer I tried adding some dummy characters before and after the multicols and the problem persisted. – Xavier Peña Aug 19 '15 at 10:54
• @Tremor: You should put the patch behind \usepackage{multicol} – Ulrike Fischer Aug 19 '15 at 11:07
• @Tremor the patch is changing multicol so it has to come after loading that package as Ulrike said. As to "immediately":: not that immediate. The problem is that the first "h" float comes on the page where multicols starts and the second on the page where it ends. – Frank Mittelbach Aug 19 '15 at 11:19
• I am really sorry but I don't seem to be able to make the code work. I tried to follow all your specifications, trying any permutation that I could think of, but every single time LaTeX is telling me that \ERROR is not recognized. This was my best guess so far, please correct me if I'm wrong: pastebin.com/raw.php?i=f2GkwnvJ – Xavier Peña Aug 19 '15 at 13:05
• @Tremor my guess is that you are trying to patch against an old version of multicol, the patch requires 1.8m or perhaps one version earlier – Frank Mittelbach Aug 19 '15 at 16:24

All I can surmise is that the figure can't fit, contrary to your statements in the question.

Like the commenters above say, get rid of \begin{center}...\end{center} because it adds additional (and unwanted) vertical space. Here's an example that shows that you should be able to squeeze such an image onto the page:

\documentclass[a4paper]{article}
\usepackage[margin=3cm]{geometry}
\def\1{text }\def\2{\1\1\1\1\1}\def\3{\2\2\2\2\2}\def\4{\3\3\3\3}
\begin{document}
\4

\begin{figure}[!htbp]
\centering
\rule{0.25\textheight}{0.3\textheight}
\caption{\3}
\end{figure}

\4

\begin{figure}[!htbp]
\centering
\rule{0.25\textheight}{0.3\textheight}
\caption{\3 \3}
\end{figure}
\end{document}


If you want to ensure that it's not the figure placement algorithm that's moving the float, you can insert a non-floating figure in a couple of ways. One is:

\usepackage{float}
...
\begin{figure}{H}


I usually do not recommend this, but if you're in the final stages before publication then it's a lot easier to justify baking in the typesetting decisions like this.

• I tried the \centering and it didn't work (in fact the uploaded .tex contains \centering already). To me it would be weird that it would be a problem of "I don't have enough space to fit the figure in", because there seems to be space and if I set the image size at 0.01\textwidth there is no way the figure fits in the blank space at the bottom of the page... of course there is an error somewhere, but I can't figure out where. – Xavier Peña Aug 18 '15 at 10:09
• everyone, you included, have missed the fact that only the width changes when that is specified. try specifying scale or height, which should have a more useful effect. – barbara beeton Aug 18 '15 at 12:21
• @barbarabeeton That's what I included the size of the image (2506x2014 pixels) in order to show the proportions. In the document I work with \textwidth for practical reasons, but of course I keep in mind the height (which being proportional it is not that much of a hassle). This is also what I was pointing out that making it 0.01\textwidth didn't even let the image appear in the right place. I agree with Will Robertson that at this point the most likely explanation would be some kind of weird interaction with multicols. – Xavier Peña Aug 18 '15 at 12:44
• Also a \clearpage right after the multicols allows them to be on the same page. I think we need David Carlisle to answer this. – daleif Aug 18 '15 at 12:59
• It does fit, and like Tremor I think it has to do with multicols. There seems to be to many floats around it. I would use float and [H] e.g. for the float directly after the section command. – Ulrike Fischer Aug 18 '15 at 13:44