6

I have used the enumerate environment to create a list in which every item contains a table, created using the tabular environment. The first row of each table should be aligned with the item label, (a), (b), etc. This is not the the case if the table starts with an \hline. How can I fix the alignment in such a case?

\documentclass{article}
\usepackage{enumerate} 
\begin{document}
\begin{enumerate}[(a)]
\item 
   \begin{tabular}[t]{|c|c|}
     a & b \\  \hline c & d  \\ \hline
   \end{tabular}
\item 
   \begin{tabular}[t]{|c|c|} 
     \hline e & f \\ \hline g & h \\ \hline
   \end{tabular}
\end{enumerate}
\end{document}

MWE

3 Answers 3

6

I just had to solve this problem, and "The LaTeX Companion" (second edition) has the answer on page 280. The [t] option aligns the tabular to the \hline instead of the baseline of the text. The array package has the command \firsthline which corrects this:

\documentclass{article}
\usepackage{enumerate,array} 
\begin{document}
\begin{enumerate}[(a)]
\item 
   \begin{tabular}[t]{|c|c|}
     a & b \\  \hline c & d  \\ \hline
   \end{tabular}
\item 
   \begin{tabular}[t]{|c|c|} 
     \firsthline e & f \\ \hline g & h \\ \hline
   \end{tabular}
\end{enumerate}
\end{document}

This is the result:

firsthline fix

2

With {NiceTabular} of nicematrix, you will directly have the expected output.

\documentclass{article}
\usepackage{nicematrix}
\usepackage{enumerate} 
\begin{document}

\begin{enumerate}[(a)]
\item 
   \begin{NiceTabular}[t]{|c|c|}
     a & b \\  \hline c & d  \\ \hline
   \end{NiceTabular}
\item 
   \begin{NiceTabular}[t]{|c|c|} 
     \hline e & f \\ \hline g & h \\ \hline
   \end{NiceTabular}
\end{enumerate}

\end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code

1

i don't know what is the cause of your problem, however work around can be use \fbox instead top/bottom (and vertical outer lines too) table's \hline:

\documentclass{article}
\usepackage{enumerate}
\newcommand\tabbox[1]{\setlength\fboxsep{0pt}\fbox{#1}} % <-- new

\begin{document}
\begin{enumerate}[(a)]
\item
   \begin{tabular}[t]{|c|c|}
     a & b \\  \hline c & d  \\ \hline
   \end{tabular}
\item
   \tabbox{\begin{tabular}[t]{c|c} % <-- new
            e & f \\ \hline g & h \\
           \end{tabular}}
\end{enumerate}
\end{document}

enter image description here

2
  • Elegant workaround, just what I needed! Thanks! Minor remark: since the \fbox creates the outer box, the vertical line specification for the tabular environement can be simplified from {|c|c|} to {c|c}, else the outer vertical lines are printed 'twice' and might look thicker than the other lines (at least on my monitor).
    – jvd
    Oct 19, 2017 at 8:53
  • you have right. better is delete the outer vertical lines. the answer is now corrected accordingly.
    – Zarko
    Oct 19, 2017 at 9:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .