1

I want to put an itemization inside a table's cell and came across the tabu/longtabu package. Like tabularx it provides a X column that allows for including an itemization, i.e. something that spreads over multiple lines.

My goal is to create a table with two columns that is centered and is only as width as its content requires it to be (no spanning, spreading, ...!). However, it seems like I always have to specify a width:

\begin{longtabu} to \linewidth [c]{|r|X[-l]|} % \linewidth is not what I want
    some text  & \begin{itemize}[label={},leftmargin=*]
                     \item 11
                     \item 22
                 \end{itemize}  
\end{longtabu}

I could replace \linewidth with something like 10cm but that's a static and stupid solution. I basically don't know the required width because I fill the itemization inside the table dynamically during runtime.

Unlike to tabular which only spreads over the actually required width but doens't allow line breaks:

\begin{tabular}{rl} % works like I want it to work
    some text  & \begin{itemize}[label={},leftmargin=*] % fails
                     \item 11
                     \item 22
                 \end{itemize}  
\end{tabular}

I'm looking for an easy solution (no hacky solutions with command redefinitions, ...) for an easy problem like itemizations inside tables.

2
  • that is the nature of tex boxes, if it is one line like \mbox or an l column, it has a natural width, but if it allows line breaking like a p column or a \parbox then you need to tell tex in advance the target width. (you could try using an l column containing a varwidth environment containing your list (using the varwidth package) Mar 10, 2017 at 16:53
  • 1
    your \item is 11 here but if it was a whole paragraph of text, what do you what the line breaking to do, if you do not want to specify a width? Mar 10, 2017 at 16:55

2 Answers 2

1

Simple solution by using varwidth package:

\begin{table}
    \centering
    \setlength{\tabcolsep}{3pt} % add some value otherwise LaTeX puts no space at all between the columns
    \begin{tabular}{rl}
        \LaTeX~is:  & \begin{varwidth}[t]{\linewidth}\begin{itemize}[nosep,after=\strut,label={},leftmargin=0pt]
                \item Nice and powerful,
                \item but sometimes off target.
        \end{itemize}\end{varwidth}
    \end{tabular}
\end{table}

will result in

enter image description here

Note 1: Replace t with c if you want the left colum's content to be vertically centered.

Note 2: Specifying \linewidth for varwidth is only a workaround since I don't know the actual maximum column width. Make sure your itemization isn't to wide and doesn't consume more space that the column can provide.

0

TikZ workaround:

\documentclass{article}

\usepackage{enumitem}
\usepackage{tikz}

\begin{document}
\noindent
\begin{tabular}{|r|l|} % works like I want it to work
    \hline
    some text  &
    \begin{tikzpicture}[anchor=west, y=.5cm]
        \node[anchor=west] at (0, 0)  {looooooooooooong item};
        \node[anchor=west] at (0,-1)  {short item};
        \node[anchor=west] at (0,-2)  {tiny};
    \end{tikzpicture}
    \\
    \hline
\end{tabular}
\end{document}

Result:

enter image description here

Don't use vertical lines, nor \hline; I used them in order to highlight this 1 by 2 table.

You must log in to answer this question.