# Move command to the beginning or end of the paragraph

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.


becomes:

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


but should be:

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

• Your best bet would be to use \everypar to implement the wrapficure and use the aux file to communicate. This won't be easy. – John Kormylo 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:

\documentclass{article}
\usepackage{lipsum}

\newtoks\oldeverypar
\def\insertedfigure{\fbox{Inserted figure or whatever}\everypar=\oldeverypar\par}
\def\mycommand#1{\textbf{#1}\oldeverypar=\everypar\everypar={\insertedfigure}}

\begin{document}
\lipsum[66]

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

\lipsum[66]

\end{document}


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:

\def\insertedfigure{%
\begin{wrapfigure}{R}{0.5\textwidth}
\centerline{\includegraphics[width=0.4\textwidth]{example-image}}
\caption{Example figure}
\end{wrapfigure}%
\everypar=\oldeverypar\par}


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.

\documentclass[11pt]{article}
\usepackage{wrapfig,graphicx}

\def\mycommand#1#2{\textbf{#1} #2\par
\begin{wrapfigure}{R}{0.35\linewidth}
\centering\includegraphics[width=0.3\linewidth]{example-image-a}
\caption{Example figure}
\end{wrapfigure}\par}

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

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

\begin{document}

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

\b\b\b\b\b\b\b\b\b

\end{document}


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.