1

I want to wrap explanatory text (anything from a sentence to multiple paragraphs) around some topic text using wrapfig. As long as the explanatory text is longer than the topic text, the code works as expected. If the topic text is longer than the explanatory text, the the topic text overprints the text that follows in the next paragraph. In other words, I'd like the wrapped material to keep its natural height. I have also tried cutwin and insbox without success.

\documentclass[11pt]{article}

\usepackage{xparse,wrapfig,lipsum}
\usepackage[margin=1.75in,top=0.5in]{geometry}

%%% =====8><----- %%%

\NewDocumentCommand{\step}{+mm}{%
    \ignorespaces
    \par
    \noindent
    {%%
        \begin{minipage}{\textwidth}
            \begin{wrapfigure}{o}{0pt}
                \parbox[t]{0.4\textwidth}{%
                    \itshape#2
                }%
            \end{wrapfigure}\par
            #1\par
        \end{minipage}
    }%
    \par
    \bigskip
}

\setlength{\intextsep}{0pt}

\begin{document}

\noindent This is the current output:

\bigskip

\step{1 This is a little text. And this is some more bla bla to see just how this works out.}{\lipsum*[3][1-3]}

\step{2 This is a little text. And this is some more bla bla to see just how this works out.}{\lipsum*[3][1-2]}

\step{3 This is a little text. And this is some more bla bla to see just how this works out. This is a little text. And this is some more bla bla to see just how this works out.
This is a little text. And this is some more bla bla to see just how this works out.}{\lipsum*[3][1-2]}

\step{4 This is a little text. And this is some more bla bla to see just how this works out. This is a little text. And this is some more bla bla to see just how this works out.
This is a little text. And this is some more bla bla to see just how this works out.}{\lipsum*[3][1-2]}

\vspace{0.5in}

\noindent This is the desired output, but, hopefully, without the manual intervention:

\bigskip

\step{1 This is a little text. And this is some more bla bla to see just how this works out.}{\lipsum*[3][1-3]}

\vspace{3\baselineskip}

\step{2 This is a little text. And this is some more bla bla to see just how this works out.}{\lipsum*[3][1-2]}

\vspace{2\baselineskip}

\step{3 This is a little text. And this is some more bla bla to see just how this works out. This is a little text. And this is some more bla bla to see just how this works out.
This is a little text. And this is some more bla bla to see just how this works out.}{\lipsum*[3][1-2]}

\step{4 This is a little text. And this is some more bla bla to see just how this works out. This is a little text. And this is some more bla bla to see just how this works out.
This is a little text. And this is some more bla bla to see just how this works out.}{\lipsum*[3][1-2]}

\vfill

\end{document}

wrapfig sample

1 Answer 1

2

Wrapfig provides the counter WF@wrappedlines with the number of lines left to wrap.

\documentclass[11pt]{article}

\usepackage{xparse,wrapfig,lipsum}
\usepackage[margin=1.75in,top=0.5in]{geometry}

%%% =====8><----- %%%

\NewDocumentCommand{\step}{+mm}{%
    \ignorespaces
    \par
    \noindent
    {%%
        \begin{minipage}{\textwidth}
            \begin{wrapfigure}{o}{0pt}
                \parbox[t]{0.4\textwidth}{%
                    \itshape#2
                }%
            \end{wrapfigure}\par
            #1\par
            \loop\ifnum\value{WF@wrappedlines}>1
              \strut\par
            \repeat
        \end{minipage}
    }%
    \par
    \bigskip
}

\setlength{\intextsep}{0pt}

\begin{document}

\noindent This is the current output:

\bigskip

\step{1 This is a little text. And this is some more bla bla to see just how this works out.}{\lipsum*[3][1-3]}

\step{2 This is a little text. And this is some more bla bla to see just how this works out.}{\lipsum*[3][1-2]}

\step{3 This is a little text. And this is some more bla bla to see just how this works out. This is a little text. And this is some more bla bla to see just how this works out.
This is a little text. And this is some more bla bla to see just how this works out.}{\lipsum*[3][1-2]}

\step{4 This is a little text. And this is some more bla bla to see just how this works out. This is a little text. And this is some more bla bla to see just how this works out.
This is a little text. And this is some more bla bla to see just how this works out.}{\lipsum*[3][1-2]}

\end{document}
2
  • Fascinating: I definitely would not have thought of that on my own. Thanks. Also surprised to learn that \value{WF@wrappedlines) works without a \makeatletter and \makeatother pair.
    – sgmoye
    Dec 12, 2020 at 15:36
  • @ is legal in counters and names in general, just not macro names. Incidentally \value{name} becomes \c@name. Dec 12, 2020 at 15:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .