Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

How can I automate multiple choice question type in my own class in LaTeX?

share|improve this question
1  
Welcome to TeX.sx! –  hpesoj626 Oct 22 '12 at 4: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
add comment

1 Answer 1

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:

enter image description here

Notes:

  • You had a minor typo in your code which I have corrected below. \end parts should have been be \end{parts}.
  • You should also be using the choices instead of parts environment 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:

enter image description here

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 use hfill for the itemjoin option:

     \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*.}

% http://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}
share|improve this answer
    
@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 5:42
    
@hpesoj626: Have updated solution for horizontal choices. –  Peter Grill Oct 22 '12 at 5:52
    
@Kayla: Will update solution such that you do not use exam class (I think I understand, but not sure). –  Peter Grill Oct 22 '12 at 5:52
    
@PeterGrill thank u sir –  Kayla Oct 22 '12 at 6:09
    
@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 6:32
show 15 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.