我对表格对齐的期望特征是:
- 单元格大小应根据内容自动调整。
- 每个表格单元格内容都应垂直和水平居中对齐。
- 第一列的内容(标题除外)应垂直居中对齐、水平左对齐。
- 图像和表格单元格边框之间应该有相当数量的填充。
- 标题行的文字颜色和背景颜色应该不同。
\documentclass{article}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage[export]{adjustbox}
\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}
需要修复的问题:
- 第一列的内容未垂直居中对齐
- 显示方程式的第二列单元格未正确居中对齐。
- 图像周围的填充显示得不太完美。
答案1
使用\(...\)
而非$$...$$
;\raggedright
而非\flushleft
。此外valign=C
,还有一个技巧可以在顶部添加一些填充。
\documentclass{article}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage[export]{adjustbox}
\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}
答案2
使用tabularray
表并使用包\adjincludegraphics
中的宏adjustbox
:
\documentclass{article}
\usepackage{geometry}
\usepackage{amsmath}
\usepackage[export]{adjustbox}
\usepackage{tabularray}
\begin{document}
\begin{center}
\adjustboxset{height=0.15\textwidth, % had to be defined
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
& \adjincludegraphics{example-image-duck}
& \adjincludegraphics{example-image-duck}
& \adjincludegraphics{example-image-duck} \\
\end{tblr}
\end{center}
\end{document}
编辑: 上述解决方案的弱点在于确定图像的大小。为了正确地将它们调整到列的宽度,必须猜测允许的高度。不幸的是,现在通过列宽确定它们的宽度会在其上方/下方插入大量垂直空间(由于软件包中的错误)。
使用新版本的软件包,这个错误将被删除,并且可以通过以下设置根据列的宽度简单地确定正确的图像大小:
\adjustboxset{width=\linewidth, % <---
keepaspectratio,
valign=m}
结果表将与之前相同。更多信息,请参阅这回答。
答案3
与。{NiceTabular}
nicematrix
\documentclass{article}
\usepackage{graphicx}
\usepackage{geometry}
\usepackage[export]{adjustbox}
\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}