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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.