我有代码自动对齐多项选择题的四个选项,并将答案用虚线放在问题后面。它与文档类配合得很好exam
(见下面的输出截图)。
最小示例代码如下:
\documentclass[answers,a4paper]{exam}
\usepackage{graphicx}
\usepackage{environ}
\usepackage[normalem]{ulem}
\usepackage{etoolbox}
\usepackage{enumitem}
%
\renewcommand{\choiceshook}{%
\setlength{\leftmargin}{10pt}%
}
\newlength\answerspace
\setlength\answerspace{0.5in}
\newcommand\dottedanswerline[1][{}]{%
% One optional argument, the default value of which is empty.
\unskip\linebreak[0]\enspace
\hbox{}\nobreak\dotfill
\ifprintanswers
\hbox to \answerspace{(\hfil#1\hfil)}%
\else
\hbox{(\hskip 0.5in)}%
\fi
}% dottedanswerline
\makeatletter
\newlength\choiceitemwidth
\newif\ifshowsolution \showsolutiontrue
\newcounter{choiceitem}%
\def\thechoiceitem{\Alph{choiceitem}}%
\setlength{\fboxsep}{0pt}
%\def\makechoicelabel#1{#1\uline{\thechoiceitem.}\else\thechoiceitem.\fi\space} %underline the answer item label if we want to print the answer
\def\makechoicelabel#1{#1\fbox{\thechoiceitem.}\else\thechoiceitem.\fi\space} %underline the answer item label if we want to print the answer
\def\choice@mesureitem#1{\cr\stepcounter{choiceitem}\makechoicelabel#1}%
%measure the choices, this is the first time we need to parse the \BODY
\def\choicemesureitem{\@ifstar
{\choice@mesureitem\ifprintanswers \xappto\theanswer{\thechoiceitem}\ignorespaces}%
{\choice@mesureitem\iffalse}}%
\def\choice@blockitem#1{%
\ifnum\value{choiceitem}>0\hfill\fi
\egroup\hskip0pt
\hbox to \choiceitemwidth\bgroup\hss\refstepcounter{choiceitem}\makechoicelabel#1}
\def\choiceblockitem{\@ifstar
{\choice@blockitem\ifprintanswers\ignorespaces}%
{\choice@blockitem\iffalse}}
\def\choice@paraitem#1{%
\par\refstepcounter{choiceitem}\makechoicelabel#1}
\def\choiceparaitem{\@ifstar
{\choice@paraitem\ifprintanswers\ignorespaces}%
{\choice@paraitem\iffalse}}
\NewEnviron{items}{%
\def\theanswer{}
\begingroup
\let\item\choicemesureitem
\setcounter{choiceitem}{0}%
\settowidth{\global\choiceitemwidth}{\vbox{\halign{##\hfil\cr\BODY\crcr}}}%
\endgroup \dottedanswerline[\theanswer]
\setlist[trivlist]{nosep}
\trivlist\item\relax%
\parindent0pt%
\setcounter{choiceitem}{0}%
\ifdim\choiceitemwidth<0.25\columnwidth
\choiceitemwidth=0.25\columnwidth
\let\item\choiceblockitem
\bgroup\BODY\hfill\egroup
\else\ifdim\choiceitemwidth<0.5\columnwidth
\choiceitemwidth=0.5\columnwidth
\let\item\choiceblockitem
\bgroup\BODY\hfill\egroup
\else % \choiceitemwidth > 0.5\columnwidth
\let\item\choiceparaitem
\BODY
\fi\fi
\endtrivlist
}
\makeatother
\begin{document}
\section{multiply choice questions}
\begin{questions}
\question this is a question
\begin{items}
\item wrong choice
\item wrong choice
\item wrong choice
\item * right choice
\end{items}
\question this is a question
\begin{items}
\includegraphics[width=5cm,height=5cm]{example-image}\\
\item wrong choice
\item wrong choice
\item wrong choice
\item * right choice
\end{items}
\question this is a question
\begin{items}
\includegraphics[width=5cm,height=5cm]{example-image}\\
\item wrong choice
\item wrong choice wrong choice wrong choice wrong choice
\item * right choice
\item wrong choice
\end{items}
\end{questions}
\end{document}
你看我在问题中放了一个图形。但是,我想节省空间,这意味着我想把图形放在选项的右侧(如红色箭头所示)。这该怎么做?
请注意,这四个选项的排版方式紧凑,这意味着它们可以放在一行、两行或四行中,具体取决于它们的最大长度。详细信息可在此处的已解决问题中找到:自动对齐多项选择题的四个选项,并将答案用虚线放在问题后面。
编辑:在我的 TeX 代码中,图像宽度用于计算选择的最大长度,因此,如果我使用以下代码:
\question this is a question
\begin{items}
\includegraphics[width=15cm,height=5cm]{example-image}\\
\item wrong choice
\item wrong choice
\item wrong choice
\item * right choice
\end{items}
注意图像的宽度为 15 厘米,现在结果选项排版在一列,但预计在两列,请参阅屏幕截图的第二个问题。
EDIT2:如果我有这样的代码,我对 nickie 的解决方案有疑问:
\question this is a question
\begin{items}[{\includegraphics[width=15cm,height=2cm]{example-image}}]
\item wrong choice
\item wrong choice
\item wrong choice
\item * right choice
\end{items}
我将在这里获取图像:
因此,在这种情况下(我猜测图像大于 0.75\columnwidth),我们应该将所有四个选项放在图形下方。
答案1
这就是我想到的。我改变了你的items
环境,现在它接受一个可选参数:无论什么都必须与答案右对齐。
\usepackage[export]{adjustbox}
\newdimen\qanswd
\newdimen\qanswdtmp
\newbox\qimgbox
\NewEnviron{items}[1][]{%
\def\theanswer{}
\begingroup
\let\item\choicemesureitem
\setcounter{choiceitem}{0}%
\settowidth{\global\choiceitemwidth}{\vbox{\halign{##\hfil\cr\BODY\crcr}}}%
\endgroup \dottedanswerline[\theanswer]
\setbox\qimgbox\hbox{#1}%
\setlist[trivlist]{nosep}
\trivlist\item\relax%
\qanswd=\linewidth%
\advance\qanswd-\wd\qimgbox%
% handle large images (leaving less than 30% space)
\qanswdtmp=0.3\linewidth%
\ifnum\qanswd<\qanswdtmp%
%\strut\hfill% uncomment to right-align large images
\unhbox\qimgbox%
%\hfill\strut% uncomment this too to center them
\par%
\qanswd=\linewidth%
\setbox\qimgbox\hbox{}%
\fi%
% end of handling for large images
\begin{minipage}[t]{\qanswd}
\trivlist\item\relax%
\parindent0pt%
\setcounter{choiceitem}{0}%
\ifdim\choiceitemwidth<0.25\columnwidth
\choiceitemwidth=0.25\columnwidth
\let\item\choiceblockitem
\bgroup\BODY\hfill\egroup
\else\ifdim\choiceitemwidth<0.5\columnwidth
\choiceitemwidth=0.5\columnwidth
\let\item\choiceblockitem
\bgroup\BODY\hfill\egroup
\else % \choiceitemwidth > 0.5\columnwidth
\let\item\choiceparaitem
\BODY
\fi\fi
\endtrivlist
\end{minipage}%
\adjustbox{valign=t}{\unhbox\qimgbox}
\endtrivlist
}
你必须像这样使用它:
\question this is a question
\begin{items}[{\includegraphics[width=5cm,height=5cm]{example-image}}]
\item wrong choice
\item wrong choice
\item wrong choice
\item * right choice
\end{items}
请注意 周围的额外括号\includegraphics
,它们保护着嵌套的括号。
我得到的是欠满\hbox
,但它从一开始就在那里。