如何正确格式化具有不同列宽和居中文本的 tabularx

如何正确格式化具有不同列宽和居中文本的 tabularx

我面临的问题是,如何tabularx正确格式化我的表格。表格应具有以下格式,且文本居中:

通缉

我的代码:

\begin{table}[H]
    \begin{tabularx}{\linewidth}{X|p{3cm}|p{3cm}}
        \textbf{Text} & \textbf{First Number (that is a longer text)} & \textbf{Second number (indeed also a long text)}\\
        \hline \\[-0.5ex]
        This is a long text with no sense but that might contain a linebreak & 3 & 3\\[1.5ex]
    \end{tabularx}
    \caption{mytable}
\end{table}

因此,我尝试了各种方法,但结果发现格式化对我来说比我之前想象的要难。当将文本居中时,似乎tabularx没有浮动列宽。我该怎么做?

我最接近的是以下代码:

\begin{table}[H]
    \begin{tabularx}{\linewidth}{c|X|X}
        \textbf{Text} & \textbf{First Number (that is a longer text)} & \textbf{Second number (indeed also a long text)}\\
        \hline \\[-0.5ex]
        This is a long text with no sense but that might contain a linebreak & 3 & 3\\[1.5ex]
    \end{tabularx}
    \caption{mytable - v2}
\end{table}

\begin{table}[H]
    \begin{tabularx}{\linewidth}{X|X|X}
        \textbf{Text} & \textbf{First Number (that is a longer text)} & \textbf{Second number (indeed also a long text)}\\
        \hline \\[-0.5ex]
        This is a long text with no sense but that might contain a linebreak & 3 & 3\\[1.5ex]
    \end{tabularx}
    \caption{mytable - v3}
\end{table}

结果是:

在此处输入图片描述

答案1

这就是你想要的东西吗?

\documentclass[12pt,a4paper,twoside,openright]{book}
\usepackage{geometry}
\usepackage{ tabularx, caption, float}
\renewcommand{\tabularxcolumn}[1]{>{\centering\arraybackslash}m{#1}}

\begin{document}

\begin{table}[H]
\setlength{\extrarowheight}{2pt}
    \centering
    \begin{tabularx}{\linewidth}{>{\hsize=1.5\hsize}X|>{\hsize=0.75\hsize}X|>{\hsize=0.75\hsize}X}
        \textbf{Text} & \textbf{First Number (that is a longer text)} & \textbf{Second number (indeed also a long text)} \\
        \hline
        This is a long text with no sense but that might contain one or several linebreaks & 3 & 3 %\\
    \end{tabularx}
    \caption{mytable}
\end{table}

\end{document} 

在此处输入图片描述

相关内容