表格:将两列值对齐到一行

表格:将两列值对齐到一行

我正在写一份很长的文档,直到我写到要用表格写工作计划的地步。现在我想设置每个活动的时间表,以与相应的任务保持一致。

MSE 如下:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}
\usepackage{floatrow}
\usepackage{tabularx}

\begin{document}

\begin{table}[h]
        \caption{Table of activities}
        \label{tblactivity}
        \centering
        \begin{tabular}{lccc}
            \toprule
             \textbf{No.} & \textbf{Activity}   & \textbf{Task(s) to be completed}  &
              \textbf{Timeline}\\
            \midrule
              \textbf{1} &\multicolumn{1}{m{3cm}}{\textbf{\textit{Activity number 1}}} & \multicolumn{1}{m{7cm}}{ \begin{enumerate}
                  \item Some long sentence here, describing task 1 of activity 1
                  \item Another long sentence here, describing task 2 of activity 1
                  \item Yet another long sentence here, describing task 3 of activity 1
                  \item Activity 1 task 4 ...
              \end{enumerate} } & 
                  \\
              \midrule
              \textbf{2} &  \multicolumn{1}{m{3cm}}{\textbf{\textit{Activity number 2}}} & \multicolumn{1}{m{7cm}}{ \begin{enumerate}
                  \item Activity 2 task 1
                  \item Activity 2 task 2
                  \item Activity 2 task 3 
              \end{enumerate} } \\

        \bottomrule
        \end{tabular}
         \hfill
\end{table} 

\end{document}

在此处输入图片描述

对于活动 1 的每个任务 1、2、3 和 4(例如 2 个月、2 个月、1 个月、1 个月),我希望设置截止日期,使时间与任务一致。活动 2 等也是如此...

答案1

将截止日期与任务对齐的最简单方法可能就是不使用enumerate环境,而是将每个任务放在表格中的单独单元格中。这样,您可以轻松地将每个截止日期放在自己的单元格中,并与相应的任务正确对齐。

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}
\usepackage{floatrow}
\usepackage{tabularx}
\usepackage{multirow}

\begin{document}

\begin{table}[h]
        \caption{Table of activities}
        \label{tblactivity}
        \centering
        \begin{tabular}{llp{1mm}p{6cm}p{2cm}}
            \toprule
             \textbf{No.} & \multicolumn{1}{c}{\textbf{Activity}} && \textbf{Task(s) to be completed}  &
              \textbf{Timeline}\\
            \midrule
              \multirow{7}{*}{\textbf{1}} & \multirow{7}{*}{\textbf{\textit{Activity number 1}}}
                & 1. & Some long sentence here, describing task 1 of activity 1 & Deadline 1 \\
                && 2. & Another long sentence here, describing task 2 of activity 1 & Deadline 2 \\
                && 3. & Yet another long sentence here, describing task 3 of activity 1 & Deadline 3 \\
                && 4. & Activity 1 task 4 & Deadline 4 \\
              \midrule
              \multirow{3}{*}{\textbf{2}} & \multirow{3}{*}{\textbf{\textit{Activity number 2}}}
                & 1. & Activity 2 task 1 & Deadline 1 \\
                && 2. & Activity 2 task 2 & Deadline 2 \\
                && 3. & Activity 2 task 3  & Deadline 3 \\
        \bottomrule
        \end{tabular}
         \hfill
\end{table} 

\end{document}

答案2

定义特殊的列类型E˙具有集成enumerateminipage

\documentclass{article}
\usepackage{ragged2e}
\usepackage{booktabs, makecell, multirow, tabularx}
\newcolumntype{E}{ >{\csname @minipagetrue\endcsname% list is in minipage env.
                     \RaggedRight\enumerate}X<{\endenumerate}}  % <---
