当我尝试使用具有堆叠功能的数学环境(例如\Vectorstack
fromstackengine
包)时,文本将不再垂直居中在单元格中。相反,它会向上移动,就像它被挤到顶部一样。以下是一个例子
更奇怪的是,禁用该命令\renewcommand{\tabularxcolumn}[1]{>{\Centering}m{#1}}
可以解决这个问题。讽刺的是,该命令是用来启用垂直居中的,那么为什么它的存在会阻止垂直居中呢?
我的源代码
\documentclass{report}
\usepackage{amsmath,array,booktabs,multirow}
\usepackage{ragged2e}
\usepackage{tabularx}
\usepackage{booktabs}
\newcolumntype{C}{ >{\arraybackslash \Centering}X
}
\newcolumntype{R}{ >{\arraybackslash \RaggedLeft}X
}
\newcolumntype{L}{ >{\arraybackslash \RaggedRight}X
}
\renewcommand{\tabularxcolumn}[1]{>{\Centering}m{#1}}
\usepackage{stackengine}
\newcommand{\stackanchormath}[3][\stackgapSdefaultl]{
\stackanchor[#1]{$#2$}{$#3$}
}
\newcommand{\stackgapSdefault}{
\setstackgap{S}{\stackgapSdefaultl}
}
\newlength{\stackgapSdefaultl}
\setlength{\stackgapSdefaultl}{1.06mm}
\setstackgap{S}{\stackgapSdefaultl}
\begin{document}
\begin{tabularx}{0.7\columnwidth}{>{\hsize=0.75\hsize}R |L}
\toprule
%
\texttt{$\backslash$Vectorstack} & Test \Vectorstack[c]{ {e = mc^{2}} {y = a x + b} } text
\\
\hline
\texttt{$\backslash$stackanchormath} & Test \stackanchormath{e=mc^{2}}{y = a x + b} text
\\
\hline
\texttt{aligned} & Test $\begin{aligned} e &= mc^{2} \\ y &= a x + b \end{aligned}$ text
%
\\
\bottomrule
\end{tabularx}
\end{document}
答案1
出现此问题的原因是您的R
和L
列的类型为X
,该类型设置为自动换行段落模式。因此,特定堆叠机制(例如\Vectorstack
)设置的任何垂直对齐方式都会被列的垂直段落对齐方式所取代X
。
为了进行比较,我展示了tabularx
两次:第一次按原样显示(R L
最后一个单元格扩展以显示已激活的自动换行功能),然后将列类型更改为(非换行)r l
列。如您在第二种情况下所见,基线对齐得以保留。
\documentclass{report}
\usepackage{amsmath,array,booktabs,multirow}
\usepackage{ragged2e}
\usepackage{tabularx}
\usepackage{booktabs}
\newcolumntype{C}{ >{\arraybackslash \Centering}X
}
\newcolumntype{R}{ >{\arraybackslash \RaggedLeft}X
}
\newcolumntype{L}{ >{\arraybackslash \RaggedRight}X
}
\renewcommand{\tabularxcolumn}[1]{>{\Centering}m{#1}}
\usepackage{stackengine}
\newcommand{\stackanchormath}[3][\stackgapSdefaultl]{
\stackanchor[#1]{$#2$}{$#3$}
}
\newcommand{\stackgapSdefault}{
\setstackgap{S}{\stackgapSdefaultl}
}
\newlength{\stackgapSdefaultl}
\setlength{\stackgapSdefaultl}{1.06mm}
\setstackgap{S}{\stackgapSdefaultl}
\begin{document}
\begin{tabularx}{0.7\columnwidth}{R L}
\toprule
%
\texttt{$\backslash$Vectorstack} & Test \Vectorstack[c]{ {e = mc^{2}} {y = a x + b} } text
\\
\hline
\texttt{$\backslash$stackanchormath} & Test \stackanchormath{e=mc^{2}}{y = a x + b} text
\\
\hline
\texttt{aligned} & Test $\begin{aligned} e &= mc^{2} \\ y &= a x + b \end{aligned}$ text is in paragraph form and auto-wraps
%
\\
\bottomrule
\end{tabularx}
\begin{tabularx}{0.7\columnwidth}{r l}
\toprule
%
\texttt{$\backslash$Vectorstack} & Test \Vectorstack[c]{ {e = mc^{2}} {y = a x + b} } text
\\
\hline
\texttt{$\backslash$stackanchormath} & Test \stackanchormath{e=mc^{2}}{y = a x + b} text
\\
\hline
\texttt{aligned} & Test $\begin{aligned} e &= mc^{2} \\ y &= a x + b \end{aligned}$ text
%
\\
\bottomrule
\end{tabularx}
\end{document}
请注意,如果将适当的内容应用到左侧以匹配右侧,则X
可以恢复与列的对齐:\vphantom
\documentclass{report}
\usepackage{amsmath,array,booktabs,multirow}
\usepackage{ragged2e}
\usepackage{tabularx}
\usepackage{booktabs}
\newcolumntype{C}{ >{\arraybackslash \Centering}X
}
\newcolumntype{R}{ >{\arraybackslash \RaggedLeft}X
}
\newcolumntype{L}{ >{\arraybackslash \RaggedRight}X
}
\renewcommand{\tabularxcolumn}[1]{>{\Centering}m{#1}}
\usepackage{stackengine}
\newcommand{\stackanchormath}[3][\stackgapSdefaultl]{
\stackanchor[#1]{$#2$}{$#3$}
}
\newcommand{\stackgapSdefault}{
\setstackgap{S}{\stackgapSdefaultl}
}
\newlength{\stackgapSdefaultl}
\setlength{\stackgapSdefaultl}{1.06mm}
\setstackgap{S}{\stackgapSdefaultl}
\begin{document}
\begin{tabularx}{0.7\columnwidth}{R L}
\toprule
%
\texttt{$\backslash$Vectorstack}\vphantom{\Vectorstack[c]{ {e = mc^{2}} {y = a x + b} }}
& Test \Vectorstack[c]{ {e = mc^{2}} {y = a x + b} } text
\\
\hline
\texttt{$\backslash$stackanchormath} \vphantom{\stackanchormath{e=mc^{2}}{y = a x + b}}
& Test \stackanchormath{e=mc^{2}}{y = a x + b} text
\\
\hline
\texttt{aligned} \vphantom{$\begin{aligned} e &= mc^{2} \\ y &= a x + b \end{aligned}$}
& Test $\begin{aligned} e &= mc^{2} \\ y &= a x + b \end{aligned}$ text
%
\\
\bottomrule
\end{tabularx}
\end{document}