How to wrap a figure in exam document?
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}
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}