\newcommand*\mcc[1]{\multicolumn{1}{>{\bfseries}l}{#1}}         % <---
\usepackage{enumitem}

\begin{document}
    \begin{table}
    \setcellgapes{1pt}
    \makegapedcells
\setlist[enumerate]{%
         resume=column, % ensure sequential numbering
         nosep,
         leftmargin=*,
         label=\arabic*.,
         after=\vspace*{-\baselineskip}
                 }
\begin{tabularx}{\linewidth}{@{} c >{\RaggedRight}p{17mm} E l @{}}
    \toprule
\mcc{No.}
    & \mcc{Activity}
        & \mcc{Task(s) to be completed}         & \mcc{Timeline}    \\
    \midrule
1   &   \multirow[t]{4}{=}{Activity number 1}
        & \setcounter{enumi}{0}   %%%% <------- start numbering with 1
          \item Some long sentence here,
                describing task 1 of activity 1 & monday            \\
    &   & \item Another long sentence here,
                describing task 2 of activity 1 & in 2 weeks        \\
    &   & \item Yet another long sentence here,
                describing task 3 of activity 1 & timeline          \\
    &   & \item Activity 1 task 4 \dots         & timeline          \\
    \midrule
2   &   \multirow[t]{2}{=}{Activity number 2}
        & \setcounter{enumi}{0}   %%%% <------- restart numbering with 1
          \item Some long sentence here,
                describing task 1 of activity 1 & monday            \\
    &   & \item Another long sentence here,
                describing task 2 of activity 1 & in 2 weeks        \\
    &   & \item Yet another long sentence here,
                describing task 3 of activity 1 & timeline          \\
    &   & \item Activity 1 task 4 \dots         & timeline          \\
    \bottomrule
\end{tabularx}
    \end{table}
\end{document}

在此处输入图片描述

答案3

这是我的建议enumitem,使用自定义命令来节省输入。我还使用了它tabularx来使表格适合文本宽度。我还添加了另一种布局,左列中的空白较少:

在此处输入图片描述 在此处输入图片描述

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}
\usepackage{tabularx}

\usepackage{enumitem}

\newcommand{\mytabenum}[2][resume=mylist]{\begin{enumerate}[#1, label*=\arabic*.,leftmargin=*,nosep,leftmargin=*,before=\vspace{-0.5\baselineskip},after=\vspace{-1\baselineskip}]\item #2 \end{enumerate}}



\begin{document}


\begin{table}[h]
        \caption{Table of activities}
        \label{tblactivity}
        \centering
\begin{tabularx}{\textwidth}{l>{\raggedright\arraybackslash}p{1.5cm}Xl}
\toprule 
\textbf{No.} & \textbf{Activity}   & \textbf{Task(s) to be completed}  &  \textbf{Timeline}\\
\midrule
1 & Activity number 1 & \mytabenum[series=mylist]{Some long sentence here, describing task 1 of activity 1} & monday \\
 &  & \mytabenum{Another long sentence here, describing task 2 of activity 1} & in 2 weeks\\
 &  & \mytabenum{Yet another long sentence here, describing task 3 of activity 1} & timeline\\
 &  & \mytabenum{Activity 1 task 4 ...} & timeline\\
 \midrule
2 & Activity number 2 & \mytabenum[series=mylist]{Some long sentence here, describing task 1 of activity 1} & monday \\
 &  & \mytabenum{Another long sentence here, describing task 2 of activity 1} & in 2 weeks\\
 &  & \mytabenum{Yet another long sentence here, describing task 3 of activity 1} & timeline\\
 &  & \mytabenum{Activity 1 task 4 ...} & timeline\\
\bottomrule
\end{tabularx}
\end{table}


\begin{table}[h]
        \caption{Table of activities}
        \label{tblactivity}
        \centering
\begin{tabularx}{\textwidth}{p{\tabcolsep}Xl}
\toprule 
   & \textbf{Task(s) to be completed}  &  \textbf{Timeline}\\
\midrule
\multicolumn{3}{l}{1 Activity number 1} \\
  & \mytabenum[series=mylist]{Some long sentence here, describing task 1 of activity 1} & monday \\
  & \mytabenum{Another long sentence here, describing task 2 of activity 1} & in 2 weeks\\
  & \mytabenum{Yet another long sentence here, describing task 3 of activity 1} & timeline\\
  & \mytabenum{Activity 1 task 4 ...} & timeline\\
 \midrule
\multicolumn{3}{l}{2 Activity number 2} \\
  & \mytabenum[series=mylist]{Some long sentence here, describing task 1 of activity 1} & monday \\
  & \mytabenum{Another long sentence here, describing task 2 of activity 1} & in 2 weeks\\
  & \mytabenum{Yet another long sentence here, describing task 3 of activity 1} & timeline\\
  & \mytabenum{Activity 1 task 4 ...} & timeline\\
\bottomrule
\end{tabularx}
\end{table}
\end{document}

相关内容