[Tex/LaTex] Two tables horizontally aligned at the top

formattinghorizontal alignmenttables

I am struggling to get two tables aligned at the top (horizontal alignment). I have consulted Align the top of two tables horizontally (side-by-side with minipage), How can I have two tables side by side?, Make two figures aligned at top, Top-align two parboxes with tables, and many, many more. I have tried \parbox and \minipage but haven't had any luck. When I comment out the \minipage code, I get the same thing that I do when the \minipage code is in there. What's really odd is that I had it aligned using \parbox, except the second table was aligned with the center of the first table. When I thought I found a fix for it (using \parbox[t]{} ), the tables were then outputted as table 1 (answer box) and then table 2 (scoring box) below it (instead of next to it). When I removed the [t], nothing changed… the two tables are now stubbornly stacked on top of each other instead of next to each other. I have been working between \minipage and \parbox as I research new solutions on here, but I am still coming up empty.

(Does the document class have anything to do with it? Most of the document classes I've found on here are {article} and not {exam}?)

I am relatively new to LaTeX! Thank you for your patience.

\documentclass[12pt]{exam}
\usepackage{multirow} % for tables

\pagestyle{headandfoot}

\usepackage[margin=1in]{geometry}
\singlespacing

%to make cells in table have decent spacing
\usepackage{array}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}


\begin{document}

\begin{table}

%heading
\begin{flushright}
\begin{tabular}[t]{p{3.8in} r l}
\textbf{class} & \textbf{Name:} & \makebox[2in]{\hrulefill}\\
\textbf{exam} &&\\
\textbf{date} &&
\end{tabular}
\end{flushright}
\rule[1ex]{\textwidth}{.1pt}

%answer box 
\begin{minipage}[t]{0.45\linewidth}
\begin{tabular}[t]{ | C{1cm} | R{1.5cm} | }
    \hline
    1. &  \\ 
        & \\ \hline
    2. &  \\ 
        & \\ \hline
    3. &   \\
        & \\ \hline
    4. &   \\
        & \\ \hline
    5. &   \\
        & \\ \hline
    6. &   \\
        & \\ \hline
    7. &   \\ 
        & \\ \hline
\end{tabular} 
\end{minipage}


%scoring box
\begin{minipage}[b]{0.45\linewidth}
\begin{tabular}[t]{ | c | R{2cm} | R{1.5cm} | }
    \hline
    \multirow{2}{*}{Multiple Choice} & x $(9/7)$ & \\ 
                            &       & \\ \hline
    \multirow{2}{*}{Free Response} & x 1      & \\ 
                            &       & \\ \hline
    \multicolumn{1}{ c }{} & Your Score out of 18 &      \\ \cline{3-3} 
\end{tabular} \\
\end{minipage}


\end{table}
\end{document}

Best Answer

You can use a single tabularx in place of two tables. By this, you needn't use minipages to spread the tables a part. The columns of tabularx will be the same as your two separate tables plus an X column type at the middle to fill the remaining horizontal space. Note also that I used tabularx for the first table. This enabled us to use \hrulefill directly without \makebox.

\documentclass[12pt]{exam}
\usepackage{tabularx}  % for tabularx
\usepackage[margin=1in]{geometry}

% to make cells in table have decent spacing
\usepackage{array}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

\begin{document}

\begin{table}
% heading
\begin{tabularx}{\linewidth}{@{}p{3.8in} X@{}}
\textbf{class} & \textbf{Name:} \hrulefill \\
\textbf{exam}  &                           \\
\textbf{date}  &                
\end{tabularx}

\addvspace{1em} 
\hrule height .1pt 
\addvspace{1em}

% answer box 
\renewcommand{\arraystretch}{2}
\begin{tabularx}{\linewidth}{|C{1cm}|R{1.5cm}| X |c|R{2cm}|R{1.5cm}|}
    \cline{1-2}\cline{4-6}
    1. &  &  & Multiple Choice & ${}\times(9/7)$ & \\ \cline{1-2}\cline{4-6} 
    2. &  &  & Free Response   & ${}\times1$     & \\ \cline{1-2}\cline{4-6}
    3. &  & \multicolumn{1}{c}{} & \multicolumn{2}{r|}{Your Score out of 18} & \\ \cline{1-2}\cline{6-6}
    4. &   \\ \cline{1-2}
    5. &   \\ \cline{1-2}
    6. &   \\ \cline{1-2}
    7. &   \\ \cline{1-2}  
\end{tabularx} 
\end{table}

\end{document}

enter image description here

Yet another simpler option is to use two tabulars with the [t] alignment option as follows (with the same result as above):

\begin{tabular}[t]{|C{1cm}|R{1.5cm}|}
    \hline
    1. &  \\ \hline
    2. &  \\ \hline
    3. &  \\ \hline
    4. &  \\ \hline
    5. &  \\ \hline
    6. &  \\ \hline
    7. &  \\ \hline
\end{tabular}
\hfill
\begin{tabular}[t]{|c|R{2cm}|R{1.5cm}|}
   \hline
   Multiple Choice & ${}\times(9/7)$ & \\ \hline
   Free Response   & ${}\times1$     & \\ \hline
   \multicolumn{2}{r|}{Your Score out of 18} & \\ \cline{3-3}
\end{tabular}