为什么使用数学堆叠命令时“tabularx”中的文本没有垂直居中?

为什么使用数学堆叠命令时“tabularx”中的文本没有垂直居中?

当我尝试使用具有堆叠功能的数学环境(例如\Vectorstackfromstackengine包)时,文本将不再垂直居中在单元格中。相反,它会向上移动,就像它被挤到顶部一样。以下是一个例子

在此处输入图片描述

更奇怪的是,禁用该命令\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

出现此问题的原因是您的RL列的类型为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}

在此处输入图片描述

相关内容