如何制作具有固定列和跨越多列的单元格的表格?

如何制作具有固定列和跨越多列的单元格的表格?

我想做一个这样的表格:

在此处输入图片描述

我找到了如何修复列宽的方法:如何创建带有文本“raggedright/centered/raggedleft”的固定宽度表格列?如何创建具有固定列宽的表格在多列内将固定宽度的文本居中

但是,当我尝试将多个单元格与多列合并在一起时,列宽会失败:

\documentclass[10pt]{article}
\usepackage{booktabs}
\usepackage{tabularx}

\begin{document}
\begin{table}[!ht]
  \centering
  \begin{tabularx}{\linewidth}{l|X|X|X|X}
    \toprule
    Activity & \multicolumn{2}{|c|}{1st year} & \multicolumn{2}{c}{2nd year} \\
     & 1st semester & 2nd semester & 1st semester & 2nd semester  \\
    \midrule
    I. &  Perform some experiment, do some calculations, write a report and present the results in a conference.
 & & \\
  II. &  \multicolumn{3}{|c|}{Perform some experiment, do some calculations, write a report and present the results in a conference.}
  & \\
  III. &  & \multicolumn{3}{|c|}{Perform some experiment, do some calculations, write a report and present the results in a conference.} \\
    \bottomrule
  \end{tabularx}
\end{table}
\end{document}

在此处输入图片描述

我怎样才能制作一个具有固定列和跨越多列的单元格的表格?

答案1

在此处输入图片描述

在的帮助下\newcolumntype{L}{>{\raggedright\arraybackslash}X}定义了一个新的列类型L。此列类型基于类型X,但其内容左对齐而不是两端对齐。(另请参阅:如何创建带有文本“raggedright/centered/raggedleft”的固定宽度表格列?

为了获得\multicolumn单元格的正确宽度,我使用了>{\hsize=\dimexpr3\hsize+4\tabcolsep+2\arrayrulewidth}L。由于\multicolumn单元格跨越 3 L(或X)列,我们可以从以下开始>{\hsize=3\hsize}L(另请参阅:使用 tabularx 的表格布局(列宽:50%|25%|25%))。然后,我们在此基础上添加4\tabcolsep\tabcolsep是在表格单元格内文本两侧添加的少量水平空白)以及2\arrayrulewidth\arrayrulewidth是表格中垂直线的宽度。)

\documentclass{article}
\usepackage{tabularx}
\newcolumntype{L}{>{\raggedright\arraybackslash}X}
\begin{document}
\begin{table}
  \begin{tabularx}{\linewidth}{|l|L|L|L|L|}
    \hline
    Activity & \multicolumn{2}{c|}{1st year} & \multicolumn{2}{c|}{2nd year} \\ \cline{2-5}
     & 1st semester & 2nd semester & 1st semester & 2nd semester  \\
    \hline
    I. &  Perform some experiment, do some calculations, write a report and present the results in a conference.
 & & &\\ \hline
  II. &  \multicolumn{3}{>{\hsize=\dimexpr3\hsize+4\tabcolsep+2\arrayrulewidth}L|}{Perform some experiment, do some calculations, write a report and present the results in a conference.}
  & \\ \hline
  III. &  & \multicolumn{3}{>{\hsize=\dimexpr3\hsize+4\tabcolsep+2\arrayrulewidth}L|}{Perform some experiment, do some calculations, write a report and present the results in a conference.} \\
    \hline
  \end{tabularx}
\end{table}
\end{document}

以下是基于pgfgantt包的完全不同的方法:

在此处输入图片描述

\documentclass{article}
\usepackage{pgfgantt}


\begin{document}

\noindent
     \begin{ganttchart}[
     y unit title=0.5cm,
     y unit chart=1.25cm,
     x unit =1.75cm,
     vgrid,hgrid,
     title height=1,
     title label font=\footnotesize,
     bar height=0.5,
     group right shift=0,
     group top shift=0.7,
     bar label node/.style={text width=4.75cm,
                            align=right,
                            anchor=east,
                            font=\footnotesize\raggedleft}
    ]{1}{4}

  \gantttitle{1st year}{2}
  \gantttitle{2nd year}{2}         \\
  \gantttitle{1st semester}{1}
  \gantttitle{2st semester}{1}
  \gantttitle{1st semester}{1}
  \gantttitle{2st semester}{1}\\
  \ganttbar{Perform some experiment, do some calculations, write a report and present the results in a conference.}{1}{1}\\
  \ganttbar{Perform some experiment, do some calculations, write a report and present the results in a conference.}{1}{3}\\
    \ganttbar{Perform some experiment, do some calculations, write a report and present the results in a conference.}{2}{4}
\end{ganttchart}

\end{document}

相关内容