How can I automate multiple choice question type in my own class in LaTeX?
-
1Welcome to TeX.sx! – hpesoj626 Oct 22 '12 at 04:55
-
@Kayla: I think it would be better if you posted your version as a new answer instead of editing the answer I posted a while ago. – Peter Grill May 10 '13 at 21:13
1 Answers
On way would be to define \hcoices (or \vchoices) using a \foreach form the pgffor package.
1. Using exam class:
The first part below if using the choices environment, the next two are the custom ones that you would use:

Notes:
- You had a minor typo in your code which I have corrected below.
\end partsshould have been be\end{parts}. - You should also be using the
choicesinstead ofpartsenvironment for multiple choice options.
Code:
\documentclass{exam}
\usepackage{pgffor}
\newcommand{\hchoices}[1]{%
\par
\begin{oneparchoices}
\foreach \Choice in {#1} {%
\choice \Choice
}%
\end{oneparchoices}
}%
\newcommand{\vchoices}[1]{%
\begin{choices}
\foreach \Choice in {#1} {%
\choice \Choice
}%
\end{choices}
}%
\begin{document}
\noindent\textbf{Choices:}
\begin{questions}
\question Which among the following focuses on values?
\begin{choices}
\choice Axiology
\choice Epistomology
\choice Philosophy
\end{choices}
\end{questions}
\hrule\medskip\noindent\textbf{hchoices:}
\begin{questions}
\question Which among the following focuses on values?
\hchoices{Axiology, Epistomology, Philosophy}
\end{questions}
\hrule\medskip\noindent\textbf{vchoices:}
\begin{questions}
\question Which among the following focuses on values?
\vchoices{Axiology, Epistomology, Philosophy}
\end{questions}
\end{document}
2. Without exam class:
To produce something similar without the exam class, you can use enumerated lists. I used the enumitem package below as it is more flexible:

Notes:
- Thanks to Heiko Oberdiek for the solution to How to create an inline list via a macro which was needed here.
I have used
\hspace*{2.0em}to separate the individual horizontal items, but if you prefer that the items be more uniformly distributed based on the number of items, one can use usehfillfor theitemjoinoption:\setlist*[MyHChoices]{label=\Alph*., itemjoin={\hfill}}
Code:
\documentclass{article}
\usepackage[inline]{enumitem}
\usepackage{pgffor}
\newlist{MyHChoices}{enumerate*}{2}
\newlist{MyVChoices}{enumerate}{2}
\setlist*[MyHChoices]{label=\Alph*., itemjoin={\hspace*{2.0em}}}
\setlist*[MyVChoices]{label=\Alph*.}
% https://tex.stackexchange.com/questions/78628/how-to-create-an-inline-list-via-a-macro
\newtoks\gInlineToks
\newcommand*{\hchoices}[1]{%
\global\gInlineToks{}%
\foreach \Choice in {#1} {%
\global\gInlineToks\expandafter{%
\the\expandafter\gInlineToks
\expandafter\item\Choice
}%
}%
\par%
\begin{MyHChoices}\the\gInlineToks\end{MyHChoices}%
}%
\newcommand{\vchoices}[1]{%
\begin{MyVChoices}
\foreach \Choice in {#1} {%
\item \Choice
}%
\end{MyVChoices}
}%
\newenvironment{questions}{
\let\question\item
\begin{enumerate}[label=\arabic*.]
}{%
\end{enumerate}
}
\begin{document}
\noindent\textbf{hchoices:}
\begin{questions}
\question Which among the following focuses on values?
\hchoices{Axiology, Epistomology, Philosophy}
\end{questions}
\noindent\textbf{vchoices:}
\begin{questions}
\question Which among the following focuses on values?
\vchoices{Axiology, Epistomology, Philosophy}
\end{questions}
\end{document}
- 223,288
-
@Peter Grill.. thank u, but thats not the way i will make it. i am working on my own document class (\ProvidesClass{questiontypes}) and not on a tex file. so dont mind of packages.. My problem would be creating the command for a multiple choice question (\hchoice) inside my class. Wherein if users will use my class he/she should only use the command i have created when making a multiple choice question. Thus, they will no longer indicate commands such as {\part} within {\begin{parts}} and end with {\end{parts}}.. sorry for the typo :) – Kayla Oct 22 '12 at 05:42
-
-
@Kayla: Will update solution such that you do not use
examclass (I think I understand, but not sure). – Peter Grill Oct 22 '12 at 05:52 -
@Kayla you can put the definitions used here into your class file. The minimal working example (MWE) are needed to show how the commands really work in an actual tex file. – hpesoj626 Oct 22 '12 at 06:32
-
-
@hpesoj626: While that is normally the case, in this case it needed a different solution as the original relied on things form the
examclass. – Peter Grill Oct 22 '12 at 06:48 -
@PeterGrill True. But it can be done with your second answer. I was already anticipating it :) nice work here. I can use this, too. High school teachers love giving multiple choice tests. Can the choices be made to be aligned as if they are on a table with columns of equal width? Say for instance, if there are 3 choices then each choice shall occupy 1/3 of
linewidthand if there are 4 choices each choice shall occupy 1/4 oflinewidth. – hpesoj626 Oct 22 '12 at 06:55 -
@hpesoj626: Like this one sir?:
##Code \ProvidesClass{MyTemplate}[2012/09/03 version 0.01 My exam class] \AtBeginDocument{ \begin{center} \sffamily {\Large\textbf{schoolname}}{\large\textbf{College}}{\large Departmentname}\end{center} \noindent Name: \makebox[3in]{\hrulefill} \hfill } \newcommand{\hchoices}[1]{% \par \begin{oneparchoices} \foreach \Choice in {#1} {% \choice \Choice }% \end{oneparchoices} }% \newcommand{\vchoices}[1]{% \begin{choices} \foreach \Choice in {#1} {% \choice \Choice }% \end{choices} }% \endinput
– Kayla Oct 22 '12 at 06:59 -
@hpesoj626: thats right sir.. but how should be created and should it placed in the class i am making? – Kayla Oct 22 '12 at 07:02
-
@Kayla: You class will need the packages used here, and should be able to put the macros here in your own custom
.styfile. – Peter Grill Oct 22 '12 at 07:04 -
Sir! i cant attach here the image i made.. :) that could be helpful in understanding my point. :) – Kayla Oct 22 '12 at 07:05
-
-
@Kayla: I think you can load the image using the picture icon, but since you are a new user, you will need to remove the
!, and then a higher rep user can attach the image. – Peter Grill Oct 22 '12 at 07:07 -
@PeterGrill: sir, is there really a need for me to make a custom .sty file? Coz i dont know how will i make one, i still have more question types to do. Can i just directly require the package in myclass? – Kayla Oct 22 '12 at 07:11
-
@hpesoj626: Added a note to show how be able to distribute the multiple choice options. Let me know if that works well enough for you. – Peter Grill Oct 22 '12 at 07:12
-
@Kayla: I don't think you need to have a separate
.styfile. You should just be able to add them to the class file. I have not personally built class files, but if you have difficulties please free to post a separate question as that is more about.clsfiles instead of horizontal multiple choice options. This will help to keep the questions more organized. Feel free to link back to this question to show where you got the code that you are trying to use. – Peter Grill Oct 22 '12 at 07:15 -
@Kayla For more help on writing class files, you may have a look at Style/class tutorials – hpesoj626 Oct 22 '12 at 07:29
-
@Kayla I assume you already have the class file. You can copy and paste the code in the preamble in the second case of Peter's solution, but keep in mind to change
\usepackageinto\RequirePackage– hpesoj626 Oct 23 '12 at 00:15