我正在尝试使用以下代码将一些方程式放置在表中:
\documentclass[a4paper]{article}
\begin{document}
\begin{tabular}{ |p{0.5cm}||p{3.5cm}|p{3.8cm}|p{3.1cm}|p{3.6cm}| }
\hline
$Y_{ \textcolor{red}{l}, \textcolor{blue}{m} }$& \textcolor{blue}{0}& \textcolor{blue}{1}& \textcolor{blue}{2}& \textcolor{blue}{3}\\
\hline
\textcolor{red}{0} & $\frac{1}{2} \sqrt{ \frac{1}{\pi} }$ &- &- & - \\
\hline
\textcolor{red}{1} & $\frac{1}{2} \sqrt{\frac{3}{\pi}} \cos\theta$ & $-\frac{1}{2} \sqrt{\frac{3}{2\pi}} e^{i\phi}\sin\theta$ &- &- \\
\hline
\textcolor{red}{2} & $\frac{1}{4} \sqrt{\frac{5}{\pi}} (3\cos^{2}\theta - 1)$ & $-\frac{1}{2} \sqrt{\frac{15}{2\pi}}e^{i\phi}\sin\theta \cos\theta$ &$\frac{1}{4}\sqrt{\frac{15}{2\pi}}e^{2i\phi}\sin^{2}\theta$ &-\\
\hline
\textcolor{red}{3} & $\frac{1}{4}\sqrt{\frac{7}{\pi}} (5\cos^{4}\theta - 3\cos\theta)$ & $-\frac{1}{8}\sqrt{ \frac{21}{\pi} } e^{2i\phi}\sin\theta(5\cos^{2}\theta - 1)$ & $\frac{1}{4}\sqrt{ \frac{105}{2\pi} } e^{2i\phi} \sin^{2}\theta \cos\theta$ & $-\frac{1}{8}\sqrt{ \frac{35}{\pi} }e^{3i\phi} \sin^{3}\theta$ \\
\hline
\end{tabular}
\end{document}
得到的是:
可以做些什么来制作表格:
- 适合页面吗?
- 看起来更居中吗?
答案1
一种解决方案是使用tabularx
(nccmath
针对其medsize
环境),hhline
用于完美交叉垂直和水平规则,以及cellspace
在带有字母 前缀的说明符的列中单元格顶部和底部的最小填充S
:
\documentclass[a4paper]{article}
\usepackage{geometry}
\usepackage{nccmath}
\usepackage{xcolor}
\usepackage{tabularx, hhline}
\newcolumntype{Y}{>{\centering\arraybackslash$\medsize}S{X}<{\endmedsize$}}
\newcolumntype{Z}{>{\hsize =0.8\hsize\centering\arraybackslash$\medsize}S{X}<{\endmedsize$}}
\newcolumntype{W}{>{\hsize =1.2\hsize\centering\arraybackslash$\medsize}S{X}<{\endmedsize$}}
\usepackage{cellspace}
\setlength{\cellspacetoplimit}{4pt}
\setlength{\cellspacebottomlimit}{4pt}
\addparagraphcolumntypes{X, Y}
\begin{document}
{\setlength{\tabcolsep}{4pt}%
\centering\small\begin{tabularx}{\linewidth}{|l||S{Y}|W|S{Y}|Z|}
\hhline{|-||----|}
$Y_{ \textcolor{red}{l}, \textcolor{blue}{m} }$& \textcolor{blue}{0}& \textcolor{blue}{1}& \textcolor{blue}{2}& \textcolor{blue}{3}\\
\hhline{|-||----|}
\textcolor{red}{0} & \mfrac{1}{2} \sqrt{ \mfrac{1}{\pi} } &- &- & - \\
\hhline{|-||----|}
\textcolor{red}{1} & \mfrac{1}{2} \sqrt{\mfrac{3}{\pi}} \cos\theta & -\mfrac{1}{2} \sqrt{\mfrac{3}{2\pi}} e^{i\phi}\sin\theta &- &- \\
\hhline{|-||----|}
\textcolor{red}{2} & \mfrac{1}{4} \sqrt{\mfrac{5}{\pi}} (3\cos^{2}\theta - 1) & -\mfrac{1}{2} \sqrt{\mfrac{15}{2\pi}}e^{i\phi}\sin\theta \cos\theta &\mfrac{1}{4}\sqrt{\mfrac{15}{2\pi}}e^{2i\phi}\sin^{2}\theta &-\\
\hhline{|-||----|}
\textcolor{red}{3} & \mfrac{1}{4}\sqrt{\mfrac{7}{\pi}} (5\cos^{4}\theta - 3\cos\theta) & -\mfrac{1}{8}\sqrt{ \mfrac{21}{\pi} } e^{2i\phi}\sin\theta(5\cos^{2}\theta - 1) & \mfrac{1}{4}\sqrt{ \mfrac{105}{2\pi} } e^{2i\phi} \sin^{2}\theta \cos\theta & -\mfrac{1}{8}\sqrt{ \mfrac{35}{\pi} }e^{3i\phi} \sin^{3}\theta \\
\hhline{|-||----|}
\end{tabularx}
}
\end{document}
答案2
假设您的文档使用标准边距宽度(约 2.5 厘米),您需要做的就是使用一个array
环境——的内容array
自动处于数学模式,无需使用大量$
符号来启动和结束数学模式——并稍微降低值\arraycolsep
,从默认值 5pt 降低到 4pt;该参数\arraycolsep
控制列间空白的数量。
我还会通过 (a) 删除所有垂直线和三条内部水平线以及 (b) 使用包的规则绘制宏,使表格看起来更加开放booktabs
。最后,我会将单元格内容居中,而不是将它们对齐到左侧单元格边框上。
\documentclass[a4paper]{article}
\usepackage[margin=2.5cm]{geometry} % set the page parameters suitably
\usepackage{xcolor} % for \textcolor macro
\usepackage{booktabs} % for \toprule, \midrule, \bottomrule, and \addlinespace macros
\begin{document}
\begin{center}
\setlength\arraycolsep{4pt} % default: 5pt
$\begin{array}{@{} *{5}{c} @{}}
\toprule
Y_{ \textcolor{red}{l}, \textcolor{blue}{m} } &
\textcolor{blue}{0} & \textcolor{blue}{1} &
\textcolor{blue}{2} & \textcolor{blue}{3} \\
\midrule
\textcolor{red}{0} &
\frac{1}{2} \sqrt{ \frac{1}{\pi} }
&- &- & - \\
\addlinespace
\textcolor{red}{1} &
\frac{1}{2} \sqrt{\frac{3}{\pi}} \cos\theta &
-\frac{1}{2} \sqrt{\frac{3}{2\pi}} e^{i\phi}\sin\theta
&- &- \\
\addlinespace
\textcolor{red}{2} &
\frac{1}{4} \sqrt{\frac{5}{\pi}} (3\cos^{2}\theta - 1) &
-\frac{1}{2} \sqrt{\frac{15}{2\pi}}e^{i\phi}\sin\theta \cos\theta &
\frac{1}{4}\sqrt{\frac{15}{2\pi}}e^{2i\phi}\sin^{2}\theta
&-\\
\addlinespace
\textcolor{red}{3} &
\frac{1}{4}\sqrt{\frac{7}{\pi}} (5\cos^{4}\theta - 3\cos\theta) &
-\frac{1}{8}\sqrt{ \frac{21}{\pi} } e^{2i\phi}\sin\theta(5\cos^{2}\theta - 1) &
\frac{1}{4}\sqrt{ \frac{105}{2\pi} } e^{2i\phi} \sin^{2}\theta \cos\theta &
-\frac{1}{8}\sqrt{ \frac{35}{\pi} }e^{3i\phi} \sin^{3}\theta \\
\bottomrule
\end{array}$
\end{center}
\end{document}