使用 tabularx 自动使表格条目垂直和水平居中

使用 tabularx 自动使表格条目垂直和水平居中

我想知道是否有可能在tabularx环境中自动将表格条目垂直和水平居中。

您可以看到矩阵没有居中: 输出

我想要调整的代码是:

\begin{table}[h]
\renewcommand{\arraystretch}{1.1}
\centering{\caption{Test Table}
    \begin{tabularx}{\textwidth}{|X||c|c|c|c|c|}
        \hline          
        Holder1 & \multicolumn{2}{c|}{Holder2}& \multicolumn{2}{c|}{Holder3} & Holder4 \\
        %   \cline{2-5} 
        & $\boldsymbol{X}_1$    & $\boldsymbol{X}_1$ & $\boldsymbol{X}_2$ & {$\boldsymbol{X}_2$} & {Holder4 additional}\\
        \cline{1-6}
        abcv abcv abcv abcv abcv abcv ac sf abcv abcv & $\begin{bmatrix} W_{11}  \end{bmatrix}$   & $\begin{bmatrix} D_{11}  \end{bmatrix}$    & $\begin{bmatrix} K_{12}  \end{bmatrix}$    & 0  & 22\\
        \hline
        abcv abcv abcv abcv abcv abcv ac sf abcv abcv & $\begin{bmatrix} DD_{11} \\ Kl_{21}  \end{bmatrix}$      & $\begin{bmatrix} D_{11} \\ D_{21}  \end{bmatrix}$     & $\begin{bmatrix} X_{12} \\ S_{22}  \end{bmatrix}$    & 0  & 1\\
        \hline
        \text{text text text} & $\begin{bmatrix} G_{11} & G_{41} \\ G_{21} & G_{51} \\ W_{31} & O_{61} \end{bmatrix}$      & $\begin{bmatrix} G_{11} \\ G_{21} \\ G_{31}  \end{bmatrix}$   & $\begin{bmatrix} F_{12} \\ F_{12} \\ X_{12}  \end{bmatrix}$   & 0  & 3\\
        \hline  
    \end{tabularx}
    \label{}}
\end{table} 

答案1

将列类型重新定义X为。我用和m{#1}添加了一些垂直填充,并在包中为标题和表格之间留出了合适的间距。cellspaceextrarowheightcaption

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{tabularx, caption}
\usepackage{cellspace}
\setlength\cellspacetoplimit{4pt}
\setlength\cellspacebottomlimit{4pt}
\renewcommand{\tabularxcolumn}[1]{>{\centering}m{#1}}

\begin{document}

\begin{table}[h]
  \caption{Test Table}
  \setlength\extrarowheight{2pt}
  \begin{tabularx}{\textwidth}{|X||*{5}{Sc|}}
    \hline
    Holder1 & \multicolumn{2}{c|}{Holder2}& \multicolumn{2}{c|}{Holder3} & Holder4 \\
    \cline{2-5}
                                                         & $\boldsymbol{X}₁$ & $\boldsymbol{X}₁$ & $\boldsymbol{X}₂$ & {$\boldsymbol{X}₂$} & {Holder4 additional} \\
    \cline{1-6}
    \strut abcv abcv abcv abcv abcv abcv ac sf abcv abcv & $\begin{bmatrix} W_{11} \end{bmatrix}$ & $\begin{bmatrix} D_{11} \end{bmatrix}$ & $\begin{bmatrix} K_{12} \end{bmatrix}$ & 0 & 22 \\
    \hline
    abcv abcv abcv abcv abcv abcv ac sf abcv abcv & $\begin{bmatrix} DD_{11} \\ Kl_{21} \end{bmatrix}$ & $\begin{bmatrix} D_{11} \\ D_{21} \end{bmatrix}$ & $\begin{bmatrix} X_{12} \\ S_{22} \end{bmatrix}$ & 0 & 1 \\
    \hline
    \text{text text text} & $\begin{bmatrix} G_{11} & G_{41} \\ G_{21} & G_{51} \\ W_{31} & O_{61} \end{bmatrix}$ & $\begin{bmatrix} G_{11} \\ G_{21} \\ G_{31} \end{bmatrix}$ & $\begin{bmatrix} F_{12} \\ F_{12} \\ X_{12} \end{bmatrix}$ & 0 & 3 \\
    \hline
  \end{tabularx}
  \label{}
\end{table}

\end{document} 

在此处输入图片描述

答案2

\documentclass[a4paper,11pt,ngerman]{scrartcl}
\usepackage{amsmath}
\usepackage{tabularx}
\renewcommand\tabularxcolumn[1]{m{#1}}
\begin{document}

\renewcommand{\arraystretch}{1.7}
\begin{tabularx}{\textwidth}{|X||c|c|c|c|c|}        \hline          
    Holder1 & \multicolumn{2}{c|}{Holder2}& \multicolumn{2}{c|}{Holder3} & Holder4 \\
    %   \cline{2-5} 
    & $\boldsymbol{X}_1$    & $\boldsymbol{X}_1$ & $\boldsymbol{X}_2$ & 
    {$\boldsymbol{X}_2$} & {Holder4 additional}\\
    \cline{1-6}
    abcv abcv abcv abcv abcv abcv ac sf abcv abcv & $\begin{bmatrix} W_{11}  
    \end{bmatrix}$   & $\begin{bmatrix} D_{11}  \end{bmatrix}$    & $\begin{bmatrix} 
    K_{12}  \end{bmatrix}$    & 0  & 22\\
    \hline
    abcv abcv abcv abcv abcv abcv ac sf abcv abcv & $\begin{bmatrix} DD_{11} \\ 
    Kl_{21}  \end{bmatrix}$      & $\begin{bmatrix} D_{11} \\ D_{21}  
    \end{bmatrix}$     & $\begin{bmatrix} X_{12} \\ S_{22}  \end{bmatrix}$    & 0  & 
    1\\
    \hline
    \strut\text{text text text} & $\begin{bmatrix} G_{11} & G_{41} \\ G_{21} & G_{51} 
    \\ 
    W_{31} & O_{61} \end{bmatrix}$      & $\begin{bmatrix} G_{11} \\ G_{21} \\ 
    G_{31}  \end{bmatrix}$   & $\begin{bmatrix} F_{12} \\ F_{12} \\ X_{12}  
    \end{bmatrix}$   & 0  & 3\\
    \hline  
\end{tabularx}
\end{document}

在此处输入图片描述

不过,我不会使用所有的垂直线。

相关内容