Exam Document – How to Wrap a Figure in an Exam Document?

examwrapfigure

I can wrap in ARTICLE, but not in EXAM. How to wrap in EXAM?
(Question Plus-if you´re a LaTeX master-.- What´s the mosr suitable and easy package to do this kind of things?)

I can wrap a figure in an ARTICLE document

with this code

\documentclass[12pt]{article}
\usepackage{graphicx,wrapfig,lipsum}
\usepackage[onehalfspacing]{setspace}
\usepackage{mwe}

\begin{document}
%\begin{questions}
    \begin{wrapfigure}{r}{.3\linewidth}
    \centering
    \includegraphics[width=\linewidth]{example-image-a.png}
    \caption{This is A}
\end{wrapfigure}
Hello, I want to wrap this figure in exam
\lipsum[2]\lipsum[1]
%\end{questions}
\end{document}

enter image description here

BUT I´d like wrap images in EXAM documents, and this code doesn´t give the wrapped figure:

\documentclass[12pt]{exam}
\usepackage{graphicx,wrapfig,lipsum}
\usepackage[onehalfspacing]{setspace}
\usepackage{mwe}

\begin{document}
\begin{questions}
\begin{wrapfigure}{r}{.3\linewidth}
    \centering
    \includegraphics[width=\linewidth]{example-image-a.png}
    \caption{This is A}
\end{wrapfigure}
\question Hello, I want to wrap this figure in exam
\lipsum[2]\lipsum[1]

\end{questions}
\end{document}

enter image description here

**

EDITED (2019/02/24) Afther trying Skillmon solution, I have a new problem with lists(enumerate) in the questions

**

This is the problem
enter image description here

