0

I have two problems and one additional question.

First problem

The figure wrapping does not work well, the text top and the top of the figure does not align perfectly, there is one extra line. How could could I prevent this?

Second problem

As you can see on the second page, there is gap in the text, I do not want it.

Addition question

Is there a way that, the latex recognize, there is not enough space for the picture, than the figure and the text will start at the top of the page.

Addition information

As you can see, I am using scale=0.35, because there are more than one figure and I want to keep the same scale on each figure and each figure have different width and height.

Code

\documentclass{report}

\usepackage{graphicx} \usepackage{wrapfig}

\begin{document}

%---------------------------------------------------------------------------- \chapter{Lorem} %----------------------------------------------------------------------------

%---------------------------------------------------------------------------- \section{Lorem} %----------------------------------------------------------------------------

\begin{wrapfigure}{l}{\wd0} \centering \includegraphics[scale=0.35]{example-image} \end{wrapfigure}

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut

\begin{wrapfigure}{l}{\wd0} \centering \includegraphics[scale=0.35]{example-image} \end{wrapfigure}

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,

\end{document}

Screenshot

enter image description here

mala97
  • 312
  • begin{wrapfigure}{l}{\wd0} makes the cut in as wide as box0 but you have not set box 0 so this is a completely arbitrary value???? – David Carlisle Dec 21 '22 at 12:18
  • If I only use \begin{wrapfigure}{l}, the I cannot use \centering, and if I do not use \centering then the figure does not appear on the screen. – mala97 Dec 21 '22 at 12:22
  • 2
    the width option is mandatory you can not omit it. I guess you want {0pt} which is a special value to mean "as wide as the figure" You get that accidentally here as box0 happens to be empty in this example so \wd0 is 0pt – David Carlisle Dec 21 '22 at 12:26
  • The additonal question is addressed by the L and R options (see page 2), but instead of moving to the top of the page, it moves to the start of the next paragraph. It is posible to insert wrapfigure into the middle of a paragraph, but the manual positioning is tricky. – John Kormylo Dec 21 '22 at 17:38
  • @JohnKormylo The top means, move to the next page, so basically \newpage command. But the figure should be on the left part of the page. – mala97 Dec 21 '22 at 18:06
  • Ah, you want \needspace (needspace package). – John Kormylo Dec 21 '22 at 18:17
  • @JohnKormylo and exactly how should I use it? – mala97 Dec 21 '22 at 18:35

2 Answers2

6

\intextsep is a vertical space added before and after a float. To align the top line of the paragraph with the wrapped figure, it is possible to set it to 0pt using \setlength{\intextsep}{0pt}. However, to affect only wrapped figures, it is only added to the beginning of the wrapfigure environment.

enter image description here

The second required parameter is the width of the figure. When specifying a width of zero (0pt), the actual width of the figure will be used as the wrapper width.

\documentclass{report}

\usepackage{graphicx} \usepackage{wrapfig}

\usepackage{etoolbox}% added <<<<<<<<<<<<<<<< \BeforeBeginEnvironment{wrapfigure}{\setlength{\intextsep}{0pt}}% only for wrapfigure-- added <<<<<<<<<<<<<<<<

\begin{document}

%----------------------------------------------------------------------------
\chapter{Lorem}
%----------------------------------------------------------------------------

%----------------------------------------------------------------------------
\section{Lorem}
%----------------------------------------------------------------------------

\begin{wrapfigure}{l}{0pt}
    \includegraphics[scale=0.35]{example-image}
\end{wrapfigure}

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus.


\begin{wrapfigure}{l}{0pt}
    \includegraphics[scale=0.35]{example-image}
\end{wrapfigure}

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,

\end{document}

