0

For the following, why does \setlength{\intextsep}{#5} inside \NewDocumentEnvironment{mywrapfigure} not have any effect on the output, which makes me have to use \setlength{\intextsep}{0pt} locally before every use of wrapfigure? For example, image-b doesn't get affected by \setlength{\intextsep}{#5}.

Additionally, why is the space below image-a narrower than that below image-c?

enter image description here

\documentclass{exam}
\usepackage{graphbox,wrapfig,duckuments}

\usepackage{xparse}

% https://tex.stackexchange.com/a/266431/2288 \NewDocumentEnvironment{mywrapfigure}{O{}mO{\wrapoverhang}mO{0pt}}{% \setlength{\intextsep}{#5}% <<< has no effect \wrapfigure[#1]{#2}[#3]{#4}% }{% \endwrapfigure% }

\begin{document} {% https://tex.stackexchange.com/a/365764/2288 \setlength\intextsep{0pt} \begin{mywrapfigure}{r}{0.3\linewidth} \includegraphics[width=\linewidth]{example-image-a} \end{mywrapfigure} \blindduck[1-2] } \bigskip \begin{questions} \begin{minipage}[t][][t]{\linewidth} \question {% https://tex.stackexchange.com/a/365764/2288 %\setlength\intextsep{0pt} \begin{mywrapfigure}{r}{0.3\linewidth}[0pt] \includegraphics[width=\linewidth]{example-image-b} \end{mywrapfigure} \blindduck[1-2] } \end{minipage} \bigskip \begin{parts} \begin{minipage}[t]{\linewidth} \part[4] {% https://tex.stackexchange.com/a/365764/2288 \setlength\intextsep{0pt} \begin{mywrapfigure}{r}{0.3\linewidth}[0pt] \includegraphics[width=\linewidth]{example-image-c} \end{mywrapfigure} \blindduck[1-2] } \end{minipage} \end{parts} \end{questions} \end{document}


Update 1

Compiling the following with lualatex

\documentclass{exam}
\usepackage{graphbox,wrapfig,duckuments,blindtext}

\usepackage{xparse}

\NewDocumentEnvironment{mywrapfigure}{O{}mO{\wrapoverhang}mO{1pt}}{% \setlength{\intextsep}{#5}% <<< has no effect \wrapfigure[#1]{#2}[#3]{#4}% }{% \endwrapfigure% }

\begin{document} \begin{mywrapfigure}{r}{0.3\linewidth} \includegraphics[width=\linewidth]{example-image-a} \end{mywrapfigure} \blindduck[1] \bigskip \begin{questions} \begin{minipage}[t]{\dimexpr \linewidth} \question%\the\everypar \begin{mywrapfigure}{r}{0.3\linewidth} \vskip-\baselineskip \includegraphics[width=\linewidth]{example-image-b} \end{mywrapfigure} \blindtext[1] \end{minipage} \bigskip%\hrule \begin{parts} \begin{minipage}[t]{\linewidth} \part[4]%\the\everypar \begin{mywrapfigure}{r}{0.3\linewidth} \vskip-\baselineskip \includegraphics[width=\linewidth]{example-image-c} \end{mywrapfigure} \blindduck[1] \end{minipage} \end{parts} \end{questions} \end{document}

I get this

enter image description here

Diaa
  • 9,599
  • 1
    you are placing the wrapfigure in a group that ends before the paragraph that has to wrap starts. I'm surprised it works at all in that context. – David Carlisle Oct 06 '20 at 21:48
  • 1
    aren't the spaces in a and c the same, just that the line in C is short and does not reach under the image – David Carlisle Oct 06 '20 at 21:52
  • @DavidCarlisle You are right about my second question, but what about the first one? I need to make wrapfigure work inside the listing of questions/parts? – Diaa Oct 06 '20 at 22:08
  • @DavidCarlisle I understand. I would be grateful if you could come up with a more proper solution than my short-circuits :) – Diaa Oct 06 '20 at 22:12
  • oh that's why it seems to work, sorry just noticed that you lost one group level by using \wrapfigure rather than \begin{wrapfigure}` hmm Ok so you have a chance to make that work, but too late tonight to think about wrapfig:-) – David Carlisle Oct 06 '20 at 22:17

1 Answers1

1

Every time you put wrapfigure immediately after \item (which is used by \question and \part), it gets pushed down one line. (Interestingly \rlap{...} gets pushed up one line.) The only way to fix this is to manually add \vskip-\baselineskip inside wrapfigure. It has nothing to do with \intextsep.

\documentclass{exam}
\usepackage{graphbox,wrapfig,duckuments}

\usepackage{xparse}

\NewDocumentEnvironment{mywrapfigure}{O{}mO{\wrapoverhang}mO{1pt}}{% \setlength{\intextsep}{#5}% <<< has no effect \wrapfigure[#1]{#2}[#3]{#4}% }{% \endwrapfigure% }

\begin{document} \begin{mywrapfigure}{r}{0.3\linewidth} \includegraphics[width=\linewidth]{example-image-a} \end{mywrapfigure} \blindduck[1-2] \bigskip \begin{questions} \begin{minipage}[t]{\dimexpr \linewidth} \question%\the\everypar \begin{mywrapfigure}{r}{0.3\linewidth} \vskip-\baselineskip \includegraphics[width=\linewidth]{example-image-b} \end{mywrapfigure} \blindduck[1-2] \end{minipage} \bigskip%\hrule \begin{parts} \begin{minipage}[t]{\linewidth} \part[4]%\the\everypar \begin{mywrapfigure}{r}{0.3\linewidth} \vskip-\baselineskip \includegraphics[width=\linewidth]{example-image-c} \end{mywrapfigure} \blindduck[1-2] \end{minipage} \end{parts} \end{questions} \end{document}


Update 1

You should always use \WFclear if you run out of text before wrapfigure is done. Note the use of the optional argument [9] for the second image. Also note that \linewidth keeps getting smaller, making the images smaller as well.

\documentclass{exam}
\usepackage{graphbox,wrapfig,duckuments,blindtext}

\usepackage{xparse}

\NewDocumentEnvironment{mywrapfigure}{O{}mO{\wrapoverhang}mO{1pt}}{% \setlength{\intextsep}{#5}% <<< has no effect \wrapfigure[#1]{#2}[#3]{#4}% }{% \endwrapfigure% }

\begin{document} \begin{mywrapfigure}{r}{0.3\linewidth} \includegraphics[width=\linewidth]{example-image-a} \end{mywrapfigure} There once was a very smart but sadly blind duck. When it was still a small duckling it was renowned for its good vision. But sadly as the duck grew older it caught a sickness which caused its eyesight to worsen. It became so bad, that the duck couldn't read the notes it once took containing much of inline math. Only displayed equations remained legible. That annoyed the smart duck, as it wasn't able to do its research any longer. It called for its underduckling and said: Go, find me the best eye ducktor there is. He shall heal me from my disease!" %\blindduck[1]\par \bigskip \WFclear \begin{questions} \begin{minipage}[t]{\dimexpr \linewidth} \question \begin{mywrapfigure}[9]{r}{0.3\linewidth} \vskip-\baselineskip \includegraphics[width=\linewidth]{example-image-b} \end{mywrapfigure} Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam lobortis facilisis sem. Nullam nec mi et neque pharetra sollicitudin. Praesent imperdiet mi nec ante. Donec ullamcorper, felis non sodales commodo, lectus velit ultrices augue, a dignissim nibh lectus placerat pede. Vivamus nunc nunc, molestie ut, ultricies vel, semper in, velit. Ut porttitor. Praesent in sapien. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Duis fringilla tristique neque. Sed interdum libero ut metus. Pellentesque placerat. Nam rutrum augue a leo. Morbi sed elit sit amet ante lobortis sollicitudin. Praesent blandit blandit mauris. Praesent lectus tellus, aliquet aliquam, luctus a, egestas a, turpis. Mauris lacinia lorem sit amet ipsum. Nunc quis urna dictum turpis accumsan %\blindtext[1] \end{minipage} \bigskip%\hrule \begin{parts} \begin{minipage}[t]{\linewidth} \part[4]%\the\everypar \begin{mywrapfigure}{r}{0.3\linewidth} \vskip-\baselineskip \includegraphics[width=\linewidth]{example-image-c} \end{mywrapfigure} There once was a very smart but sadly blind duck. When it was still a small duckling it was renowned for its good vision. But sadly as the duck grew older it caught a sickness which caused its eyesight to worsen. It became so bad, that the duck couldn't read the notes it once took containing much of inline math. Only displayed equations remained legible. That annoyed the smart duck, as it wasn't able to do its research any longer. It called for its underduckling and said: Go, find me the best eye ducktor there is. He shall heal me from my disease!" \WFclear \end{minipage} \end{parts} \end{questions} \end{document}

John Kormylo
  • 79,712
  • 3
  • 50
  • 120
  • Please, consider this document https://pastebin.com/zQDanFj6. How to fix its output shown here https://i.ibb.co/gF3Dg4V/image.png? The text overwrites image-b bottom, while it doesn't wrap around image-a and image-c. – Diaa Oct 07 '20 at 06:46
  • I updated my tex distribution but I get the same weird output. Please, if you don't mind, take a look at my log file here https://pastebin.com/riZF5zF9. – Diaa Oct 07 '20 at 17:12
  • Even pdflatex has a similar distorted output for me :/ – Diaa Oct 07 '20 at 18:31
  • please, find my question Update #1 – Diaa Oct 08 '20 at 14:06
  • 1
    Ah, you changed \blindduck[1-2] to \blindduck[1]. If you just use text instead of such packages, the problems usually go away. – John Kormylo Oct 08 '20 at 15:03
  • Many thanks for pointing out the use of \WFclear and not depending on those dummy text packages. – Diaa Oct 08 '20 at 15:45