Re: [問題] 選項

作者: ChenMeng0518 (LSiYue)   2014-02-25 18:54:53
這個問題﹐其實在大陸這邊的論壇裡﹐不久前討論過。當時討論的內容﹐比你這個可能
還會更復雜一些。
下邊的代碼考慮﹕
1. 如果選項內容很短﹐則以
A ... B ... C ... D ...
來放置四個選項
2. 如果內容稍長﹐則以
A ... B ...
C ... D ...
來放置
3. 如果內容十分長﹐則以
A ...
B ...
C ...
D ...
來放置。
注意﹐這裡 \choice 後邊跟的數字﹐是用來標記答案的。如果該選項是正確答案﹐則
記錄 1; 如果是錯誤答案﹐則記錄 0. 當然﹐如果不想顯示答案出來﹐一律填零即可。
\documentclass{article}
\usepackage{environ}
\newlength{\choiceslen}
\newif\ifshowcorrect
\newcounter{choices}
\newcommand{\choicefinal}[1]{%
\ifnum\value{choices}>0 \hfill\fi\egroup
\hspace{0pt}%
\hbox to\choiceslen
\bgroup
\stepcounter{choices}%
\ifcase#1\relax
(\Alph{choices})%
\else
\ifshowcorrect
\expandafter\underline
\fi
{(\Alph{choices})}%
\fi\space}
\newcommand{\choicetemp}[1]{\stepcounter{choices}\space(\Alph{choices})\cr}
\NewEnviron{choices}
{\setcounter{choices}{0}%
\let\choice\choicetemp
\settowidth{\choiceslen}{\vbox{\halign{##\hfil\cr\BODY\crcr}}}
\ifdim\choiceslen>.5\textwidth
\setlength{\choiceslen}{\textwidth}%
\else
\ifdim\choiceslen>.25\textwidth
\setlength{\choiceslen}{.5\textwidth}%
\else
\setlength{\choiceslen}{.25\textwidth}%
\fi
\fi
\let\choice\choicefinal
\setcounter{choices}{0}%
\begin{flushleft}
\bgroup\BODY\hfill\egroup
\end{flushleft}}
\begin{document}
\begin{choices}
\choice0 apple
\choice1 banana
\choice0 orange
\choice0 pear
\end{choices}
\begin{choices}
\choice0 apple
\choice1 banana
\choice0 orang, eeeeeeeee
\choice0 pear
\end{choices}
\showcorrecttrue
\begin{choices}
\choice0 apple
\choice1 banana
\choice0 orange, eeeeeeeee, eeeeeeeee, eeeeeeeee
\choice0 pear
\end{choices}
\end{document}
代碼的一個問題是﹐如果選項確實很長﹐還要考慮換行的問題﹐但是這份代碼裡並沒有
去實現。如果有需要的話﹐考慮使用 pbox 來實現即可﹐還是很簡單的。所以就不多說
了。
※ 引述《s60984 (Angus)》之銘言:
: 在exam環境下,有choice可以用
: 但在article,book沒有choice可用
: 所以我就用下列方式得到選擇題
: \begin{enumerate}
: \item ABCCD
: \begin{multicols}{2}
: \begin{enumerate}
: \renewcommand{\labelenumii}{$($\Alph{enumii}$)$}
: \item ABCCDABCCDABCCD
: \item ABCCDABCCDABCCD
: \item ABCCDABCCDABCCD
: \item ABCCDABCCDABCCD
: \end{enumerate}
: \end{multicols}
: \end{enumerate}
: 顯示為
: 1. ABCCD
: (A)BCCDABCCDABCCD (C) BCCDABCCDABCCD
: (B)BCCDABCCDABCCD (D) BCCDABCCDABCCD
: 請問要如何變成
: 1. ABCCD
: (A)BCCDABCCDABCCD (B) BCCDABCCDABCCD
: (C)BCCDABCCDABCCD (D) BCCDABCCDABCCD
作者: s60984 (Angus)   2014-02-27 00:07:00
感謝~~~

Links booklink

Contact Us: admin [ a t ] ucptt.com