Simon Dispa
  • 39,141
  • Second time you forget to add wrapfigure. But the code does not solve the Second problem. If I copy and paste the your code, and the scale become 0.45, then the second problem occures – mala97 Dec 21 '22 at 13:22
  • @mala97 Corrected! Thank you for your feedback. In specific cases you can use the first optional parameter (number of narrows lines) \begin{wrapfigure}[9]{l}{0pt} in the second figure when scale = 0.45. – Simon Dispa Dec 21 '22 at 13:29
  • Is there a way to solve my Addition question? Or I just have to add \newpage every single time? – mala97 Dec 21 '22 at 13:40
  • @mala97 I don't know of any automatic solution. (There are many manual adjustments that are made to a final version of a document. Even rewrite paragraphs.) – Simon Dispa Dec 21 '22 at 13:51
2

This shows how to use \needspace. The trick is to measure the height needed first.

Actually, one should use a named savebox instead of box 0 unless you are inside a group. It should be okay, but every once in a while it isn't.

\documentclass{report}

\usepackage{graphicx} \usepackage{wrapfig} \usepackage{needspace}

\begin{document}

%---------------------------------------------------------------------------- \chapter{Lorem} %----------------------------------------------------------------------------

%---------------------------------------------------------------------------- \section{Lorem} %----------------------------------------------------------------------------

\sbox0{\includegraphics[scale=0.35]{example-image}}% \needspace{\dimexpr \ht0+2\intextsep}% \begin{wrapfigure}{l}{\wd0} \usebox0
\end{wrapfigure}

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut

\sbox0{\includegraphics[scale=0.35]{example-image}}% \needspace{\dimexpr \ht0+2\intextsep}% \begin{wrapfigure}{l}{\wd0} \usebox0 \end{wrapfigure}

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,

\end{document}


It ocurred to me that one could use \parshape and \afterpage to add a wrapped image at the top of the page. It even uses wrapfigure if the image will fit on this page, or if the image stretches beyond the first paragraph. However, it must be used with only one paragraph left on the page.

The tricky bit was getting \parshape and wrapfigure to work from inside another environment.

\documentclass{article}
\usepackage{wrapfig}
\usepackage{environ}
\usepackage{xparse}
\usepackage{varwidth}
\usepackage{afterpage}

\usepackage{lipsum}% MWE only \usepackage{showframe}

\makeatletter \newsavebox{\wp@image}

