3

The basic problem is that I need an enumeration in a table cell. The special requirement is to have the text in the cell (or whole column) left to this cell top-aligned.

My first approach is:

\begin{table}[h!]
\begin{tabular}{r|l}
    \textbf{Input} &
  some text\\
  \textbf{Work} &
  \parbox{\linewidth}{\begin{enumerate}
      \item The first step 
      \item The second step which is very long, so this text is more than the table width and should be broken .
    \end{enumerate}}\\
    \textbf{Output}: & some other text.\\
\end{tabular}
\end{table}

Which yields to: enter image description here

As you see, the text in the left column is not top aligned. I tried multiple ways, e.g. this one but could not fix it.

My second approach was to use nested tables:

\begin{table}[h!]
\begin{tabular}{r|l}
    \textbf{Input} &
  some text\\
  \textbf{Work} &
  \begin{tabular}[t]{ll}
            1. & The first step \\
            2. & The second step which is very long, so this text is more than the table width and should be broken.\\ 
            \end{tabular}
            \\
    \textbf{Output}: & some other text.\\
\end{tabular}
\end{table}

Which fixes the alignment issue, but cuts the text: enter image description here

I tried to fix the width of the inner table (see here) but this also did not work.

How can I have an enumeration in a table with the cells to the left top-aligned? Thank you!

1
  • why not use package multirow
    – js bibra
    Mar 24, 2021 at 6:55

2 Answers 2

2

I suggest you (a) use a tabularx environment, to avoid the tedium of having to calculate the width of the second column, and (b) use a minipage[t] environment to encase the enumerate environment.

enter image description here

\documentclass{article}
\usepackage{enumitem}
\usepackage{tabularx} % for 'tabularx' env. and 'X' column type
\begin{document}
\begin{table}[h!]
\begin{tabularx}{\textwidth}{ @{} r | X @{} }
  \textbf{Input}  & Some text.\\
  \textbf{Work}   &
      \begin{minipage}[t]{\linewidth}
      \raggedright % optional
      \begin{enumerate}[noitemsep]
         \item The first step 
         \item The second step is very long, so this text exceeds the 
               table width and should be line-broken automatically.\strut
     \end{enumerate}
     \end{minipage}\\
  \textbf{Output} & Some other text.\\
\end{tabularx}
\end{table}
\end{document}
2
  • how to get align=left for the enumerate items so that 1. starts under the S(ome) in first row
    – js bibra
    Mar 24, 2021 at 7:07
  • 1
    @jsbibra - Just add the option left=0pt to the optional argument of the enumerate environment.
    – Mico
    Mar 24, 2021 at 7:09
0

enter image description here

%Preamble
\usepackage{multirow}
%______________________%
\begin{tabular}{r|l}
    \textbf{Input} &
    some text\\
\multirow{-3}{*}{\textbf{Work}} &
    \parbox{\linewidth}{\begin{enumerate}
            \item The first step 
            \item The second step which is very long, so this text is more than the table width and should be broken .
    \end{enumerate}}\\
    \textbf{Output}: & some other text.\\
\end{tabular}
1
  • 1
    You need to subtract ((width of the first column)+4\tabcolsep+\arrayrulewidth) from the width of the second column (which is currently set to \linewidth).
    – Mico
    Mar 24, 2021 at 7:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.