# Latex Table in Exam Class

I'm trying to add Question Type : Match the Pairs using EXAM Class use code mentioned below : [FYI: I'm converting HTML Table into Latex table. I'm using pdflatex to get pdf from tex file mentioned below.]

\documentclass[addpoints,answers,10pt]{exam}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{tabularx}
\usepackage{nonfloat}
\usepackage{caption}
\usepackage{amsmath,amssymb}
\usepackage{lmodern}
\usepackage{textcomp}
\usepackage{ifpdf}
\newenvironment{Figure}
{\par\medskip\noindent\minipage{\linewidth}}
{\endminipage\medskip\par}
\marksnotpoints
\bracketedpoints
\pointsdroppedatright
\qformat{Q. \thequestion: \thequestiontitle\hfill\thepoints}
\renewcommand{\thesubsubpart}{\arabic{subsubpart}}
\renewcommand{\thechoice}{\arabic{choice}}
\firstpagefooter{}{Page \thepage}{}
\runningfooter{}{Page \thepage}{}
\footrule
\renewcommand{\thepartno}{\Alph{partno}}
\begin{document}
\begin{center} {\centering \textbf{\Large{Dummy Value}}} \end{center}
\small{ABCD1234} \hspace*{\fill}
\small{TERMINAL EXAMINATION} \hspace*{\fill}
\small{Marks: 60}
\newline
\small{SSC-SET1} \hspace*{\fill}
\small{Science (Ch - 1,2,3)} \hspace*{\fill}
\small{2\textonehalf Hours}
\newline
\noindent\makebox[\linewidth]{\rule{\textwidth}{1pt}}
\begin{center} {\centering \textbf{SECTION A}} \end{center}
\begin{questions}
\titledquestion{Match the Pairs}[10]
\begin{table}[ht] \begin{tabular}{l l l} \hline \hline
A &
B &
\\
\ 1) Why this text is dummy over here &
\ a) there is not dummy text &
\\
\ 2) THis is one word &
\ b) THis is not the correct word &
\\
\\
\\
\ e) adjfklasdjfa;lksdjf;alskdjf;alskdjf;alksdjf;laksdjf;alksdfj;alksdfja;lksdfja;lksdfja;lskdfj;alksdjfalksdfjasdklfjas;dlfja;sdlkfalkdjfa;lkdsjf;alksdfaldfja;lsdkfja;lsdkjf;alskdfj;aldkjf;alksdjfalksddddddfajsdlkfja;lsdkjf ;alsdjf ;asdjf; ajdf;l j;asdlfj ;alkdjsf;laksejriaeidkl ja;dlkfja;slkdfj a;ldskfja;sdikdfka dof asdf asdfk;lajsd;flkaj d;lkfajsd;lfk ja;dsfj ;alskdjf; lkadsjf;alksdjf; alsdfkja;lskdjf;alsdjf;alsdkjf;alskdjf\  &
\\
\hline \end{tabular} \label{table:nonlin} \end{table}
\end{questions}
\end{document}


Generated output is mentioned below

I'm facing two problems in this code :

• Match the pair table is coming above question title
• If there is longer word or more text, it's going outside pagewidth

Can you please give me direction to solve these errors?

• I would suggest using the tabularx package as you've already included in your preamble and use \noindent\begin{tabularx}{\linewidth}{XX}...\end{tabularx} to set the "match the pairs table. This will allow for proper line breaking while also filling the text block width. – Werner Aug 10 '13 at 4:57
• Now both issues are solved. Thanks for your help. – Pawan Mude Aug 10 '13 at 5:13
• I updated my answer, check it out! – Francis Aug 10 '13 at 9:45

Here are some more to add on Phil and Werner's answer: first, for list structure, you should consider using enumerate environment; second, even with tabularx LaTeX will still have some trouble handling extremely long word which had appeared in your example, in this case you can either use \hyphenation{...} to manually set up hyphenation or use \seqsplit{...} provided by seqsplit package. Now here is a demonstration:

\usepackage{seqsplit}


Here I skip the preamble for it's too long

