Graphics, Figures & TablesCenter align lists in cells

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
bstex
Posts: 69
Joined: Mon Oct 23, 2017 10:22 am

Center align lists in cells

Post by bstex »

Hi,
I'm trying to center align the lists inside the cells of a table.
Till now, I have this:

Code: Select all

\documentclass[a4paper]{book}
\usepackage{array}
\usepackage{enumitem}
\begin{document}

\begin{table}[h]
\centering
\begin{tabular}{|>{\centering\arraybackslash} m{10em}|>{\centering\arraybackslash} m{8em}|>{\centering\arraybackslash} m{9em}|}
\multicolumn{1}{|c|}{A} & \multicolumn{1}{c|}{B} & \multicolumn{1}{c|}{C} \\

\begin{enumerate}
\item [A.] text
\item [D.] text
\item [E.] text
\end{enumerate}
&
\begin{enumerate}[label=\textbf{\arabic*.}]
\item text
\item text
\item text
\end{enumerate}
&
\begin{enumerate}
\item [B -] text
\item [C -] text
\item [F -] text
\end{enumerate}
\end{tabular}
\end{table}

\end{document}
which produces this:
Screen Shot 2018-01-19 at 20.20.41.png
Screen Shot 2018-01-19 at 20.20.41.png (17.14 KiB) Viewed 5997 times
Why lists don't cooperate with the center-align? Any help?

Also, a general question about tables:
Is there any way to remove the space which appears under the last row of the table? It always seems like there is an extra empty row under the last text.

Thank you.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Stefan Kottwitz
Site Admin
Posts: 10324
Joined: Mon Mar 10, 2008 9:44 pm

Center align lists in cells

Post by Stefan Kottwitz »

Hi,

\centering within the enumerate environment works:

\begin{enumerate}\centering ...

It seems like there is an extra line because there's a space before and after enumerate lists. enumerate is for numbered lists, but is not designed for using in table cells.

Stefan
LaTeX.org admin
bstex
Posts: 69
Joined: Mon Oct 23, 2017 10:22 am

Center align lists in cells

Post by bstex »

I must have been misunderstood. Sorry, my fault.
I need the lists to be as they are right now (left-aligned), but the whole list to be placed in the center of the cell.
I mean something like this:
Screen Shot 2018-01-19 at 22.11.08.png
Screen Shot 2018-01-19 at 22.11.08.png (19.01 KiB) Viewed 5990 times
Thank you, Stefan!
bstex
Posts: 69
Joined: Mon Oct 23, 2017 10:22 am

Center align lists in cells

Post by bstex »

Since you told me about the enumerate conflict with tables, I will remove the enumerate and insert the text as a plain text and not as a list.
Thank you again.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10324
Joined: Mon Mar 10, 2008 9:44 pm

Center align lists in cells

Post by Stefan Kottwitz »

A not so easy workaround: putting the enumerate in a minipage or use \parbox, estimate the width, or calculate it, such as:

Code: Select all

\usepackage{varwidth}
\newlength\lenA
\newsavebox\boxA
\savebox\boxA{\begin{varwidth}{\textwidth}[A.]\qquad text text\end{varwidth}}
\setlength\lenA{\the\wd\boxA}
% above goes to the preamble
...
%in the table cell:
\parbox{\lenA}{%
  \begin{enumerate}
  \item [A.] text text
  \item [D.] text
  \item [E.] text
  \end{enumerate}}
...
Complete code for testing:

Code: Select all

\documentclass[a4paper]{book}
\usepackage{array}
\usepackage{enumitem}

\usepackage{varwidth}
\newlength\lenA
\newsavebox\boxA
\savebox\boxA{\begin{varwidth}{\textwidth}[A.]\qquad text text\end{varwidth}}
\setlength\lenA{\the\wd\boxA}

\begin{document}
 
\begin{table}[h]
\centering
\begin{tabular}{|>{\centering\arraybackslash} m{10em}|>{\centering\arraybackslash} m{8em}|>{\centering\arraybackslash} m{9em}|}
\multicolumn{1}{|c|}{A} & \multicolumn{1}{c|}{B} & \multicolumn{1}{c|}{C} \\
 
\parbox{\lenA}{%
  \begin{enumerate}
  \item [A.] text text
  \item [D.] text
  \item [E.] text
  \end{enumerate}}
&
\begin{enumerate}[label=\textbf{\arabic*.}]
\item text
\item text
\item text
\end{enumerate}%
&
\begin{enumerate}
\item [B -] text
\item [C -] text
\item [F -] text
\end{enumerate}%
\end{tabular}
\end{table}
 
\end{document}
But yes, simple text would be much easier.

Stefan
LaTeX.org admin
bstex
Posts: 69
Joined: Mon Oct 23, 2017 10:22 am

Center align lists in cells

Post by bstex »

Thanks.
I'll give a shot that solution too. ;)
Post Reply