\NewDocumentEnvironment{wrappage}{O{0} m O{0} m}{\par % #1 (optional) is the number of lines until the end of the page % #2 is r (right) or l (left) % #3 (optional) is the number of lines for the image. % #4 width (or 0pt to compute). \xdef\wp@page{#1}% \ifnum\wp@page=0\relax \dimen0=\dimexpr \pagegoal-\pagetotal-0.5\baselineskip\relax \xdef\wp@page{\the\numexpr \dimen0/\baselineskip}% \fi \xdef\wp@side{#2}% \xdef\wp@lines{#3}% \xdef\wp@width{#4}% \def@captype{figure}% \wp@environ}% body here {\endwp@environ \ifnum\wp@lines=0\relax \dimen0=\dimexpr \ht\wp@image+\dp\wp@image+\intextsep\relax \xdef\wp@lines{\the\numexpr \dimen0/\baselineskip}% \fi \ifdim \wp@page\baselineskip>\dimexpr\wp@lines\baselineskip+\intextsep\relax% put figure on this page \AddToHookNext{para/before}{\begin{wrapfigure}[\wp@lines]{\wp@side}{\wp@width} \box\wp@image \end{wrapfigure}} \else \if r\wp@side\relax \wp@right% put image on right side \else \if l\wp@side\relax \wp@left% put image on left side \else \errmessage{Bad wrappage argument: \textbf{\wp@side} must be \texbf{r} or \textbf{l}.}% \fi \fi \fi }

\def\wp@right{% handle right side wrapfig \afterpage{\xdef\wp@extra{\the\dimexpr \wp@lines\baselineskip-\ht\AP@partial-\dp\AP@partial}% \noindent\hspace{\dimexpr \textwidth-\wp@width}% \raisebox{\dimexpr \ht\strutbox-\height}[\ht\strutbox][0pt]{\box\wp@image}% \vskip-\baselineskip \unvbox\AP@partial \ifdim\wp@extra>0pt\relax \AddToHookNext{para/begin}{\begin{wrapfigure}{r}{\wp@width} \vskip-\intextsep \rule{0pt}{\wp@extra} \vskip-\intextsep \end{wrapfigure}\vskip-\baselineskip\indent}% \fi} \xdef\wp@parshape{\the\numexpr \wp@page+\wp@lines+1}% \edef\wp@test{\the\textwidth}% \count1=\wp@page\relax \loop\ifnum\count1>0 \advance\count1 by -1 \xdef\wp@parshape{\wp@parshape\space 0pt \wp@test}% \repeat \edef\wp@test{\the\dimexpr \textwidth-\wp@width-\columnsep}% \count1=\wp@lines\relax \loop\ifnum\count1>0 \advance\count1 by -1 \xdef\wp@parshape{\wp@parshape\space 0pt \wp@test}% \repeat \xdef\wp@parshape{\wp@parshape\space 0pt \the\textwidth}% \AddToHookNext{para/before}{\parshape=\wp@parshape}% }

\def\wp@left{% handle right side wrapfig \afterpage{\xdef\wp@extra{\the\dimexpr \wp@lines\baselineskip-\ht\AP@partial-\dp\AP@partial}% \noindent\raisebox{\dimexpr \ht\strutbox-\height}[0pt][0pt]{\box\wp@image}% \vskip-\baselineskip \unvbox\AP@partial \ifdim\wp@extra>0pt\relax \AddToHookNext{para/begin}{\begin{wrapfigure}{l}{\wp@width} \vskip-\intextsep \rule{0pt}{\wp@extra} \vskip-\intextsep \end{wrapfigure}\vskip-\baselineskip\indent}% \fi} \xdef\wp@parshape{\the\numexpr \wp@page+\wp@lines+1}% \edef\wp@test{\the\textwidth}% \count1=\wp@page\relax \loop\ifnum\count1>0 \advance\count1 by -1 \xdef\wp@parshape{\wp@parshape\space 0pt \wp@test}% \repeat \edef\wp@temp{\the\dimexpr \wp@width+\columnsep}% \edef\wp@test{\the\dimexpr \textwidth-\wp@temp}% \count1=\wp@lines\relax \loop\ifnum\count1>0 \advance\count1 by -1 \xdef\wp@parshape{\wp@parshape\space \wp@temp \wp@test}% \repeat \xdef\wp@parshape{\wp@parshape\space 0pt \the\textwidth}% \AddToHookNext{para/before}{\parshape=\wp@parshape}\par }

\NewEnviron{wp@environ}{% put \BODY into \wp@image \ifdim\wp@width=0pt \savebox\wp@image{\varwidth{\textwidth} \renewcommand{\caption}[2][\empty]{\par}% dummy caption \BODY \endvarwidth}% \xdef\wp@width{\the\wd\wp@image}% \fi \global\setbox\wp@image=\hbox{\minipage{\wp@width}\BODY\endminipage} } \makeatother

\begin{document} \noindent\rule{\textwidth}{40\baselineskip}

\begin{wrappage}{r}{0pt} \rule{100pt}{5cm} \caption{test} \end{wrappage}

\lipsum[1-4]

\end{document}

John Kormylo
  • 79,712
  • 3
  • 50
  • 120
  • \ht0+2\intextsep What does the 2 mean? – mala97 Dec 22 '22 at 00:56
  • 1
    Same as $x+2a$. One above and one below. – John Kormylo Dec 22 '22 at 05:08
  • You might want to check out the new solution. – John Kormylo Dec 30 '22 at 18:45
  • Cool solution, but I have found maybe the best one: https://tex.stackexchange.com/a/515781/287023, we should also add the following (one extra line at the top) \BeforeBeginEnvironment{wrapfigure}{\setlength{\intextsep}{0pt}} – mala97 Dec 31 '22 at 13:24
  • 1
    Mine (second solution) doesn't leave a gap at the bottom of the page. But you will also need \BeforeBeginEnvironment{wrappage}{\setlength{\intextsep}{0pt}} which also uses \intextsep. – John Kormylo Dec 31 '22 at 15:36