如何在表中添加列?

如何在表中添加列?

这是我的文件:

\documentclass[a4paper]{article}
\usepackage{multirow, tabularx}

\begin{document}
    \begin{table}[!ht]
        \renewcommand\arraystretch{1.5}
        \newcommand\mcl[1]{\multicolumn{3}{l|}{#1}}
        \caption{Tabella di Cockburn}

        \begin{tabularx}{\linewidth}{|l|>{\hsize=0.4\hsize}X|
                >{\hsize=1.8\hsize}X|
                >{\hsize=0.8\hsize}X|}
            \hline 
            Use Case \#N    & \mcl{Login}                               \\ 
            \hline
            Goal in Context & \mcl{Per identificarsi}                   \\
            \hline 
            Preconditions   & \mcl{L'utente deve avere già un account}  \\
            \hline 
            Success End Conditions 
            & \mcl{L'utente è stato identificato nel sistema}       \\
            \hline 
            Failed End Conditions 
            &   \multicolumn{3}{>{\hsize=\dimexpr3\hsize+6\tabcolsep+3\arrayrulewidth}X|}{ 
                Lorem ipsum dolor sit amet, consectetur adipiscing elit.} \\
            \hline 
            Primary Actor   & \mcl{Attore principale dello UC}                      \\
            \hline 
            Trigger         & \mcl{Azione principale dell'attore che attiva lo UC}  \\
            \hline 
            Description     & Step  & User Action               & System            \\  \cline{2-4} 
            & 1     & L'utente clicca OK        &                   \\  \cline{2-4} 
            & 2     &                           & Mostra M1         \\  \cline{2-4} 
            & 3     & L'utente clicca Cliccami  &                   \\  \cline{2-4} 
            & 4     &                           & Chiude            \\
            \hline 
            Extensions      & Step  & User Action               & System            \\  \cline{2-4} 
            &   &   &   \\
            \hline
            Subvariations   & Step  & User Action               & System            \\  \cline{2-4} 
            &   &   &   \\
            \hline 
            Notes           &   &   &   \\
            \hline
        \end{tabularx}
    \end{table}
\end{document}

这导致了

在此处输入图片描述

  • 我如何编辑文件,以便在User Action的右侧有另一列可以填充,例如User Action?如下图所示:

在此处输入图片描述

答案1

让我将我的评论改为答案。下面的 MWE 中是与您的 MWE 相比的所有更改,这些更改通过代码中的注释进行了解释。

\documentclass[a4paper]{article}
\usepackage{makecell, tabularx}
\renewcommand\theadfont{\small\bfseries} % for headers in "description" row
\renewcommand\theadgape{}                % removed aditional vertical space around \thead   
\newlength{\pwidth}

\usepackage{lipsum} % for generating dummy test
\begin{document}
    \begin{table}[!ht]
\setcellgapes{2pt}
\makegapedcells                     % for additional vertical space in cells
\setlength\tabcolsep{4pt}           % reduce \tabcolspace from default value 6pt, 
                                    % with this cell content has 4pt more space
\settowidth\pwidth{Preconditions}   % as suggested in my answer 
                                    % on one of your previous question
% shortcut \newcommand\mcl[1]{\multicolumn{3}{l|}{#1}} is desingned
% for spanning three column, but with increasing number of columns
% it should now span four columns:
\newcommand\mcl[1]{\multicolumn{4}{l|}{#1}}
    \caption{Tabella di Cockburn}
    \label{tab:cockburn}    % for referencing this table
\begin{tabularx}{\linewidth}{|>{\raggedright}p{\pwidth}|
% Here is inserted additional column. From provided image has the same width
% as last two column. In definig columns width we bear in mind, that sum of 
% X column had to be integer number equal to number of X columns in table:
        >{\hsize=0.55\hsize}X|
        >{\hsize=1.15\hsize}X|
        >{\hsize=1.15\hsize}X|
        >{\hsize=1.15\hsize}X|}
            \hline
%        1   &   2   &   3   &   4   &   5                   \\
Use Case \#N    & \mcl{Login}                               \\
    \hline
Goal in Context & \mcl{Per identificarsi}                   \\
    \hline
Preconditions   & \mcl{L'utente deve avere già un account}  \\
    \hline
Success End Conditions
                & \mcl{L'utente è stato identificato nel sistema}       \\
    \hline
Failed End Conditions
                &   \multicolumn{4}% % cell now span 4 columns, 6 \tabcolsep, 3 \arrayrulewidth 
                                {>{\hsize=\dimexpr4\hsize+6\tabcolsep+3\arrayrulewidth}X|}%
                                {\lipsum[11]} \\
    \hline
Primary Actor   & \mcl{Attore principale dello UC}                      \\
    \hline
Trigger         & \mcl{Azione principale dell'attore che attiva lo UC}  \\
    \hline
Description     &  \thead{Step $n$} 
                        & \thead{Attore 1} 
                                & \thead{Attore $n$} 
                                        & \thead{SIstema}               \\  \cline{2-5}
                & 1     & Azzione trigger   
                                &       &                               \\  \cline{2-5}
                & 2     &       &       & Mostra M1                     \\  \cline{2-5}
                & \dots & Azione 2          
                                &       &                               \\  \cline{2-5}
                & $n$   &       &       & Chiude                        \\
    \hline
\end{tabularx}
    \end{table}
\end{document}

在此处输入图片描述

答案2

这应该对你有用

\documentclass[a4paper]{article}
\usepackage{multirow, tabularx}

\begin{document}
    \begin{table}[!ht]
        \renewcommand\arraystretch{1.5}
        \newcommand\mcl[1]{\multicolumn{4}{l|}{#1}}
        \caption{Tabella di Cockburn}

        \begin{tabularx}{\linewidth}
            {
                |l|>{\hsize=0.4\hsize}X|
                >{\hsize=1.8\hsize}X|
                >{\hsize=0.8\hsize}X|
                >{\hsize=0.8\hsize}X|
            }
            \hline
            Use Case \#N    & \mcl{Login}                               \\
            \hline
            Goal in Context & \mcl{Per identificarsi}                   \\
            \hline
            Preconditions   & \mcl{L'utente deve avere già un account}  \\
            \hline
            Success End Conditions
            & \mcl{L'utente è stato identificato nel sistema}       \\
            \hline
            Failed End Conditions
            &   \multicolumn{4}{>{\hsize=\dimexpr3\hsize+6\tabcolsep+3\arrayrulewidth}X|}{
                Lorem ipsum dolor sit amet, consectetur adipiscing elit.} \\
            \hline
            Primary Actor   & \mcl{Attore principale dello UC}                      \\
            \hline
            Trigger         & \mcl{Azione principale dell'attore che attiva lo UC}  \\
            \hline
            Description     & Step  & Attore 1 & attore n               & sistema            \\  \cline{2-5}
            & 1     & L'utente clicca OK        &                   & \\  \cline{2-5}
            & 2     &                           & Mostra M1         & \\  \cline{2-5}
            & 3     & L'utente clicca Cliccami  &                   & \\  \cline{2-5}
            & 4     &                           & Chiude            & \\
            \hline
            Extensions      & Step  & User Action               & System            & \\  \cline{2-5}
            &   &   &   & \\
            \hline
            Subvariations   & Step  & User Action               & System            & \\  \cline{2-5}
            &   &   &   & \\
            \hline
            Notes           &   &   &   & \\
            \hline
        \end{tabularx}
    \end{table}
\end{document}

问题是,您\mcl在第 7 行定义了一个命令,该命令旨在作为\multicolumnone 的快捷方式。这没什么问题。但是,在表格后面,\multicolumn被明确使用。因此,您应该将多列参数从 3 更改为 4 两次。问题可能就在这里。

\multicolumn不是一个很长的命令,如果您是 LaTeX 新手,请尝试使用软件包的原生命令,然后再定义(您已定义的)快捷方式。大多数编辑器都提供简单的“查找和替换”功能,可节省您的时间。当您感到足够自信时,请随意使用命令快捷方式 ;)

这是我对您需要的表格的个人解释,带有圆顶额外装饰和一些更少的东西(表格并不复杂,您需要强制调整列的宽度\hsize,您只需要知道哪些列需要拉伸X,哪些不需要,比如第二列和第四列)

\documentclass[a4paper]{article}
\usepackage{multirow, tabularx}

\usepackage{booktabs}

\begin{document}
    \begin{table}[tb]
        \renewcommand\arraystretch{1.5}
        \newcommand\mcl[1]{\multicolumn{4}{l}{#1}}
        \centering
        \caption{Tabella di Cockburn}
        \label{tab:tabella-cockburn}
        \begin{tabularx}{\textwidth}{llXXl}
            \toprule
            \textbf{Use Case \#N}  & \mcl{\textbf{Login}}                                   \\ \toprule
            Goal in Context        & \mcl{Per identificarsi}                                \\ \midrule
            Preconditions          & \mcl{L'utente deve avere già un account}               \\ \midrule
            Success End Conditions & \mcl{L'utente è stato identificato nel sistema}        \\ \midrule
            Failed End Conditions  & \mcl{lorem ipsum dolor sit amet}                       \\ \midrule
            Primary Actor          & \mcl{Attore principale dello UC}                       \\ \midrule
            Trigger                & \mcl{Azione principale dell'attore che attiva lo UC}   \\ \midrule
            \textsc{Description}   & \textbf{Step} & \textbf{Attore 1}        & \textbf{Attore $n$} & \textbf{Sistema} \\ \cline{2-5}
                                   & 1             & L'utente clicca OK       &                     & \\ \cline{2-5}
                                   & 2             &                          & Mostra M1           & \\ \cline{2-5}
                                   & 3             & L'utente clicca Cliccami &                     & \\ \cline{2-5}
                                   & 4             &                          & Chiude              & \\ \midrule
            \textsc{Extensions}    & \textbf{Step} & \textbf{Attore 1}        & \textbf{Attore $n$} & \textbf{Sistema} \\ \cline{2-5}
                                   &               &                          &                     & \\ \midrule
            \textsc{Subvariations} & \textbf{Step} & \textbf{Attore 1}        & \textbf{Attore $n$} & \textbf{Sistema} \\ \cline{2-5}
                                   &               &                          &                     & \\ \midrule
            \textsc{Notes}         & \mcl{one note spanning 4 columns} \\
                                   & note 1        & note 2                   & note 3              & note 4 \\
            \bottomrule
        \end{tabularx}
    \end{table}
\end{document}

在此处输入图片描述

相关内容