![表格中的垂直对齐方式因编译环境而异](https://linux22.com/image/394928/%E8%A1%A8%E6%A0%BC%E4%B8%AD%E7%9A%84%E5%9E%82%E7%9B%B4%E5%AF%B9%E9%BD%90%E6%96%B9%E5%BC%8F%E5%9B%A0%E7%BC%96%E8%AF%91%E7%8E%AF%E5%A2%83%E8%80%8C%E5%BC%82.png)
我(再次)遇到了表格垂直对齐的问题,但显然,代码在我的计算机上编译时有问题,但在 overleaf 上编译时没有问题。例如:
背面版本:
我的电脑上的 TeXStudio 版本:
代码如下:
\documentclass[a4paper, 10pt]{article}
\usepackage{bm}
\usepackage[dvipsnames,table]{xcolor}
\usepackage{cmbright}
\renewcommand{\sfdefault}{cmbr}
\everymath{\displaystyle}
\begin{document}
\begin{tabular}{|
>{\centering\arraybackslash}m{10ex}|
>{\centering\arraybackslash}m{4ex}|
>{\centering\arraybackslash}m{4ex}|
>{\centering\arraybackslash}m{4ex}|
>{\centering\arraybackslash}m{4ex}|
>{\centering\arraybackslash}m{4ex}|
@{}m{0pt}@{}}
\hline
\cellcolor{gray!25}{\textbf{Degrees}}
& \cellcolor{green!15}{$0^{\circ}$}
& \cellcolor{green!15}{$30^{\circ}$}
& \cellcolor{green!15}{$45^{\circ}$}
& \cellcolor{green!15}{$60^{\circ}$}
& \cellcolor{green!15}{$90^{\circ}$}&\\[4ex]
\hline
\cellcolor{gray!25}{\textbf{Radians}}
& \cellcolor{green!15}{$ 0$}
& \cellcolor{green!15}{ $ \frac{\pi}{6}$}
& \cellcolor{green!15}{$ \frac{\pi}{4}$}
& \cellcolor{green!15}{$ \frac{\pi}{3}$}
& \cellcolor{green!15}{$ \frac{\pi}{2}$}&\\[4ex]
\hline
\cellcolor{blue!15}{\textbf{sin}($\bm{\theta}$)}
& $ 0$
& $ \frac{1}{2}$
& $ \frac{\sqrt 2}{2}$
& $ \frac{\sqrt 3}{2}$
& $ 1$&\\[4ex]
\hline
\cellcolor{blue!15}{\textbf{cos}($\bm{\theta}$)}
& $ 1$
& $ \frac{\sqrt 3}{2}$
& $ \frac{\sqrt 2}{2}$
& $ \frac{1}{2}$
& $ 0$&\\[4ex]
\hline
\cellcolor{blue!15}{\textbf{tan}($\bm{\theta}$)}
& $ 0$
& $ \frac{\sqrt 3}{3}$
& $ 1$
& $ \sqrt 3$
&\cellcolor{black}{}\\[4ex]
\hline
\end{tabular}
\end{document}
我在用着 :
最新的 TeXStudio 版本(TeXstudio 2.12.14(git 2.12.14)使用 Qt 版本 5.8.0)
在 Windows 7 上
pdflatex 作为默认编译器
latex 是在我的计算机上用 MiKTeX 实现的。
有人知道为什么会发生这种情况吗?
谢谢
答案1
实际上,你的代码只需要修改两处就可以正确编译。如果你查看该包的文档第 6 页大批中,作者描述了以下变化:
在 2.4f 版之前,如果最后一列为 m 类型,则由 \ 的可选参数添加的空间会添加到 m 单元格内。结果,该单元格垂直居中,该空间位于其内部,从而产生奇怪的偏移。从 2.4f 版开始,现在在单元格居中后添加此空间。
在所有行的末尾,您都设置了间距4ex
( \\[4ex]
)。我认为这是为了确保所有行的高度相等。但是,我发现确保行高相等的最简单解决方案是将零宽度规则(所需高度)放入所有行中 ( \rule{0pt}{4ex}
)。在我的 MWE 中,我增加了+1\tabcolsep
高度以补偿表中的垂直线(并添加了包计算做数学题)。当然这不是绝对必要的。我将 MWE 中的最后一列重新定义为:
@{}>{\rule{0pt}{4ex+1\tabcolsep}}m{0pt}@{}}
删除了[4ex]
五个位置,并加载计算在第二行
usepackage{bm, calc}
不幸的是,您的 MWE 中有一个错误,您必须更正该错误才能使最后一行正确间隔。您忘记&
在表格的最后一行添加最后一个“&”符号。
&\cellcolor{black}{}
&\\ % Last `&` cell was missing
通过这三处修改,我能够在更新后的本地系统和 Overleaf 上编译您的代码,并获得类似的结果。完整的 MWE:
\documentclass[a4paper, 10pt]{article}
\usepackage{bm, calc} % Added the package `calc`
\usepackage[dvipsnames,table]{xcolor}
\usepackage{cmbright}
\renewcommand{\sfdefault}{cmbr}
\everymath{\displaystyle}
\begin{document}
\begin{tabular}{|
>{\centering\arraybackslash}m{10ex}|
>{\centering\arraybackslash}m{4ex}|
>{\centering\arraybackslash}m{4ex}|
>{\centering\arraybackslash}m{4ex}|
>{\centering\arraybackslash}m{4ex}|
>{\centering\arraybackslash}m{4ex}|
@{}>{\rule{0pt}{4ex+1\tabcolsep}}m{0pt}@{}} % Zero width rule
\hline
\cellcolor{gray!25}{\textbf{Degrees}}
& \cellcolor{green!15}{$0^{\circ}$}
& \cellcolor{green!15}{$30^{\circ}$}
& \cellcolor{green!15}{$45^{\circ}$}
& \cellcolor{green!15}{$60^{\circ}$}
& \cellcolor{green!15}{$90^{\circ}$}&\\
\hline
\cellcolor{gray!25}{\textbf{Radians}}
& \cellcolor{green!15}{$0$}
& \cellcolor{green!15}{$\frac{\pi}{6}$}
& \cellcolor{green!15}{$\frac{\pi}{4}$}
& \cellcolor{green!15}{$\frac{\pi}{3}$}
& \cellcolor{green!15}{$\frac{\pi}{2}$}&\\
\hline
\cellcolor{blue!15}{\textbf{sin}($\bm{\theta}$)}
& $0$
& $\frac{1}{2}$
& $\frac{\sqrt 2}{2}$
& $\frac{\sqrt 3}{2}$
& $1$&\\
\hline
\cellcolor{blue!15}{\textbf{cos}($\bm{\theta}$)}
& $1$
& $\frac{\sqrt 3}{2}$
& $\frac{\sqrt 2}{2}$
& $\frac{1}{2}$
& $0$&\\
\hline
\cellcolor{blue!15}{\textbf{tan}($\bm{\theta}$)}
& $0$
& $\frac{\sqrt 3}{3}$
& $1$
& $\sqrt 3$
&\cellcolor{black}{}
&\\ % Last `&` cell was missing
\hline
\end{tabular}
\end{document}
背面结果:
MikTEx 2.9 更新的结果:
我删除了一些后面的空格,$
以便单元格看起来居中。
作为奖励,我附上了一个优化的 MWE 代码:
\documentclass[a4paper, 10pt]{article}
\usepackage{bm, cmbright, calc}
\usepackage[dvipsnames,table]{xcolor}
\renewcommand{\sfdefault}{cmbr}
%\everymath{\displaystyle} % Nicer without displaystyle?
\setlength\extrarowheight{1pt} % Move cell content down
\newcolumntype{C}{>{\centering\arraybackslash$}m{4ex}<{$}}
\newcolumntype{F}{>{\bfseries\centering\arraybackslash\columncolor{blue!15}}m{10ex}}
\newcolumntype{L}{@{}>{\rule{0pt}{4ex+1\tabcolsep}}m{0pt}@{}} % To ensure equal row height
% Shortcuts
\newcommand*{\gray}{\cellcolor{gray!25}}
\newcommand*{\black}{\cellcolor{black}}
\newcommand*{\green}{\cellcolor{green!15}}
\arrayrulecolor{gray!15} % Light gray table rules
\begin{document}
\begin{tabular}{|F|*{5}{C|}L}
\firsthline%
\gray{}Degrees % Gray cell over blue column, \rowcolor not possible to use
& \green\kern0.4em0^{\circ}
& \green30^{\circ}
& \green45^{\circ}
& \green60^{\circ}
& \green90^{\circ}
& \\
\hline%
\gray{}Radians
& \green 0
& \green\frac{\pi}{6}
& \green\frac{\pi}{4}
& \green\frac{\pi}{3}
& \green\frac{\pi}{2}
&\\
\hline%
sin($\bm{\theta}$)
& 0
& \frac{1}{2}
& \frac{\sqrt 2}{2}
& \frac{\sqrt 3}{2}
& 1
&\\
\hline%
cos($\bm{\theta}$)
& 1
& \frac{\sqrt 3}{2}
& \frac{\sqrt 2}{2}
& \frac{1}{2}
& 0
&\\
\hline%
tan($\bm{\theta}$)
& 0
& \frac{\sqrt 3}{3}
& 1
& \sqrt 3
& \black
& \\
\hline%
\end{tabular}
\end{document}
以及卡路里-版本:
\documentclass[a4paper, 10pt]{article}
\usepackage{bm, cmbright, calc, cals}
\usepackage[dvipsnames,table]{xcolor}
\renewcommand{\sfdefault}{cmbr}
\everymath{\displaystyle} % Nicer without displaystyle?
\begin{document}
\begin{calstable}
\colwidths{{56pt}{33pt}{33pt}{33pt}{33pt}{33pt}} % 6 colums
\makeatletter
\def\cals@framers@width{0.8pt}
\def\cals@framecs@width{0.8pt}
\cals@setpadding{Ag}
\cals@setcellprevdepth{Al}
\def\cals@cs@width{0.4pt}
\def\cals@rs@width{0.4pt}
% R1
\brow
\def\cals@bgcolor{gray!15}
\alignC\cell{\vfil\textbf{Degrees}}
\def\cals@bgcolor{green!15}
\alignC\cell{\vfil $\kern0.4em0^{\circ}$}
\alignC\cell{\vfil $30^{\circ}$}
\alignC\cell{\vfil $45^{\circ}$}
\alignC\cell{\vfil $60^{\circ}$}
\alignC\cell{\vfil $90^{\circ}$}
\ht\cals@current@row=33pt
\erow
% R2
\brow
\def\cals@bgcolor{gray!15}
\alignC\cell{\vfil \textbf{Radians}}
\def\cals@bgcolor{green!15}
\alignC\cell{\vfil $0$}
\alignC\cell{\vfil $\frac{\pi}{6}$}
\alignC\cell{\vfil $\frac{\pi}{4}$}
\alignC\cell{\vfil $\frac{\pi}{3}$}
\alignC\cell{\vfil $\frac{\pi}{2}$}
\ht\cals@current@row=33pt
\erow
% R3
\brow
\def\cals@bgcolor{blue!15}
\alignC\cell{\vfil \textbf{sin}($\bm{\theta}$)}
\def\cals@bgcolor{}
\alignC\cell{\vfil $0$}
\alignC\cell{\vfil $\frac{1}{2}$}
\alignC\cell{\vfil $\frac{\sqrt 2}{2}$}
\alignC\cell{\vfil $\frac{\sqrt 3}{2}$}
\alignC\cell{\vfil $1$}
\ht\cals@current@row=33pt
\erow
% R4
\brow
\def\cals@bgcolor{blue!15}
\alignC\cell{\vfil \textbf{cos}($\bm{\theta}$)}
\def\cals@bgcolor{}
\alignC\cell{\vfil $1$}
\alignC\cell{\vfil $\frac{\sqrt 3}{2}$}
\alignC\cell{\vfil $\frac{\sqrt 2}{2}$}
\alignC\cell{\vfil $\frac{1}{2}$}
\alignC\cell{\vfil $0$}
\ht\cals@current@row=33pt
\erow
% R5
\brow
\def\cals@bgcolor{blue!15}
\alignC\cell{\vfil \textbf{tan}($\bm{\theta}$)}
\def\cals@bgcolor{}
\alignC\cell{\vfil $0$}
\alignC\cell{\vfil $\frac{\sqrt 3}{3}$}
\alignC\cell{\vfil $1$}
\alignC\cell{\vfil $\sqrt 3$}
\def\cals@bgcolor{black}
\alignC\cell{}
\ht\cals@current@row=33pt
\erow\makeatother
\end{calstable}
\end{document}
答案2
看看以下解决方案在您的 PC 和 Overleaf 上是否产生相同的结果:
\documentclass[tikz]{standalone}
\usetikzlibrary{matrix}
\usepackage{cmbright}
\renewcommand{\sfdefault}{cmbr}
\usepackage{bm}
\usepackage{siunitx}
\usepackage{amsmath}
\begin{document}
\begin{tikzpicture}
\matrix [matrix of math nodes,
nodes={draw, minimum height=6.4ex, minimum width=2.5em,
anchor=center},
column sep=-\pgflinewidth,
row sep=-\pgflinewidth,
column 1/.style = {nodes={fill=blue!15, minimum width=5em}},
row 1/.style = {nodes={preaction={fill=green!15}}},
row 2/.style = {nodes={preaction={fill=green!15}}}
]
{
|[fill=gray!25]| \textbf{Degrees}
& \SI{ 0}{\degree} & \SI{30}{\degree}
& \SI{45}{\degree} & \SI{60}{\degree}
& \SI{90}{\degree} \\
|[fill=gray!25]| \textbf{Radians}
& 0 & \dfrac{\pi}{6}
& \dfrac{\pi}{4} & \dfrac{\pi}{3}
& \dfrac{\pi}{2} \\
\textbf{sin}(\bm{\theta})
& 0 & \dfrac{1}{2}
& \dfrac{\sqrt{2}}{2} & \dfrac{\sqrt{3}}{2}
& 1 \\
\textbf{cos}(\bm{\theta})
& 1 & \dfrac{\sqrt{3}}{2}
& \dfrac{\sqrt{2}}{2} & \dfrac{1}{2}
& 0 \\
\textbf{tan}(\bm{\theta})
& 0 & \dfrac{\sqrt{3}}{3}
& 1 & \sqrt{3}
& |[fill=black]| \\
};
\end{tikzpicture}
\end{document}