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?
\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


\wrapfigurerather 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