# How to wrap comma-separated math terms in a table?

Correct Image:

Output from LaTeX:

Hi everyone. Anyone can please help me to solve this problem? My words won't wrap into the table as shown in picture 2.

\documentclass{article}
\usepackage{array}
\newcolumntype{L}{>{\centering\arraybackslash}m{3cm}}

\begin{document}

\begin{table}
\begin{tabular}{|c|c|L|L|}\hline
$A$ & $B$ & $A.B$ & $B.A$\\\hline
$\{x^2,x^3,y,xy\}$ & \{$1,x,x^2y,x^3y\}$ & $\{x^2,x^3,y,xy,x^3,1,xy,x^2y,y,x^3y,x^2,x,xy,y,x^3,x^2\}$ & $\{x^2,x^3,y,xy,x^3,1,xy,x^2y,y,x^3y,x^2,x,xy,y,x^3,x^2\}$ \\ \hline
\end{tabular}
\end{table}
\end{document}


Here is my latex code.

Thank you for your help.

• Off-topic: Should A.B and B.A maybe be typeset as A\cdot B and B\cdot A? Please advise.
– Mico
Commented Nov 30, 2017 at 17:36

Here are two solutions that use a tabularx environment to eliminate pointless guess-work as to how wide the 3rd and 4th columns should be. Both solutions make use of a macro called \breakingcomma, obtained from an earlier answer by @egreg, to permit TeX to break lines immediately after commas.

The former solution uses your setup of vertical and horizontal rules; the second uses no vertical rules and only well-spaced horizontal rules.

\documentclass{article}
\usepackage{tabularx,booktabs,ragged2e,breqn}
\renewcommand{\tabularxcolumn}[1]{m{#1}}
\newcolumntype{C}{>{\Centering\arraybackslash$}X<{$}}
\usepackage[skip=0.333\baselineskip]{caption}

% see https://tex.stackexchange.com/a/60761/5001 for source of \breakingcomma
% (requires the "breqn" package; that's why it's loaded above)
\newcommand{\breakingcomma}{%
\begingroup\lccode~=,
\lowercase{\endgroup\expandafter\def\expandafter~\expandafter{~\penalty0 }}}

\begin{document}

\begin{table}
\caption{Using both vertical and horizontal rules}
\breakingcomma
\begin{tabularx}{\textwidth}{|*{2}{>{$}c<{$}|}*{2}{C|}}
\hline
A & B & A.B & B.A\\
\hline
\{x^2,x^3,y,xy\} &
\{1,x,x^2y,x^3y\} &
\{x^2,x^3,y,xy,x^3,1,xy,x^2y,y,x^3y,x^2,x,xy,y,x^3,x^2\} &
\{x^2,x^3,y,xy,x^3,1,xy,x^2y,y,x^3y,x^2,x,xy,y,x^3,x^2\} \\ \hline
\end{tabularx}
\end{table}

\begin{table}[h]
\caption{Using only well-spaced horizontal rules}
\breakingcomma
\begin{tabularx}{\textwidth}{@{}*{2}{>{$}c<{$}}*{2}{C}@{}}
\toprule
A & B & A.B & B.A\\
\midrule
\{x^2,x^3,y,xy\} &
\{1,x,x^2y,x^3y\} &
\{x^2,x^3,y,xy,x^3,1,xy,x^2y,y,x^3y,x^2,x,xy,y,x^3,x^2\} &
\{x^2,x^3,y,xy,x^3,1,xy,x^2y,y,x^3y,x^2,x,xy,y,x^3,x^2\} \\
\bottomrule
\end{tabularx}
\end{table}

\end{document}

• \allowbreak: good idea! (+1) Commented Nov 30, 2017 at 16:39
• @Zarko - It just got even better, after I located an earlier posting by egreg (who else?!) that showed how to let TeX break lines automatically after commas. :-)
– Mico
Commented Nov 30, 2017 at 16:42

Try this:

\documentclass{article}
\usepackage{array}
\newcolumntype{L}{>{\raggedright\arraybackslash}p{3cm}}
\begin{document}
\begin{tabular}{|l|l|L|L|}
\hline
$A$ & $B$ & $A.B$ & $B.A$\\
\hline
$\{x^2$, $x^3$, $y$, $xy\}$ &
\{$1$, $x$, $x^2y$, $x^3y\}$ &
$\{x^2$, $x^3$, $y$, $xy$, $x^3$, $1$, $xy$, $x^2y$, $y$, $x^3y$, $x^2$, $x$, $xy$, $y$, $x^3$, $x^2\}$ &
$\{x^2$, $x^3$, $y$, $xy$, $x^3$, $1$, $xy$, $x^2y$, $y$, $x^3y$, $x^2$, $x$, $xy$, $y$, $x^3$, $x^2\}$ \\
\hline
\end{tabular}
\end{document}

• Thank you for your help. I had been struggling with this problem for a few days. Commented Nov 30, 2017 at 15:45

You can use the split environment from amsmath package inside a minipage like this:

\documentclass{article}
\usepackage{array,tabularx,amsmath}
\newcolumntype{C}{>{\centering\arraybackslash}X}

\begin{document}

\begin{table}
\begin{tabularx}{\linewidth}{|c|c|C|C|}\hline
$A$ & $B$ & $A.B$  & $B.A$\\ \hline
$\{x^2,x^3,y,xy\}$ & \{$1,x,x^2y,x^3y\}$ &
\begin{minipage}{\linewidth}
$\begin{split} \lbrace x^2,x^3,y,xy,x^3,1, \\ xy,x^2y,y,x^3y,x^2, \\ x,xy,y,x^3,x^2\rbrace \end{split}$
\end{minipage}
&
\begin{minipage}{\linewidth}
$\begin{split} \lbrace x^2,x^3,y,xy,x^3,1, \\ xy,x^2y,y,x^3y,x^2, \\ x,xy,y,x^3,x^2 \rbrace \end{split}$
\end{minipage} \\ \hline
\end{tabularx}
\end{table}

\end{document}


• there is really too much space at the top of the last two columns. i suggest inserting \abovedisplayskip 2pt\relax in those cells, and also changing split to gathered to center the lines. Commented Nov 30, 2017 at 17:47