# Fixing the alignment of table with equations and images

My desired features for the table alignment are:

• The cell sizes should be auto-adjustable according to the contents.
• Every table cell content should be vertically and horizontally center-aligned.
• The first column's contents (except its header) should be vertically center-aligned but horizontally left-aligned.
• A fair amount of padding should appear between images and table cell borders.
• The text-color and background-color of the header row should be different.
\documentclass{article}
\usepackage{amsmath}
\usepackage{graphicx}
\tabcolsep10pt\fboxrule0pt\fboxsep\tabcolsep
\usepackage{geometry}
\usepackage{array}
\renewcommand{\arraystretch}{1.2}
\usepackage{tabularx}
\newcolumntype{L}[1]{>{\flushleft\arraybackslash}m{#1}}
\renewcommand{\tabularxcolumn}[1]{>{\centering\arraybackslash}m{#1}}

\begin{document}
\begin{center}
\begin{tabularx}{\linewidth}{|L{.15\linewidth}|X|X|X|} \hline
\multicolumn{1}{|c|}{Property} & Microcanonical & Canonical & Grand-canonical  \\ \hline
Contact with environment
& Isolated system, so the energy and number of particles are constant.
& System are in thermal contact, such that it can exchange energy but no particles.
&  System can exchange both energy and particles. \\ \hline
Probability density, $\rho(E)$
&  {$$\begin{cases} \text{C,} & E \text{ to } (E+dE)\\ 0, & \text{otherwise} \end{cases}$$}
here, $C=$ constant
& $Ae^{(F-E)/KT}$
& $Ae^{(\Omega + \mu N - E)/KT}$ \\ \hline
Diagram
& \includegraphics[width=\linewidth, valign=c]{example-image}
& \includegraphics[width=\linewidth, valign=c]{example-image}
& \includegraphics[width=\linewidth, valign=c]{example-image} \\ \hline
\end{tabularx}
\end{center}

\end{document}


Issues that needed to be fixed:

• The first column's contents are not vertically center-aligned
• The second column's cell with the displayed equation is not properly aligned at the center.
• The padding around images is not appearing perfectly.

Use $$...$$ and not $$...$$; \raggedright and not \flushleft. Also valign=C, plus a trick to get some padding at the top.

\documentclass{article}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{geometry}
\usepackage{array}
\usepackage{tabularx}

\newcolumntype{L}[1]{>{\raggedright\arraybackslash}m{#1}}
\renewcommand{\tabularxcolumn}[1]{>{\centering\arraybackslash}m{#1}}

\begin{document}

\begin{center}
\begin{tabularx}{\linewidth}{|L{.15\linewidth}|X|X|X|} \hline
\multicolumn{1}{|c|}{Property} & Microcanonical & Canonical & Grand-canonical \\
\hline
Contact with environment
& Isolated system, so the energy and number of particles are constant.
& System are in thermal contact, such that it can exchange energy but no particles.
& System can exchange both energy and particles. \\
\hline
Probability density, $\rho(E)$
& $$\begin{cases} \text{C,} & E \text{ to } (E+dE)\\ 0, & \text{otherwise} \end{cases}$$
\vspace{1ex}\newline here, $C$ is a constant
& $Ae^{(F-E)/KT}$
& $Ae^{(\Omega + \mu N - E)/KT}$ \\
\hline
Diagram
& \includegraphics[width=\linewidth, valign=C,margin=0pt 0pt 0pt 4pt]{example-image}
& \includegraphics[width=\linewidth, valign=C,margin=0pt 0pt 0pt 4pt]{example-image}
& \includegraphics[width=\linewidth, valign=C,margin=0pt 0pt 0pt 4pt]{example-image} \\
\hline
\end{tabularx}
\end{center}

\end{document}


• you could use \includegraphics[width=\linewidth, valign=C,margin=0pt 0pt 0pt 4pt]{example-image} to give the graphics some padding at the top. Aug 22, 2021 at 13:56
• @UlrikeFischer Thanks! Aug 22, 2021 at 14:42
• Thank you. Can the overall padding be increased a bit? I think it would look nicer if the bracket of the cases environment's not touching the table cell border.
– raf
Aug 23, 2021 at 6:16
• One more thing I forgot to mention in the question, I like to set a different color on the first (header) row. What changes in your code is necessary to obtain different color on header row?
– raf
Aug 23, 2021 at 13:51

With tabularray table and use of the \adjincludegraphics macro from the adjustbox package:

\documentclass{article}
\usepackage{geometry}
\usepackage{amsmath}
\usepackage{tabularray}

\begin{document}
\begin{center}
keepaspectratio,
valign=m}
\begin{tblr}{hlines, vlines,
colspec={X[0.6,l,m] *{3}{X[1,c,m]}},
rowsep=4pt,
}
\SetCell{c} Property
& Microcanonical
& Canonical
& Grand-canonical                                       \\
Contact with environment
&   Isolated system, so the energy and number of particles are constant.
&   System are in thermal contact, such that it can exchange energy but no particles.
&   System can exchange both energy and particles.      \\
Probability density, $\rho(E)$
& \parbox{\linewidth}{$\begin{cases} \text{C,} & E \text{ to } (E+dE)\\ 0, & \text{otherwise} \end{cases}$

\medskip
here, $C$ is a constant}
& $Ae^{(F-E)/KT}$
& $Ae^{(\Omega + \mu N - E)/KT}$ \\
Diagram
\end{tblr}
\end{center}
\end{document}


Edit: The weak point of the above solution is determining the size of the images. To adjust them correctly to the width of the columns, it is necessary to guess the allowed their heights. Determining their widths by column widths unfortunately now is inserted (due to bug in package) a large amount of vertical space above/below them.

With the new version of the package, this bug will be removed and the correct image size can will be possible to determine simply by the width of the columns with the following setting:

    \adjustboxset{width=\linewidth,   % <---
keepaspectratio,
valign=m}


• Thank you so much. Maybe I am being too picky, the 3rd row of Probability density is appearing slightly below from the vertical-center line of the cell. Check the equations in the 3rd and 4th columns too. The issue appears on the 4th row's 1st column Diagram. One more thing I forgot to mention in the question, I like to set a different color on the first (header) row.
– raf
Aug 23, 2021 at 10:32
• @raf, you are right (in all) :-). This should be addressed to package author ..... As work arounf can be insert content of cell with cases in a \parbox. For example: \parbox{\linewidth}{$\begin{cases} \text{C,} & E \text{ to } (E+dE)\\ 0, & \text{otherwise} \end{cases}$ \medskip here, $C$ is a constant} Aug 23, 2021 at 12:24
– raf
Aug 23, 2021 at 13:49
• @raf, done! See edited answer. Aug 23, 2021 at 13:55
• Thank you for the update.
– raf
Aug 23, 2021 at 13:57

With {NiceTabular} of nicematrix.

\documentclass{article}
\usepackage{graphicx}
\usepackage{geometry}
\usepackage{nicematrix}

\begin{document}

\begin{center}
\begin{NiceTabular}{m[l]{.15\linewidth}X[m,c]X[m,c]X[m,c]}[hvlines,cell-space-top-limit=3pt,colortbl-like]
\RowStyle[color=blue]{\centering}\rowcolor{lightgray}
Property & Microcanonical & Canonical & Grand-canonical \\
Contact with environment
& Isolated system, so the energy and number of particles are constant.
& System are in thermal contact, such that it can exchange energy but no particles.
& System can exchange both energy and particles. \\
Probability density, $\rho(E)$
& $$\begin{cases} \text{C,} & E \text{ to } (E+dE)\\ 0, & \text{otherwise} \end{cases}$$
\vspace{1ex}\newline here, $C$ is a constant
& $Ae^{(F-E)/KT}$
& $Ae^{(\Omega + \mu N - E)/KT}$ \\
Diagram
& \includegraphics[width=\linewidth, valign=C]{example-image}
& \includegraphics[width=\linewidth, valign=C]{example-image}
& \includegraphics[width=\linewidth, valign=C]{example-image} \\
\end{NiceTabular}
\end{center}

\end{document}


• What do I need to change in your code to set a different color for the header row's background?
– raf
Aug 23, 2021 at 15:17
• Every cell except the 1st column should be veritally+horizontally center aligned.
– raf
Aug 23, 2021 at 15:18
• @Raf: I have modified my answer. Aug 23, 2021 at 16:31
• Thank you for the update.
– raf
Aug 23, 2021 at 17:12
• @raf: Overleaf has not the latest version of nicematrix. You should upload the latest version of the file nicematrix.sty in the repertory of your Overleaf project. You can find that file on the SVN server of TeXLive: www.tug.org/svn/texlive/trunk/Master/texmf-dist/tex/latex/nicematrix/nicematrix.sty Aug 24, 2021 at 14:51