I have a macro that replaces a word in the middle of the paragraph and figure (inside a wrapfigure environment). But the text justification looks awful and it seems to be because wrapfigure cannot be used in the middle of the text (as pointed out here: Wrapping images in text without inducing ugly justified text). I would like to move the wrapfigure to the beginning or end of the paragraph.

In other words. Currently, this:

This is a \mycommand{long} text.


This is a \textbf{long}\begin{wrapfigure}...\end{wrapfigure} text.

but should be:

This is a \textbf{long} text.
  • 1
    Your best bet would be to use \everypar to implement the wrapficure and use the aux file to communicate. This won't be easy. Sep 29 '15 at 4:17

This works, but I'm not sure if fiddling with \everypar can break something, specially when used inside lists:


\def\insertedfigure{\fbox{Inserted figure or whatever}\everypar=\oldeverypar\par}


\lipsum*[66] This is \mycommand{another} test. \lipsum[66]



Basically, \mycommand puts its argument in boldface and stores in \everypar the token which will expand into the required figure (which will be output when the current paragraph is finished). It also saves the current value of \everypar, and restores it after the figure is output.

The result for this example is:


Another example, closer to your question, is providing the following definition for \insertedfigure:

\caption{Example figure}

Which produces:


However, as you can see, this makes the figure to appear in the next paragraph, which I guess is not ok, despite the question title.

  • One can't use wrapfig in lists anyway.
    – egreg
    Oct 1 '15 at 17:23

Simply include as second argument of \mycommand the rest of the paragraph, i.e.:

This is a \mycommand{long}{text.}

Then you can format #1 as bold, then just write #2\par and finally the wrapfigure, without the any magic TeX.


\def\mycommand#1#2{\textbf{#1} #2\par
\caption{Example figure}

\def\a{This is the first paragraph. }

\def\b{This is another long paragraph. }


This is a \mycommand{long}{text. \a\a\a\a\a\a\a}



However, with or wihout \everypar, this not ensure that the wrapfigure is placed in a good position. I see little advantage of this macro over another with only the figure that is moved manually as needed in the final draft.

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.