\begin{questions}
\titledquestion{Match the Pairs}[10]
\noindent
\begin{tabularx}{\linewidth}{XX}
\hline
\hline
A
\begin{enumerate}
\renewcommand{\labelenumi}{\arabic{enumi})}
\item Why this text is dummy over here
\item THis is one word
\end{enumerate}
&
B
\begin{enumerate}
\renewcommand{\labelenumi}{\alph{enumi})}
\item there is not dummy text
\item THis is not the correct word
\item
\seqsplit{adjfklasdjfa;lksdjf;alskdjf;alskdjf;alksdjf;laksdjf;alksdfj;alksdfja;lksdfja;lksdfja;lskdfj;alksdjfalksdfjasdklfjas;dlfja;sdlkfalkdjfa;lkdsjf;alksdfaldfja;lsdkfja;lsdkjf;alskdfj;aldkjf;alksdjfalksddddddfajsdlkfja;lsdkjf ;alsdjf ;asdjf; ajdf;l j;asdlfj ;alkdjsf;laksejriaeidkl ja;dlkfja;slkdfj a;ldskfja;sdikdfka dof asdf asdfk;lajsd;flkaj d;lkfajsd;lfk ja;dsfj ;alskdjf; lkadsjf;alksdjf; alsdfkja;lskdjf;alsdjf;alsdkjf;alskdjf}
\end{enumerate}
\\
\hline
\end{tabularx}


and its result:

# Update

After thinking a while about your new request I came up a way to mimic the enumerate environment with tabularx. First of all, we need to define two new counters:

\newcounter{counterA}
\setcounter{counterA}{0}

\newcounter{counterB}
\setcounter{counterB}{0}


Then we need two new column types:

\newcolumntype{A}{
@{
\stepcounter{counterA}
\hspace{\itemindent}
\arabic{counterA})
\hspace{\labelsep}
\vspace{\itemsep}
}X}

\newcolumntype{B}{
@{
\stepcounter{counterB}
\hspace{\itemindent}
\alph{counterA})
\hspace{\labelsep}
\vspace{\itemsep}
}X}


Now let's check out the code (again this is only the crucial part):

\begin{questions}
\titledquestion{Match the Pairs}[10]
\begin{tabularx}{\linewidth}{AB}
\hline
\hline

\multicolumn{2}{l}{A\hspace{216.46775pt}B}\\[\topsep]

Why this text is dummy over here &
there is not dummy text\\

THis is one word &
THis is not the correct word \\

\seqsplit{adjfklasdjfa;lksdjf;alskdjf;alskdjf;alksdjf;laksdjf;alksdfj;alksdfja;lksdfja;lksdfja;lskdfj;alksdjfalksdfjasdklfjas;dlfja;sdlkfalkdjfa;lkdsjf;alksdfaldfja;lsdkfja;lsdkjf;alskdfj;aldkjf;alksdjfalksddddddfajsdlkfja;lsdkjf ;alsdjf ;asdjf; ajdf;l j;asdlfj ;alkdjsf;laksejriaeidkl ja;dlkfja;slkdfj a;ldskfja;sdikdfka dof asdf asdfk;lajsd;flkaj d;lkfajsd;lfk ja;dsfj ;alskdjf; lkadsjf;alksdjf; alsdfkja;lskdjf;alsdjf;alsdkjf;alskdjf}\\

\hline
\end{tabularx}
\end{questions}


that will give us:

As you can see, what we got is quite similar to the implemented enumerate environment we previously have, and this time we also have nice vertical alignment. However, this method still has some imperfections, for instances:

1. There are some mysterious indentation caused by seqsplit I don't quite understand.
2. In the first line of the table,\topsep is from the list environment. I tried to put it into column definition with \ifnum but failed :P
3. 216.46775pt is manually calculated 0.5\linewidth-\columnsep, I don't know how to make calc work inside a table so I didn't use it.

et cetera et cetera.

• Thanks Francis for answers. Basically I'm converting HTML table into Latex. Currently I'm reading TR TD in as sequence and generating latex code, that's reason i'm getting one extra column at the end. Now using tabularx i'm controlling width of last column (to make it invisible). Is there any way to achieve output mentioned above like : \item Why this text is dummy over here & \item there is not dummy text (next to each other) ? – Pawan Mude Aug 10 '13 at 6:53
• @PawanMude not sure if I get it, so you want separate items be on the same line? – Francis Aug 10 '13 at 8:01

To fix the problem of the table appearing above the question title, you should not have your tabular environment inside of a table environment. The table environment is a floating environment, which isn't really what you want anyway, since you want the table to appear exactly where you put it, i.e., you don't want it to float to a different location.

• I've removed table and problem of "Match the pair table coming above question title" is solved . Thanks for your quick help. – Pawan Mude Aug 10 '13 at 4:48
• Add my tabularx suggestion to complete the solution. – Werner Aug 10 '13 at 4:58
• Now both issues are solved. Thanks for your help. – Pawan Mude Aug 10 '13 at 5:13