tabularx 最后一列垂直居中

tabularx 最后一列垂直居中

如何使tabularx表格的最后一列垂直居中?

我尝试创建一个自定义列,但是,老实说,我真的不知道自己在做什么。

基本上,我希望左右列的内容以相同的方式对齐,无论是垂直对齐还是水平对齐。我需要对y类型列做什么?我想使用\m{}某种类型,但不确定如何操作。

\documentclass[12pt,letterpaper]{article}

\usepackage{tabularx}
\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}
\newcolumntype{Y}{>{\centering\arraybackslash}X}

\usepackage{tikz}

\begin{document}

\begin{tabularx}{17cm}{|C{8cm}|Y|} \hline

\tikz{
\draw (0,0)--(4,0);
\foreach \x in {0,...,4}
 \draw (\x,-0.3)--(\x,0.3);
\foreach \x in {0,...,3} 
 \draw[-latex] (\x+0.1,-0.15)--(\x+0.9,-0.15);
\node at (2,-0.8) {4};
} &
\tikz{
\draw (0,0)--(6,0);
\foreach \x in {0,...,6}
 \draw (\x,-0.3)--(\x,0.3);
\foreach \x in {0,...,5}
 \draw[-latex] (\x+0.1,-0.15)--(\x+0.9,-0.15);
 \node at (3,-0.8) {6};}
\\ \hline
\end{tabularx}

\end{document}

答案1

tabularx使用columns\tabularxcolumn的列类型X。默认值为p{#1}。可以重新定义它以匹配其他m列:

\documentclass[12pt,letterpaper]{article}

\usepackage{tabularx}
\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}
\newcolumntype{Y}{>{\centering\arraybackslash}X}

\usepackage{tikz}

\begin{document}

\renewcommand*{\tabularxcolumn}[1]{m{#1}}

\begin{tabularx}{17cm}{|C{8cm}|Y|} \hline
\tikz{
\draw (0,0)--(4,0);
\foreach \x in {0,...,4}
 \draw (\x,-0.3)--(\x,0.3);
\foreach \x in {0,...,3}
 \draw[-latex] (\x+0.1,-0.15)--(\x+0.9,-0.15);
\node at (2,-0.8) {4};
} &
\tikz{
\draw (0,0)--(6,0);
\foreach \x in {0,...,6}
 \draw (\x,-0.3)--(\x,0.3);
\foreach \x in {0,...,5}
 \draw[-latex] (\x+0.1,-0.15)--(\x+0.9,-0.15);
 \node at (3,-0.8) {6};}
\\ \hline
\end{tabularx}

\end{document}

结果

相关内容