6

I am trying to wrap two pictures in a paragraph, as shown in the two images below. The two red boxes show the desired locations for the two figures.enter image description here

Unfortunately, I cannot get the following code to work what I am looking for.

\documentclass{article}

\usepackage{wrapfig}
\usepackage{lipsum}

\begin{document}

\lipsum[1]

\begin{wrapfigure}{R}{3cm}
    \centering
    \rule{3cm}{3cm}
\end{wrapfigure}

\begin{wrapfigure}{L}{3cm}
    \centering
    \rule{3cm}{3cm}
\end{wrapfigure}

\lipsum[1-6]

\end{document}

How can I exactly specify the locations of the wrapfigures?

5

Here is a manual manipulation using \parshape. Since you didn't provide actual measurements of the images to be included, your setup will most likely be slightly different. However, the usage would be very similar:

enter image description here

\documentclass{article}

\usepackage{graphicx}

% https://tex.stackexchange.com/a/133660/5764
\makeatletter
\def\newparshape{\parshape\@npshape0{}}
\def\@npshape#1#2#3{\ifx\\#3\expandafter\@@@npshape\else\expandafter\@@npshape\fi
  {#1}{#2}{#3}}
\def\@@npshape#1#2#3#4#5{%
  \ifnum#3>\z@\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
  {\expandafter\@@npshape\expandafter{\the\numexpr#1+1\relax}{#2 #4 #5}{\numexpr#3-1\relax}{#4}{#5}}%
  {\@npshape{#1}{#2}}}
\def\@@@npshape#1#2#3{#1 #2 }
\makeatother

\begin{document}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vitae mauris risus. Sed 
varius dui diam, sit amet volutpat risus euismod in. Ut ac dui fermentum, laoreet arcu 
volutpat, vestibulum urna. Integer vitae odio at neque imperdiet sollicitudin vel vel enim. 
Nullam sodales facilisis magna sit amet tempor. Morbi ut nulla sem. Aliquam sagittis, 
lacus auctor tincidunt semper, odio dui lacinia lectus, quis tempus quam leo sed mi. 
Maecenas eu mauris dolor.

% Placement of top-left image
\newparshape
  {5}{\dimexpr3cm+\tabcolsep}{\dimexpr\textwidth-3cm-\tabcolsep}% Unique to top-left image
  {3}{0pt}{\textwidth}% full-width lines where no image is present
  {6}{0pt}{\dimexpr\textwidth-3cm-\tabcolsep}\\% Unique to bottom-right image
\noindent\leavevmode
\llap{%
  \raisebox{\dimexpr-\height+\ht\strutbox}[0pt][0pt]{%
    \includegraphics[width=3cm,height=4.5\baselineskip]{example-image-a}}%
  \hspace*{\tabcolsep}}%
Pellentesque porta pharetra leo, ut accumsan diam feugiat sit amet. Ut mattis nulla sed 
sapien suscipit, eu sodales elit cursus. Vestibulum sit amet sollicitudin nisl. Aenean 
vestibulum purus sed tincidunt convallis. Aliquam ut sodales risus. Mauris in mauris a 
ante venenatis faucibus. Ut nunc ante, cursus sit amet iaculis eget, imperdiet quis libero. 
Pellentesque laoreet dolor eget consectetur vehicula. Maecenas ultrices vehicula ultricies. 
Morbi cursus risus ut eros placerat condimentum. Mauris nec varius lectus. Nulla adipiscing 
porttitor tellus vitae congue. Aliquam commodo gravida erat, lacinia vehicula enim mollis 
nec. Nulla sed metus gravida, faucibus neque eu, rutrum purus. Integer ornare nunc vel 
dolor lacinia, sit amet venenatis turpis porttitor. Fusce lobortis magna eget odio iaculis, 
sed blandit urna dictum.%
% Placement of bottom-right image
\hfill\rlap{\hspace*{\tabcolsep}%
  \raisebox{\dimexpr-\height+5\baselineskip+\ht\strutbox}[0pt][0pt]{%
    \includegraphics[width=3cm,height=4.5\baselineskip]{example-image-b}}}

Morbi id dolor vulputate, euismod sapien nec, condimentum tellus. Fusce tincidunt sem non 
turpis ultricies, eu egestas lorem posuere. Integer risus lectus, gravida vel tincidunt sit 
amet, vestibulum eget leo. Curabitur vitae enim sem. Vivamus rutrum velit eget justo 
condimentum egestas. Phasellus sit amet lacus mauris. Sed quis aliquet tellus. In mauris 
ante, porttitor sit amet rhoncus et, ultrices quis elit. Proin sed aliquet eros.
\end{document}

We first set the layout of the paragraph which shape has to be changed using \newparshape{<num>}{<indent>}{<width>}...{<num>}{<indent>}{<width>}\\. So, using the code above, we set 3 different line definitions:

  • {5} lines with an indent of 3cm+\tabcolsep (to avoid overwriting the actual image) and a width of \textwidth-3cm-\tabcolsep (the remainder of \textwidth minus the width of the image and the gap between the image and text);
  • {3} lines with no indent (0pt) and full width (\textwidth); and
  • {6} lines with no indent (0pt) and width \textwidth-3cm-\tabcolsep.

Remember that we have to set the paragraph shape before the paragraph starts.

The top-left image is placed just before the paragraph, while the bottom-right image is placed at the end of the paragraph. Both images are dropped/raised (using \raisebox{<height>}) into position based on the number on

  • the number of lines they occupy,
  • their height, and
  • the height of \strutbox.

Essentially they're positioned in such a way that they both have zero height/depth (resulting from the [0pt][0pt] arguments to \raisebox).

\tabcolsep is the horizontal gap between the image(s) and the text.

\newparshape was taken from More condensed version of \parshape.

I've added a \noindent, but that is probably up to you whether you want to use it or not.


Here is a different setup where the two images share some common shortened lines within the same paragraph:

enter image description here

\documentclass{article}

\usepackage{graphicx}

% https://tex.stackexchange.com/a/133660/5764
\makeatletter
\def\newparshape{\parshape\@npshape0{}}
\def\@npshape#1#2#3{\ifx\\#3\expandafter\@@@npshape\else\expandafter\@@npshape\fi
  {#1}{#2}{#3}}
\def\@@npshape#1#2#3#4#5{%
  \ifnum#3>\z@\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
  {\expandafter\@@npshape\expandafter{\the\numexpr#1+1\relax}{#2 #4 #5}{\numexpr#3-1\relax}{#4}{#5}}%
  {\@npshape{#1}{#2}}}
\def\@@@npshape#1#2#3{#1 #2 }
\makeatother

\begin{document}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vitae mauris risus. Sed 
varius dui diam, sit amet volutpat risus euismod in. Ut ac dui fermentum, laoreet arcu 
volutpat, vestibulum urna. Integer vitae odio at neque imperdiet sollicitudin vel vel enim. 
Nullam sodales facilisis magna sit amet tempor. Morbi ut nulla sem. Aliquam sagittis, 
lacus auctor tincidunt semper, odio dui lacinia lectus, quis tempus quam leo sed mi. 
Maecenas eu mauris dolor.

\newparshape
  {7}{\dimexpr3cm+\tabcolsep}{\dimexpr\textwidth-3cm-\tabcolsep}% Unique to top-left image
  {2}{\dimexpr3cm+\tabcolsep}{\dimexpr\textwidth-6cm-2\tabcolsep}% Between top-left and bottom-right image
  {6}{0pt}{\dimexpr\textwidth-3cm-\tabcolsep}\\% Unique to bottom-right image
\noindent\leavevmode
\llap{%
  \raisebox{\dimexpr-\height+\ht\strutbox}[0pt][0pt]{%
    \includegraphics[width=3cm,height=9\baselineskip]{example-image-a}}%
  \hspace*{\tabcolsep}}%
Pellentesque porta pharetra leo, ut accumsan diam feugiat sit amet. Ut mattis nulla sed 
sapien suscipit, eu sodales elit cursus. Vestibulum sit amet sollicitudin nisl. Aenean 
vestibulum purus sed tincidunt convallis. Aliquam ut sodales risus. Mauris in mauris a 
ante venenatis faucibus. Ut nunc ante, cursus sit amet iaculis eget, imperdiet quis libero. 
Pellentesque laoreet dolor eget consectetur vehicula. Maecenas ultrices vehicula ultricies. 
Morbi cursus risus ut eros placerat condimentum. Mauris nec varius lectus. Nulla adipiscing 
porttitor tellus vitae congue. Aliquam commodo gravida erat, lacinia vehicula enim mollis 
nec. Nulla sed metus gravida, faucibus neque eu, rutrum purus. Integer ornare nunc vel 
dolor lacinia, sit amet venenatis turpis porttitor. Fusce lobortis magna eget odio iaculis, 
sed blandit urna dictum.
\hfill\rlap{\hspace*{\tabcolsep}%
  \raisebox{\dimexpr-\height+8\baselineskip+\ht\strutbox}[0pt][0pt]{%
    \includegraphics[width=3cm,height=9\baselineskip]{example-image-b}}}

Morbi id dolor vulputate, euismod sapien nec, condimentum tellus. Fusce tincidunt sem non 
turpis ultricies, eu egestas lorem posuere. Integer risus lectus, gravida vel tincidunt sit 
amet, vestibulum eget leo. Curabitur vitae enim sem. Vivamus rutrum velit eget justo 
condimentum egestas. Phasellus sit amet lacus mauris. Sed quis aliquet tellus. In mauris 
ante, porttitor sit amet rhoncus et, ultrices quis elit. Proin sed aliquet eros.
\end{document}
  • 1
    All right, what I do not like is that you need to handle many internal commands of LaTeX (or other package) and to use TeX primitives to reach this. It would be interesting to implement it in a package or to improve wrapfig. To non-expert user is difficult to remember this. – skpblack Jul 30 '14 at 23:18
  • @skpblack: Oh well, now you can't please everyone, can you. You can write up a package and post an answer here when it becomes available, if you're interested. ;) – Werner Jul 30 '14 at 23:20
  • It seems it needs a lot of calculations to be able to put it into the desired place. would there potentially another approach rather than \parshape? Is it also possible to add a caption to the figures? – AFP Jul 30 '14 at 23:36
  • 2
    @A2009: Don't get bowled over by \newparshape. Read the post More condensed version of \parshape to understand the input. Then the trouble is only fiddling around with the location. Your setup is non-standard, so anything you try may have to require some finicky coding... – Werner Jul 30 '14 at 23:39
  • 1
    @A2009: I've added some elaboration... – Werner Jul 31 '14 at 1:25

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.