0

I always put my float environments at the end of the paragraph where it was first referenced.

But I have a problem when the paragraph is split over two pages (beginning at the bottom of the first page and ending at the top of the second page). The float could be placed easily at the top of the first page, because it is referenced directly in the first sentence of the paragraph which is on the first page even if the float is on this page too. But Latex puts the float on the second page resulting in a half empty first page (because of the now missing float) and a half empty second page (because the float is now the only thing on this page).

Is there an easier way than manually move the float environment in the source before the paragraph (with all the problems when the above mentioned condition no longer holds, e.g. when additional text is inserted)?

MWE (with the float on the second page)

\documentclass{article}

\usepackage{algorithm2e}
\usepackage[nopar]{lipsum}

\begin{document}
\null
\vfill

\lipsum[1-3]


In the first sentence I reference the float~\ref{alg:Algo}. After the first sentence there are many more sentences and the float ist put after the whole paragraph. After the first sentence there are many more sentences and the float ist put after the whole paragraph. After the first sentence there are many more sentences and the float ist put after the whole paragraph. After the first sentence there are many more sentences and the float ist put after the whole paragraph. After the first sentence there are many more sentences and the float ist put after the whole paragraph.
\begin{algorithm}[tbp]
\SetKwInOut{Input}{Input}
  \SetKwInOut{Output}{Output}

\LinesNumbered
\caption{Caption}
\label{alg:Algo}
\Input{-}
\Output{-}
\BlankLine

First Line\;
First Line\;
First Line\;
First Line\;
First Line\;


\Return{result}\;
\end{algorithm}

\end{document}

MWE (with the float on the first page)

\documentclass{article}

\usepackage{algorithm2e}
\usepackage[nopar]{lipsum}

\begin{document}
\null
\vfill

\lipsum[1-3]



\begin{algorithm}[tbp]
\SetKwInOut{Input}{Input}
  \SetKwInOut{Output}{Output}

\LinesNumbered
\caption{Caption}
\label{alg:Algo}
\Input{-}
\Output{-}
\BlankLine

First Line\;
First Line\;
First Line\;
First Line\;
First Line\;


\Return{result}\;
\end{algorithm}
In the first sentence I reference the float~\ref{alg:Algo}. After the first sentence there are many more sentences and the float ist put after the whole paragraph. After the first sentence there are many more sentences and the float ist put after the whole paragraph. After the first sentence there are many more sentences and the float ist put after the whole paragraph. After the first sentence there are many more sentences and the float ist put after the whole paragraph. After the first sentence there are many more sentences and the float ist put after the whole paragraph.

\end{document}
user2653422
  • 2,333
  • 1
    the whole point of floating is to not leave space at page breaks. Are you using [H] ? – David Carlisle Aug 20 '14 at 12:58
  • 1
    Why not simply put the figure directly at the point of reference? blah blah \ref{foo}\begin{figure}....\label{foo}\end{figure} blah blah .... – David Carlisle Aug 20 '14 at 12:59
  • @DavidCarlisle I'm not using H, because I only want floats to be at the top, bottom or on their own page. To put the float directly where it is referenced is possible, but I think it produces ugly code. I have also algorithm floats and they break my paragraph in the code which makes it more difficult to maintain it. – user2653422 Aug 20 '14 at 13:17
  • 1
    Since only you see the code itself, does it matter? You could move it to the end of the sentence. I like to start every sentence on a fresh line anyway, in the editor. – John Kormylo Aug 20 '14 at 13:47
  • @JohnKormylo For me it makes things a bit more complicated, because it breaks the flow when reading a paragraph within the code. But I get your and David's point that this could easily solve my problem, thank for that. Anyway, if someone knows a solution to the initial problem I would be happy to get some hints how to solve the problem automatically. – user2653422 Aug 20 '14 at 14:04
  • It is hard to solve your original problem as it is hard to guess why you are getting spaces at the page break. all questions should have an example document that demonstrates the problem. I asked about H not to suggest you use it, but that was my best guess as to the cause of the problem you describe but that guess was false... – David Carlisle Aug 20 '14 at 14:19
  • You could create a macro with your figure definition (before the paragraph) then expand it at the reference. You could even keep using the same name over and over. BTW, I take it you are using http://tex.stackexchange.com/questions/6157/floating-an-algorithm ? – John Kormylo Aug 20 '14 at 14:29
  • @DavidCarlisle Sorry for that I added two MWE to my initial post. When you compare the two you will see that the desired result is the one in the second MWE. But when I place the float before the paragraph it could lead to some problems when more text will be inserted resulting in the case where I have the float on the first page and the reference on the second. – user2653422 Aug 20 '14 at 14:52
  • @JohnKormylo Could you please explain a bit more on how to define such a macro? – user2653422 Aug 20 '14 at 14:58
  • the examples are as I expect (and not as you described with "half empty page") LaTeX never places a figure on a page earlier than its position in the source so placing it at the end of a long paragraph is bound to have this effect. The solution is as in the original comment to put the figure (or a macro expanding to the figure, which is the same thing really) at the earliest point that you want to allow it, namely at the \ref. – David Carlisle Aug 20 '14 at 15:25

1 Answers1

1

To reuse the macro name use \renewcommand the second time.

\documentclass{article}

\usepackage{algorithm2e}
\usepackage[nopar]{lipsum}

\begin{document}
\null\vfill
\lipsum[1-3]

\newcommand{\InsertAlgorithmHere}{%
\begin{algorithm}[tbp]
\SetKwInOut{Input}{Input}
 \SetKwInOut{Output}{Output}
\LinesNumbered
\caption{Caption}
\label{alg:Algo}
\Input{-}
\Output{-}
\BlankLine
First Line\;
First Line\;
First Line\;
First Line\;
First Line\;
\Return{result}\;
\end{algorithm}}

In the first sentence I reference the float~\ref{alg:Algo}.\InsertAlgorithmHere
After the first sentence there are many more sentences and the float ist put after the whole paragraph. 
After the first sentence there are many more sentences and the float ist put after the whole paragraph.
After the first sentence there are many more sentences and the float ist put after the whole paragraph. 
After the first sentence there are many more sentences and the float ist put after the whole paragraph. 
After the first sentence there are many more sentences and the float ist put after the whole paragraph.

\end{document}
John Kormylo
  • 79,712
  • 3
  • 50
  • 120