# How to wrap a figure in exam document?

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}


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}


**

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

**

This is the problem

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
\begingroup
\@placepointstrue
\bonuspoint@block
\endgroup
}%
\else
\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
\question[#1]%
\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 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!!

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!

THANK YOU, SKILLMON

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
\begingroup
\@placepointstrue
\bonuspoint@block
\endgroup
}%
\else
\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
\question[{#2}]%
\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}


• @Skillmom the solutions it´s exactly how I liked. We need to add many code lines, but the result it´s exactly how I liked. With the tabulars/indents as \question of exam docs. Feb 23 '19 at 22:14
• Is possible another \questionInsbox with option for Left figure instead of Right figure? Feb 23 '19 at 22:31
• @MikaIke that's why we need so many code lines, because we want all those indents to be kept, and that's only possible with wobbling a bit with internals of exam and lists (and 40 lines are just a simple copy of some code of exam to use it elsewhere). See my new edit for figures on the left and right. Feb 24 '19 at 8:24
• Thank you for solution @Skillmon Now I have another problem, with the answers in a enumerate. I post it in a new EDIT 2019/02/24 in the original question. Feb 24 '19 at 11:26
• @MikaIke nesting inside itemize or enumerate is different. This solution is tailored to work inside of questions with a \question. For a solution that works in itemize instead, see the link I refer to in my answer. The situation with different types of lists is less than ideal and each type of list needs its own set of adaptations to make the insbox solution work. I originally planned to write a package which tries to tackle this, but up till now I didn't succeed in doing so (and in fact this question didn't contribute to my motivation) :) Feb 24 '19 at 11:57

Here is a solution with caption and the plain TeX insbox macro package. It defines a \InsertBoxR command, to be inserted at the very beginning of a paragraph, which takes two mandatory arguments: the number of untouched lines before a box id inserted, and the inserted box itself. In addition, in case TeX has a wrong computation of number of the necessary shorter lines, it accepts as an optional argument the number of supplementary shorter lines that you want.

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

\begin{document}

\begin{questions}
\question
Hello, I want to wrap this figure in exam. \par
\InsertBoxR {0}{\parbox{0.3\linewidth}{\includegraphics[width=\linewidth]{example-image-a.png}\captionof{figure}{This is A}}}[3]
\lipsum[2]\lipsum[1]
\end{questions}

\end{document}


• As far as I know, it is advised to use the capt-of package, if the only application of the caption package would be \captionof. Feb 23 '19 at 21:36
• You're probably right. However, I systematically load it for another reason: when you put captions above tables (as is the typographical tradition, for obvious reasons), LaTeX does not swap the values of \abovecaptionskip and \belowcaptionskip, whence captions much too close to tables, whereas caption does. Feb 23 '19 at 21:44
• I know. The above is only the advise given by the documentation of caption as well. And thanks again for sharing your insbox+itemize magic to me :) Feb 23 '19 at 21:46
• @Bernard I like the solution. It´s not complicated (little code) but the tabulars/indent are not ok in all the questions i.imgur.com/BUH1Brt.jpg Feb 23 '19 at 22:12
• I didn't try to delve into the exam code — I don't use this package, and don't have the expertise for that. Skillmon did a really fine job. Feb 23 '19 at 22:35