summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/multiple-choice/tex/multiple-choice.sty
blob: e672e5dd54192d2207f144e41b28e2acbea657b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
%%
%% This is file `multiple-choice.sty'.
%%
%% LaTeX package for multiple-choice questions
%%
%% Copyright © 2022
%% ------------------------------------------------------------------
%%  Author: Seiied-Mohammad-Javad Razavian <javadr at gmail dot com>
%%
%%  This work may be distributed and/or modified under the
%%  conditions of the Latex Project Public License, either
%%  version 1.3 of this license or (at your option) any
%%  later version.
%%  The latest version of the license is in
%%    http://www.latex-project.org/lppl.txt
%%  and version 1.3 or later is part of all distributions of
%%  LaTeX version 2003/06/01 or later.
%%
%%  This work has the LPPL maintenance status `maintained'.
%%  This work is “author-maintained” (as per LPPL maintenance status).
%%  The Current Maintainer of this work is Seiied-Mohammad-Javad Razavian.
%% ------------------------------------------------------------------
%%
\NeedsTeXFormat{LaTeX2e}
\def\choicesdate{2022/06/24}
\def\choicesversion{0.2}
\ProvidesPackage{multiple-choice}[\choicesdate\space v\choicesversion^^JMultiple-choice question with automatic adjustment of the choices based on their widths]
\RequirePackage{biditools}
\newdimen\mc@ChoiceLabelWidth
\newdimen\mc@ChoiceLabelSep
\mc@ChoiceLabelWidth=1.5em
\mc@ChoiceLabelSep=0.5em
\newcounter{mc@MultChoiceCount}
\newcommand{\mc@InitialChoice}{%
    \ifnum\value{mc@MultChoiceCount}>\z@
        \cr
    \fi
    \stepcounter{mc@MultChoiceCount}%
    \hbox to \mc@ChoiceLabelWidth{\hss(\Alph{mc@MultChoiceCount})}%
    \kern\mc@ChoiceLabelSep
}%
\newcommand{\mc@FinalChoice}{%
    \ifnum\value{mc@MultChoiceCount}>\z@
        \egroup
        \egroup
    \fi
    \stepcounter{mc@MultChoiceCount}%
    \hspace{0pt plus 1pt}%
    \hbox\bgroup
    \hbox to \mc@ChoiceLabelWidth{\hss(\Alph{mc@MultChoiceCount})}%
    \kern\mc@ChoiceLabelSep
        \vtop\bgroup
        \hsize\mc@MultChoicesWidth
        \advance\hsize-\mc@ChoiceLabelWidth
        \advance\hsize-\mc@ChoiceLabelSep
}%
\newbox\mc@MultChoicesBox
\newdimen\mc@MultChoicesWidth
\newcommand{\mc@ComputeMultChoicesWidth}[1]{%
    \setbox\mc@MultChoicesBox\hbox{%
        \vbox{%
        \halign{%
            ##\hfil\cr
            #1\crcr
        }%
        }%
    }%
}%
% MultipltChoices Environment.
\newenvironment{choices}{%
    \setcounter{mc@MultChoiceCount}{0}%
    \let\choice\mc@InitialChoice
    \bidi@collect@long@body\mc@ComputeMultChoicesWidth
}{%
    \mc@MultChoicesWidth-\fontcharwd\font`
    \advance\mc@MultChoicesWidth\wd\mc@MultChoicesBox
    \ifdim\mc@MultChoicesWidth>0.5\linewidth
        \mc@MultChoicesWidth=\linewidth
    \else
        \ifdim\mc@MultChoicesWidth>0.25\linewidth
        \mc@MultChoicesWidth=0.5\linewidth
        \else
        \mc@MultChoicesWidth=0.25\linewidth
        \fi
    \fi
    \setcounter{mc@MultChoiceCount}{0}%
    \let\choice\mc@FinalChoice
    \parindent\z@
    \ifvmode
    \else
        \par
    \fi
    \the\@bidi@envbody
    \egroup
    \egroup
}%
%%
%%
%% End of file `multiple-choice.sty'.