# Automatically enumerate for two columns and longtable

I’m in the middle of creating a test that has several pages. The example below illustrates its structure.

\documentclass[a4paper,9pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath, amsfonts, amssymb}
\renewcommand{\rmdefault}{ptm}

\usepackage{longtable}
\usepackage[left=0.5cm, right=0.5cm, top=0.5cm, bottom=0.5cm]{geometry}

\usepackage{color}
\newcommand{\B}[1]{ \noindent{\setlength{\fboxsep}{1pt}\colorbox{black}{\color{white}\textbf{#1}}}}

\begin{document}
\begin{center}
\textbf{Test}\\
\end{center}
Names ………………………………………… id …………………………

\begin{longtable}{p{19cm}|p{1cm}}
\textbf{1.} Question about  &   \textbf{1.} \\
a)  Answer 1    &   a)  \\
b)  Answer 2    &   b)  \\
c)  Answer 3    &   c)  \\
d)  Answer 4 &  d)  \\
\textbf{2.} Question is &   \textbf{2.} \\
a)  Answer 1    &   a)  \\
b)  Answer 2    &   b)  \\
c)  Answer 3    &   c)  \\
d)  Answer 4 &  d)  \\
\textbf{3.} Question be &   \textbf{3.} \\
a)  Calculate 1     &   $\ldots$    \\
b)  Calculate 2 &   $\ldots$    \\
c)  Calculate 3 &   $\ldots$    \\
d)  Calculate 4 &   $\ldots$    \\

\end{longtable}
\end{document}


Is it possible to automatically enumerate questions instead of writing e.g. \textbf{1.}? Sometimes I need to delete a question and I have to adjust the numbers manually. The same problem concerns to answers in the right column.

My second question is probably little challenging. I create an answer key that looks almost identical as the test. For example for Q1 and Q3 we have ( \B is defined in the preamble)

\textbf{1.} Question about  &   \textbf{1.} \\
a)  Answer 1    &   \B{a)}  \\
b)  Answer 2    &   b)  \\
c)  Answer 3    &   c)  \\
d)  Answer 4 &  d)  \\
\textbf{3.} Question be &   \textbf{3.} \\
a)  Calculate 1     &   \B{1.2} \\
b)  Calculate 2 &   \B{2.2} \\
c)  Calculate 3 &   \B{3.2} \\
d)  Calculate 4 &   \B{1}   \


I would like to create only the template key but with possibility to switch to the test: \B{a)} --> a) and \B{1.2} --> $\ldots$ if in the preamble I change something.

I would appreciate any suggestions.

• You should look into using the exam document class. It's designed for writing exams, and is pretty flexible. It makes creating the answer key delightfully easy. Is the part that appears on the right intended to be where students write their answers, and circle multiple choice answers? That part is not built into the exam class, and I'm not sure how hard it would be to achieve that. – kmacinnis Apr 6 '13 at 18:27

You can create a counter and a helper macro as follows:

\newcounter{step}
\newcommand\inc{\stepcounter{step}\textbf{\thestep. }}


You can use as:

\inc Question about  &   \thestep \\


For the second part you can define a command between answers with a decimal number and those with an alphabetic result using a conditional such as \IfDecimal from the xstring package.

\newcommand{\B}[1]{%
\IfDecimal{#1}{$\ldots$}{\fbox{#1}}
}


I used \fbox to make the command more readable, but you can replace it with colorbox and the rest of the formatting commands.

Hope that this gets you closer to what you looking for. Just a small general comment a 9pt font is too small for an exam in my opinion.

• Thank you. I see that \thestep is not bold and without dot. My second question can be written (in preamble): WhatToDo = T; IF ( WhatToDo=T) THEN do assigment \B{a)} <-- a) and every \B{any number} <-- $\ldots$. It save times because I don't have to create 2 files: the test and answers. – Robert Apr 6 '13 at 18:02
• @Robert Formatting \thestep is easy if you can think of a reasonably short semantic name perhaps \sn for serial number, \newcommand\sn{\textbf{\thestep.}} You can then use sn, rather than thestep. Latter part of the question food for thought for tomorrow as it is getting late here. – Yiannis Lazarides Apr 6 '13 at 18:21
• I suggest \newcommand\inc[1]{\stepcounter{step}\textbf{\thestep. }#1 & \textbf{\thestep}} so you just use  \inc{Question about}\\  – Sigur Apr 6 '13 at 18:23
• @Yiannis & Sigur: Fabulous. Thank you for the first part of answer. – Robert Apr 6 '13 at 18:49
• I wanted to add to the second question that I could use different definition (for emphasizing the answers) in order to simplify the problem: \B for letters e.g. \B{a)} and \BB for numbers e.g. \BB{1.345}. – Robert Apr 6 '13 at 19:06

I could not understand why do you have two columns for each answer. However, this code works for me

\begin{longtable}{p{19cm}|p{19cm}}
\begin{enumerate}
\begin{enumerate}

\end{enumerate}

\begin{enumerate}
\end{enumerate}
\end{enumerate}
\end{longtable}


and here is the output

hope it works.

• Thank you but 2 columns are important for me. – Robert Apr 6 '13 at 18:08

Thanks to Yiannis & Sigur I have solutions. Someone might find it useful. I know that using conditional would be more elegant but I'm not able to do it.

% Put comments here if one needs to see a test
%\newcommand{\B}[1]{ \noindent{\setlength{\fboxsep}{1pt}\colorbox{black}{\color{white}\textbf{#1}}}}
%\newcommand{\BB}[1]{ \noindent{\setlength{\fboxsep}{1pt}\colorbox{black}{\color{white}\textbf{#1}}}}

\newcommand{\BB}[1]{$\ldots$} % crucial is [1]; thanks Qrrbrbirlbel

It's just enough use \B for a), b), c) and \BB for values.