[Tex/LaTex] Latex Table in Exam Class

examlongtabletables

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}
\usepackage[export]{adjustbox}
\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}}
\runningheadrule
\pagestyle{headandfoot}
\runningheader{Competitive Examination}{}{}
\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 & 
\\
 \ 3) This is dummy value al dfadf adf adfasdf \ ddfakdfaasdf .\  & 
 \ c) values are here for all fasdf ad asdfasdkfad.\  & 
\\
 \ 4) asd fads adsfkaj dkladjsflkadsf asdfa skdlj, asdfasdasdfasdfa & 
 \ d) adjfklasd fasldfjasdlkfjasdlkfadsflkja sdflkjasd;lfkjasd;lfkja;sldkfjals;dk\  & 
\\
 \ 5) asdfasdfjas;dlfkjaslkdalkdsjf;alksdjfasdklfads & 
 \ 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 enter image description here

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?

Best Answer

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
\item This is dummy value al dfadf adf adfasdf \ ddfakdfaasdf .
\item asd fads adsfkaj dkladjsflkadsf asdfa skdlj, asdfasdasdfasdfa
\item asdfasdfjas;dlfkjaslkdalkdsjf;alksdjfasdklfads
\end{enumerate}
& 
B
\begin{enumerate}
\renewcommand{\labelenumi}{\alph{enumi})}
\item there is not dummy text
\item THis is not the correct word
\item values are here for all fasdf ad asdfasdkfad.
\item adjfklasd fasldfjasdlkfjasdlkfadsflkja sdflkjasd;lfkjasd;lfkja;sldkfjals;dk
\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:

enter image description here


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 \\

This is dummy value al dfadf adf adfasdf \ ddfakdfaasdf . & 
values are here for all fasdf ad asdfasdkfad.\\

asd fads adsfkaj dkladjsflkadsf asdfa skdlj, asdfasdasdfasdfa & 
adjfklasd fasldfjasdlkfjasdlkfadsflkja sdflkjasd;lfkjasd;lfkja;sldkfjals;dk\\

asdfasdfjas;dlfkjaslkdalkdsjf;alksdjfasdklfads &
\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:

enter image description here

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.

Related Question