With the code (only adding itemize in thq euestion with the figure)

        \documentclass[12pt]{exam}
        \usepackage{graphicx,wrapfig,lipsum}
        \input{insbox}
        \usepackage{capt-of}
        \usepackage[onehalfspacing]{setspace}
        \usepackage{mwe}

        \usepackage{etoolbox}

        \makeatletter
        %% Stealing some code from exam:
        \def\mypointshandling
          {%>>>
            \if@bonus
              \def\padded@point@block{%
            \begingroup
              \@placepointstrue
              \bonuspoint@block
            \endgroup
              }%
            \else
              \def\padded@point@block{%
            \begingroup
              \@placepointstrue
              \point@block
            \endgroup
              }%
            \fi
            \if@pointsdropped
            \else
              \if@bonus
            \if@bonusqformat
              \ifx\ques@ref\@queslevel
              \else
                \setup@point@toks
              \fi
            \else
              \setup@point@toks
            \fi
              \else
            \if@qformat
              \ifx\ques@ref\@queslevel
              \else
                \setup@point@toks
              \fi
            \else
              \setup@point@toks
            \fi
              \fi
            \fi
            \global \MyIfPointsfalse
          }%<<<
        \newif\ifMyIfPoints
        \protected\def\questionInsbox
          {%
            \@ifnextchar[\questionInsbox@a{\questionInsbox@a[]}%
          }
        \protected\long\def\questionInsbox@a[#1]#2#3%
          {%
            \@ifnextchar[{\questionInsbox@b{#1}{#2}{#3}}
              {\questionInsbox@b{#1}{#2}{#3}[0]}
          }
        \long\def\questionInsbox@b#1#2#3[#4]%
          {%
            \apptocmd\@itemlabel{\hskip\leftmargin}{}{}%
            \def\makelabel##1{\hss\llap{##1}}%
            \if\relax\detokenize{#1}\relax
              \question
            \else
              \global\MyIfPointstrue
              \patchcmd\@readpoints{\global\@placepointstrue}{\relax}{}{}%
              \question[#1]%
              \patchcmd\@readpoints{\relax}{\global\@placepointstrue}{}{}%
            \fi
            \patchcmd\@itemlabel{\hskip\leftmargin}{}{}{}%
            \let\makelabel\@mklab
            \mbox{}%
            \vspace*{-\baselineskip}%
            \setlength{\leftskip}{\leftmargin}%
            \InsertBoxR{#2}{\hskip-\leftmargin#3\hskip\leftmargin}[#4]%
            \ifMyIfPoints
              \mypointshandling
            \fi
            \the\point@toks
            \ignorespaces
          }
        \makeatother

        \begin{document}
        \begin{questions}
          \question Jei you
          \questionInsbox[5]{0}
            {%
              \parbox[t]{.3\linewidth}
            {%
              \centering
              \includegraphics[width=\linewidth]{example-image-a.png}
              \captionof{figure}{This is A}%
            }%
            }[1]
        Hello, I want to wrap this figure in exam, and you should choose the rigth answer:
        \begin{enumerate}
          \item \lipsum[1]  
          \item \lipsum[1]
        \end{enumerate}

        \question Another
        \end{questions}
        \end{document} 

The solution, not the best but… on the fly I have typed is:

\question See figure and answer, please:    
\begin{enumerate}
\begin{multicols}{2}  
\item answer 1
\item ans 2
\item ans 3
{\centering
    \begingroup
                \centering
                \includegraphics[width=1.00\linewidth]{{example-image-a.png}
                \vspace{-0.3cm}
                \captionof{figure}{}
                \label{fig:croquis-rectangulo-ecuacions.png}
            \endgroup}
  \end{multicols}
  \item ans afeter fig
  \end{enumerate}

EDIT (2019/02/25) after EDIT 3 of Skillmon
I admire yout knowledge of LaTeX programming. It´s fantastic!!

The solution if perfect for normal text but when you use lists (enumerate, itemize,…) the problem is remaining.
After reading the last comments (2019/02/24) of Skillmon, I decide to simulate the lists in normal text. With
\
a) answer 1 answer 1 answer 1 answer 1 answer 1 answer 1 answer 1 answer 1 answer 1 answer 1 answer 1 answer 1\
b) answer 2 answer 2 answer 2 answer 2 answer 2 answer 2 answer 2 answer 2 answer 2 answer 2 answer 2 answer 2\

LaTeX is so beatifull, so complicated!!

Waiting for this option (I´m optimistic, yes!! 🙂 ) in exam class in the future… I´ll use your solutions sometimes.

Only fails in the bottom of page, as far as I can see, and when you use some \questionInsboxL[5]{2} with close to blank page between questions.

THANK YOU FOR ALL YOUR SOLUTIONS AND EFFORTS!

enter image description here

THANK YOU, SKILLMON

Best Answer

wrapfig doesn't work in lists, and questions is just another version of a list. The following is a quick and dirty reimplementation of https://tex.stackexchange.com/a/453624/117050 to work with enumerate like lists instead of with itemize. It uses insbox just like @Bernard's answer, but shouldn't require a manual paragraph. It does only implement the code to wrap inside one question, not inside the next one, too.

EDIT: It now should support exam's points system, but I didn't test it thoroughly. You might come back and complain if it doesn't work out.

EDIT2: Streamlined the interface to be more like the one of \InsertBoxR, the new macro's name is \questionInsboxR and it takes an optional argument, two mandatory ones, and a second optional one. The first optional argument is like the one of \question, the two mandatory arguments and the following optional one are like those of \InsertBoxR.

EDIT3: Added \questionInsboxL, fixed behaviour of multiple \questionInsboxL/R usages inside one questions environment.

\documentclass[12pt]{exam}
\usepackage{graphicx,wrapfig,lipsum}
\input{insbox}
\usepackage{capt-of}
\usepackage[onehalfspacing]{setspace}
\usepackage{mwe}

\usepackage{etoolbox}

\makeatletter
%% Stealing some code from exam:
\def\mypointshandling
  {%>>>
    \if@bonus
      \def\padded@point@block{%
        \begingroup
          \@placepointstrue
          \bonuspoint@block
        \endgroup
      }%
    \else
      \def\padded@point@block{%
        \begingroup
          \@placepointstrue
          \point@block
        \endgroup
      }%
    \fi
    \if@pointsdropped
    \else
      \if@bonus
        \if@bonusqformat
          \ifx\ques@ref\@queslevel
          \else
            \setup@point@toks
          \fi
        \else
          \setup@point@toks
        \fi
      \else
        \if@qformat
          \ifx\ques@ref\@queslevel
          \else
            \setup@point@toks
          \fi
        \else
          \setup@point@toks
        \fi
      \fi
    \fi
    \global \MyIfPointsfalse
  }%<<<
\protected\long\def\myOargparse#1#2%>>>
  {%
    \@ifnextchar[{\myOargparse@{#2}}{#2{#1}}%
  }%<<<
\long\def\myOargparse@#1[#2]%>>>
  {%
    #1{#2}%
  }%<<<
\def\q@mark{\q@mark}
\newif\ifMyIfPoints
\newif\if@notfirstins
\protected\def\questionInsboxL%>>>
  {%
    \myOargparse{\q@mark}{\questionInsbox@a\InsertBoxL}%
  }%<<<
\protected\def\questionInsboxR%>>>
  {%
    \myOargparse{\q@mark}{\questionInsbox@a\InsertBoxR}%
  }%<<<
\protected\long\def\questionInsbox@a#1#2#3#4%>>>
  {%
    \myOargparse{0}{\questionInsbox@b{#1}{#2}{#3}{#4}}%
  }%<<<
\protected\long\def\questionInsbox@b#1#2#3#4#5%>>>
  {%
    \if@notfirstins
    \else
      \def\makelabel##1{\hss\llap{##1}}%
      \apptocmd\@itemlabel{\hskip\leftmargin}{}{}%
    \fi
    \ifx\q@mark#2%
      \question
    \else
      \global\MyIfPointstrue
      \patchcmd\@readpoints{\global\@placepointstrue}{\relax}{}{}%
      \question[{#2}]%
      \patchcmd\@readpoints{\relax}{\global\@placepointstrue}{}{}%
    \fi
    \if@notfirstins
    \else
      \patchcmd\@itemlabel{\hskip\leftmargin}{}{}{}%
      \let\makelabel\@mklab
      \@notfirstinstrue
    \fi
    \mbox{}%
    \vspace*{-\baselineskip}%
    \setlength{\leftskip}{\leftmargin}%
    \ifx\InsertBoxR#1%
      #1{#3}{\hskip-\leftmargin#4\hskip\leftmargin}[{#5}]%
    \else
      #1{\numexpr#3+1\relax}{#4}[{#5}]\par\hspace{\itemindent}%
    \fi
    \ifMyIfPoints
      \mypointshandling
    \fi
    \the\point@toks
    \ignorespaces
  }%<<<
\makeatother

\begin{document}
\begin{questions}
  \question[5]\lipsum[2]
  \questionInsboxR[5]{0}
    {%
      \parbox[t]{.3\linewidth}
        {%
          \centering
          \includegraphics[width=\linewidth]{example-image-a.png}%
          \captionof{figure}{This is A}%
        }%
    }[1]
    Hello, I want to wrap this figure in exam
    \lipsum[1]
  \questionInsboxL[5]{2}
    {%
      \parbox[t]{.3\linewidth}
        {%
          \centering
          \includegraphics[width=\linewidth]{example-image-a.png}%
          \captionof{figure}{This is A}%
        }%
    }[1]
    Hello, I want to wrap this figure in exam
    \lipsum[1]
\end{questions}
\end{document}

enter image description here

Related Question