如何使表格文本不与下一列重叠

如何使表格文本不与下一列重叠

我正在尝试将表格格式化为如下所示:桌子应该是什么样的

这是我为其编写的代码:

\documentclass{article}

%Table formatting
\usepackage[none]{hyphenat}
\usepackage{array}
\newcolumntype{$}{>{\global\let\currentrowstyle\relax}}
\newcolumntype{^}{>{\currentrowstyle}}
\newcommand{\rowstyle}[1]{\gdef\currentrowstyle{#1} #1\ignorespaces}
\usepackage{multirow}
\usepackage{adjustbox}

\begin{table} [ht]
    \centering
    \label{tab:test}
    \begin{adjustbox}{max width=\textwidth}
    \begin{tabular}{$c^c^p{0.9in}^c^c^c^c}
        \hline 
        \rowstyle{\bfseries}ID & Requirement & Description & Inputs & Expected outputs & Pass/Fail & Comments \\
        \hline
        A1.1 & FR1 & \multirow{2}{0.9in}{checking attendants can only park certain vehicles} & 1-3 & Nothing & P & \\ 
                        & \multicolumn{2}{c}{}                                                  & 4-5 & produces error & F & doesn't check input of vehicleType \\
            \hline
        A1.2 & FR2 & \multirow{2}{0.9in}{checking attendants can only park certain vehicles} & 1-3 & Nothing & P & \\
                        & \multicolumn{2}{c}{}                                                  & 4-5 & produces error & F & doesn't check input of vehicleType \\

    \end{tabular}
    \end{adjustbox}
    \caption{Test table}
\end{table}

当前输出导致描述中的文本与下一行重叠。我知道有人问过类似的问题,但我对 LaTeX 还不熟悉,不明白他们的解决方案是如何解决这个问题的。

在此处输入图片描述

答案1

切勿使用adjustboxwith tabless,因为它会产生不一致的字体大小。最好调整字体大小和 的值\tabcolsep

话虽如此,您可以使用多行,不是在第 3 列,而是在第 4-7 列。

为了适应,\textwidth我建议使用tabularx,第 3 列和第 7 列为类型X。在这种情况下,您只需要\multirow对第 4-6 列使用。此外,我加载了几何图形,以获得更合理的边距(如果您不需要边注)。最后,我使用了规则booktabs,在水平线周围添加一些垂直填充:

\documentclass{article}

%Table formatting
\usepackage[none]{hyphenat}
\usepackage{array}
\usepackage{ragged2e}
\newcolumntype{$}{>{\global\let\currentrowstyle\relax}}
\newcolumntype{^}{>{\currentrowstyle}}
\newcommand{\rowstyle}[1]{\gdef\currentrowstyle{#1} #1\ignorespaces}
\usepackage{multirow, makecell, tabularx, booktabs}
\usepackage{adjustbox}
\usepackage{geometry}
\begin{document}


\begin{table} [!htb]
    \centering\renewcommand{\cellalign}{tc}
    \label{tab:test}
    \begin{adjustbox}{max width=\textwidth}
    \begin{tabular}{$c^c^p{0.9in}^c^c^c^c}
        \toprule
        \rowstyle{\bfseries}ID & Requirement & Description & Inputs & Expected outputs & Pass/Fail & Comments \\
        \midrule
        A1.1 & FR1 & \RaggedRight checking attendants can only park certain vehicles & \makecell{1-3\\4-5} & \makecell{Nothing\\produces error} & \makecell{P \\F }& \makecell{\\doesn't check input of vehicleType}\\
        \midrule
        A1.2 & FR2 & \multirow{2}{=}{checking attendants can only park certain vehicles} & 1-3 & Nothing & P & \\
                        & \multicolumn{2}{c}{} & 4-5 & produces error & F & doesn't check input of vehicleType \\[8ex]
\bottomrule
    \end{tabular}
    \end{adjustbox}
    \caption{Test table}
\end{table}

\begin{table} [! htb]
    \centering\footnotesize\renewcommand{\cellalign}{tc}
\setlength{\tabcolsep}{4pt}
    \label{tab:test}
     \begin{tabularx}{\linewidth}{$c^c^X^c^c^c^X}
        \toprule
        \rowstyle{\bfseries}ID & Requirement & Description & Inputs & Expected outputs & Pass/Fail & Comments \\
        \midrule
        A1.1 & FR1 & \RaggedRight checking attendants can only park certain vehicles & \makecell{1-3\\4-5} & \makecell{Nothing\\produces error} & \makecell{P \\F }&\mbox{} \newline doesn't check input of vehicleType \\
            \midrule
        A1.2 & FR2 & \multirow{3}{=}{\RaggedRight checking attendants can only park certain vehicles} & 1-3 & Nothing & P & \\
                        & \multicolumn{2}{c}{} & 4-5 & produces error & F & doesn't check input of vehicleType \\%[8.5ex]
\bottomrule
    \end{tabularx}
    \caption{Test table}
\end{table}

\end{document} 

在此处输入图片描述

相关内容