0

I like to place floats within paragraphs, so that the text wraps around them (not left and right). When I'm done with the writing, I fine-tune the lines that come before/after the float in the source file to arrange the lines around the float in a way I find pleasing. This is cumbersome, so I'm looking for an automatic way to do this. Either to balance the lines around the float (e.g. 5 lines before it, five lines after it) or to specify how many lines I want before/after.

Here is a MWE showing what I want to do:

\documentclass{article}
\usepackage[demo]{graphicx} 

\begin{document}
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 
    Viverra adipiscing at in tellus integer feugiat scelerisque varius morbi. 
    \begin{figure}[htb]
        \centering
        \includegraphics[width = 0.8\textwidth]{foobar}
    \end{figure}
    Lacinia quis vel eros donec ac. 
    Tristique et egestas quis ipsum suspendisse ultrices gravida. 
    Integer enim neque volutpat ac tincidunt vitae. 
    Pellentesque massa placerat duis ultricies lacus sed turpis tincidunt id. 
    At ultrices mi tempus imperdiet nulla malesuada. 
    Nunc mattis enim ut tellus elementum sagittis vitae et. 
    Euismod quis viverra nibh cras pulvinar. 
\end{document}

This yields enter image description here

I could also format it like this (where I only moved to definition of the figure a bit down):

\documentclass{article}
\usepackage[demo]{graphicx} 

\begin{document}
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 
    Viverra adipiscing at in tellus integer feugiat scelerisque varius morbi. 
    Lacinia quis vel eros donec ac. 
    Tristique et egestas quis ipsum suspendisse ultrices gravida. 
    Integer enim neque volutpat ac tincidunt vitae. 
    Pellentesque massa placerat duis ultricies lacus sed turpis tincidunt id. 
    \begin{figure}[htb]
        \centering
        \includegraphics[width = 0.8\textwidth]{foobar}
    \end{figure}
    At ultrices mi tempus imperdiet nulla malesuada. 
    Nunc mattis enim ut tellus elementum sagittis vitae et. 
    Euismod quis viverra nibh cras pulvinar. 
\end{document}

which leads to enter image description here

TL;DR: How can I fine-tune where exactly a float appears within a paragraph?

3

The only way of placing a float mid-page is to use h which means the float comes exactly where you place it in the source (or rather just after the line that contains that point after the paragraph is broken in to lines). LaTex never floats mid-page floats within the page.

If the float is floated then it will always be positioned using one of tbp so positioned at the top or bottom of a page, or on a float page with only floats and no text.

3
  • The cutwin package enables the creation of "windows" in paragraphs, an specify how-many line should be passed before the start of the winnow. This is definitely lower level and non floating, perhaps non recommended, but likely matches what you are looking for.for. – Jhor Mar 19 '20 at 12:06
  • @Jhor that's true but maybe better if you posted an answer showing that rather than comment here (which can easily get lost) – David Carlisle Mar 19 '20 at 12:19
  • Not the answer I was hoping for, but it makes sense, I guess. Thanks! – pschulz Mar 19 '20 at 12:41
2

As said by @DavidCarlisle, and many others around this site, it would be better to get rid of the floating figure environment and its h option if you really want an absolute positioning. And the latter is not really recommended... A common strategy is then to use a minipage, in conjunction with the caption package that enables to put a \caption in a non-floating environment.

In this answer I address your specific request "Specifying number of lines"

Contrary to what I said in the comment, the cutwin package is not really suited to achieve the result that you suggest in the picture. Indeed, it aims to wrap text around the "window", and if one ask for zero margin on both sides, it persists to put some text on both sides, which overflows on the margin and the figure content.

Hence in the following MWE, I use the package insbox.tex which enables to perform exactly what you ask for, except that the number of lines above is not fixed as a number (as I would be with cutwin) but is defined by the position where you call the \InsertBoxC command, in some sense more close to the strategy that you suggest in you initial code.

For the sake of completeness I keep an example of cutwin use, which, as shown in the question Custom built universal wrapfig using cutwin, could be used to overcome some limitations of the widely used wrapfig package.

Here are the MWE (the text comes from the documentation of cutwin) :

\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[margin=25mm,verbose=false]{geometry}
\usepackage{graphicx}
\usepackage{caption}      % if you want a caption
\usepackage{cutwin}
\input{insbox.tex}

%%-------------------
% what follows is for demonstration  purpose only
% and must be suppressed in real use.
\usepackage{xcolor} 
\usepackage{showframe}                           
\renewcommand*\ShowFrameColor{\color{red}}
\setlength{\fboxsep}{0pt}
\renewcommand{\fbox}[1]{\fcolorbox{red}{white}{#1}}
\usepackage{mwe}
%%-------------------

\begin{document}
\section {With \texttt{cutwin} package}

\vspace*{2\baselineskip} %% skip needed as cutwin performs a vertical shift

\opencutcenter
\renewcommand{\windowpagestuff}{\fbox{\begin{minipage}{\linewidth}%
\centering\includegraphics[width=0.75\linewidth]{example-image-golden}%
\captionof{figure}{Example figure}\par\end{minipage}}}

\begin{cutout}{3}{0.3\textwidth}{0.3\textwidth}{8}
A rectangular space can be placed in a paragraph with the text flowing across 
the gap. The space may break open into the top or side of the paragraph or, 
with some care,  into the bottom (the number of lines specified 
for the cutout should not exceed the amount of text available for those lines). 
Some text or a logo or graphic may be placed within the window, or it may be 
left  empty. In this instance  I have put three short bold text lines in the 
window opening. The window should not be too wide as it can be difficult to 
track the exterior text lines across the gap. A rectangular space can be placed 
in a paragraph with the text flowing across the gap. The space may break open 
into the top or side of the paragraph or, with some care, into the bottom.
\end{cutout}

\section {With \texttt{inbox.tex}}

\newsavebox{\inscontent}
\savebox{\inscontent}{\fbox{\begin{minipage}{\textwidth}%
\centering\includegraphics[width=0.30\linewidth]{example-image-golden}%
\captionof{figure}{Example figure}\par%
\end{minipage}}}

A rectangular space can be placed in a paragraph with the text flowing across 
the gap. The space may break open into the top or side of the paragraph or, 
with some care,  into the bottom (the number of lines specified 
\InsertBoxC{\usebox{\inscontent}} 
for the cutout should not exceed the amount of text available for those lines). 
Some text or a logo or graphic may be placed within the window, or it may be 
left  empty. In this instance  I have put three short bold text lines in the 
window opening. The window should not be too wide as it can be difficult to 
track the exterior text lines across the gap. A rectangular space can be placed 
in a paragraph with the text flowing across the gap. The space may break open 
into the top or side of the paragraph or, with some care, into the bottom.

\end{document}

and the result :

enter image description here

Remarks:

  • The red lines are used here in order to show the limit of the boxes. They will be suppressed by: 1 discarding the packages and settings between the two %%-------- 2 removing the \fbox in the body code

  • In both approaches the content of the figure must be put in a savebox which is predefined in cutwin but must be declared for insbox. The use of a minipage is convenient if one want to use a \caption, a \label or any other content grouped with the picture, but is not mandatory.

  • The file insbox.texis not a LaTeX package (i.e. a .sty file) but a .tex file (that could be used too with Plain-TeX) and must be loaded with \input and not with \usepackage.

  • this is far to be automatic as you asked, but this is rather usual in LaTeX, when if you want to get control of what LaTeX would do by itself.

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.