1

I'm creating a tool to export wiki content to LaTeX (http://extensions.xwiki.org/xwiki/bin/view/Extension/LaTeX/Syntax%201.0/) and in my wiki syntax (and in HTML too), table cells can contain anything (lists, sections, tables, multi lines, etc).

I need to find a way to generate valid LaTeX that does something similar.

I'd also like to not have to specify any column width the table columns since I don't have this information in my inputs (the wiki syntax usually generates HTML and HTML does the column width computation).

Initially I did this:

\begin{center}
\begin{tabular}{|l|l|l|}
\hline
cell 1 & cell 2  &  cell 3\\
\hline
\end{tabular}
\end{center}

This works fine and I like the result and the fact that column width auto adjust and that column widths have the minimum width to contain the cell data.

However the "l" column type doesn't support having elements such as lists.

I tried various options but none achieved what I need.

For example I tried the tabularx environment and the "X" column type but it generates equal-size columns and the table takes the full line width.

I also looked at the minipage environment which sounds promising but I have to specify a width.

Is there a way to achieve this need in LaTeX?

Thank you PS: I'm new to LaTeX (just started learning it 2 weeks ago)

  • Try p{5cm} instead of l, this gives you a column 5cm wide and enumerate or itemize are possible in there. (Actually, the column width is 5cm + 2 * \tabcolsep`, i.e. the separating space to the left and right of a column – user31729 Mar 3 '18 at 9:21
  • You are in for a rough ride. You can take a look at the latex part of the Sphinx Python tool (here). You will see that it uses among other things the varwidth recommended by HO in his answer. But it also uses tabulary which causes more problems than it solves (in future, it will get removed from Sphinx and replaced by more Python pre-processing). You will have many headaches with verbatim contents and also with splitting things across pages. Once you reach the stage you want to get tables inside tables in a breakable way – user4686 Mar 3 '18 at 10:43
  • ..you will know what suffering means. Perhaps you will want your text to flow around figures and people will mention wrapfig to you? well this can not be used in an automated way because it interacts badly with a LaTeX basic structure, lists. It always requires manual tuning and that applies to most everything LaTeX2e. – user4686 Mar 3 '18 at 10:43
2

Package varwidth can help. It tries to reduce the width if possible:

\documentclass{article}
\usepackage{booktabs}
\usepackage{varwidth}

\begin{document}
\begin{center}
\begin{tabular}{lll}
  \toprule
  header 1 & header 2  &  header 3\\
  \midrule
  cell 4 &
  \begin{varwidth}[t]{\linewidth}
    \settowidth{\leftmargini}{\labelitemi}%
    \addtolength{\leftmargini}{\dimexpr\labelsep-\itemindent\relax}%
    \begin{itemize}
      \item item a
      \item item b
      \item item c
    \end{itemize}
  \end{varwidth} &
  \begin{varwidth}[t]{\linewidth}
    \settowidth{\leftmargini}{3.}%
    \addtolength{\leftmargini}{\dimexpr\labelsep-\itemindent\relax}%
    \begin{enumerate}
      \item foo
      \item bar
      \item baz
    \end{enumerate}
  \end{varwidth} \\
  \bottomrule
\end{tabular}
\end{center}
\end{document}

Result

If the lines are too long, TeX will complain with overfull \hbox warnings. Then, either insert manual line breaks to limit the line lengths or switch to fixed width columns.

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.