如何让一列跨越表格宽度的一半

如何让一列跨越表格宽度的一半

你好,我正在制作一个有多列多行的表格。但我希望下一行有两列,宽度正好是表格宽度的一半。这意味着,如果我在表格中添加文本时表格宽度发生变化,这些列的宽度将自动调整。

这是我的表格代码:

\begin{sidewaystable*}[!htbp]
  \centering\small
  \renewcommand{\arraystretch}{1.4}
  \begin{tabular}{|c|c|c|c|c|c|c|c|}
  \hline
    \multicolumn{8}{|c|}{\textbf{ANALYSE DE RISQUE}}\\
  \hline
    \multicolumn{8}{|c|}{\textbf{Poste de travail ou machine:}}\\
  \hline
    \multicolumn{4}{|c|}{\textbf{Risque}} & \textbf{Indice} &  \textbf{Situation Dangereuse} &  \textbf{Conséquence} &  \textbf{Mesure Corrective}\\
   \hline
%%%%%%%%%%%%%%%%%%% Part to duplicate if needed %%%%%%%%%%%
   \multicolumn{4}{|c|}{} & \multirow{ 2}{*}{} &  \multirow{ 2}{*}{} &  \multirow{ 2}{*}{} &   \multirow{ 2}{*}{}\\
   \cline{1-4}
   G: & F: & O: & P: & & & & \\
   \hline
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
%%%%%%%%%%%%%%%%%%% Part to duplicate if needed %%%%%%%%%%%
   \multicolumn{4}{|c|}{} & \multirow{ 2}{*}{} &  \multirow{ 2}{*}{} &  \multirow{ 2}{*}{} &   \multirow{ 2}{*}{}\\
   \cline{1-4}
   G: & F: & O: & P: & & & & \\
   \hline
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
%%%%%%%%%%%%%%%%%%% Part to duplicate if needed %%%%%%%%%%%
   \multicolumn{4}{|c|}{} & \multirow{ 2}{*}{} &  \multirow{ 2}{*}{} &  \multirow{ 2}{*}{} &   \multirow{ 2}{*}{}\\
   \cline{1-4}
   G: & F: & O: & P: & & & & \\
   \hline
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
%%%%%%%%%%%%%%%%%%% Part to duplicate if needed %%%%%%%%%%%
   \multicolumn{4}{|c|}{} & \multirow{ 2}{*}{} &  \multirow{ 2}{*}{} &  \multirow{ 2}{*}{} &   \multirow{ 2}{*}{}\\
   \cline{1-4}
   G: & F: & O: & P: & & & & \\
   \hline
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
  \end{tabular}
\end{sidewaystable*}

它看起来是这样的: 在此处输入图片描述

编辑:这就是我想要添加的内容。 在此处输入图片描述

答案1

要解决您的问题,您需要知道表格的最终宽度。由于事先不知道,因此很难将最后一行分成两个相等的部分。

在“反复试验”的基础上,我成功获得以下结果:

在此处输入图片描述

对于这张图片,我在表格代码的末尾添加了:

%%%%%%%%%%%%%%%%%%% Part with two columns (nested) %%%%%%%%%%
   \multicolumn{8}{@{}c@{}}{
        \begin{tabular}{|*{2}{p{68mm}|}}% <-- width of columns is guessed
            &   \\
        \end{tabular}
                        }       \\
   \hline
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  \end{tabular}
\end{sidewaystable*}    
    \end{document}

升级: 通过测量表格的实际宽度可以避免猜测表格的宽度。为此,我添加了新的长度tablewidth和新的保存框tablebox。程序的思路如下:- 将原始表格存储在\tablebox - 测量保存框的宽度 - 打印保存框 - 添加表格(即:新表格的最后一行),其中一列的宽度计算为0.5\tablewidth-2\tabcolsep-9\arrayrulewidth

完整代码为:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{rotating}
\usepackage{multirow,tabularx}
\usepackage{calc}

\newsavebox\tablebox
\newlength\tablewidth
    \begin{document}
\begin{sidewaystable*}
  \centering\small
  \renewcommand{\arraystretch}{1.4}

\savebox\tablebox{
  \begin{tabular}{|*{8}{c|}}
  \hline
    \multicolumn{8}{|c|}{\textbf{ANALYSE DE RISQUE}}\\
  \hline
    \multicolumn{8}{|c|}{\textbf{Poste de travail ou machine:}}\\
  \hline
    \multicolumn{4}{|c|}{\textbf{Risque}} & \textbf{Indice} &  \textbf{Situation Dangereuse} &  \textbf{Conséquence} &  \textbf{Mesure Corrective}\\
   \hline
%%%%%%%%%%%%%%%%%%% Part to duplicate if needed %%%%%%%%%%%
   \multicolumn{4}{|c|}{} & \multirow{ 2}{*}{} &  \multirow{ 2}{*}{} &  \multirow{ 2}{*}{} &   \multirow{ 2}{*}{}\\
   \cline{1-4}
   G: & F: & O: & P: & & & & \\
   \hline
%%%%%%%%%%%%%%%%%%% Part to duplicate if needed %%%%%%%%%%%
   \multicolumn{4}{|c|}{} & \multirow{ 2}{*}{} &  \multirow{ 2}{*}{} &  \multirow{ 2}{*}{} &   \multirow{ 2}{*}{}\\
   \cline{1-4}
   G: & F: & O: & P: & & & & \\
   \hline
%%%%%%%%%%%%%%%%%%% Part to duplicate if needed %%%%%%%%%%%
   \multicolumn{4}{|c|}{} & \multirow{ 2}{*}{} &  \multirow{ 2}{*}{} &  \multirow{ 2}{*}{} &   \multirow{ 2}{*}{}\\
   \cline{1-4}
   G: & F: & O: & P: & & & & \\
   \hline
%%%%%%%%%%%%%%%%%%% Part to duplicate if needed %%%%%%%%%%%
   \multicolumn{4}{|c|}{} & \multirow{ 2}{*}{} &  \multirow{ 2}{*}{} &  \multirow{ 2}{*}{} &   \multirow{ 2}{*}{}\\
   \cline{1-4}
   G: & F: & O: & P: & & & & \\
   \hline
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  \end{tabular}
}
\usebox\tablebox

%%%%%%%%%%%%%%%%%%% Part with two columns (nested) %%%%%%%%%%
\settowidth\tablewidth{\usebox\tablebox}
    \vspace{-3\arrayrulewidth}
        \begin{tabular}{|*{2}{p{0.5\tablewidth-2\tabcolsep-9\arrayrulewidth}|}}
            &   \\
        \hline
        \end{tabular}
\end{sidewaystable*}    
    \end{document}

得到的表格与通过猜测得到的表格形式相同,但是现在不再需要猜测,最后一行的列宽将根据其之前的表格的实际宽度计算。

答案2

我建议您使用tabularx环境而不是tabular现有 8 列表格的环境,将总宽度设置为类似 的值0.8\textwidth。然后,只需添加第二个tabularx环境,其宽度与第一个环境相同,但仅包含两列而不是八列。

在此处输入图片描述

\documentclass{article}
\usepackage{multirow,rotating,tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}

\begin{document}
\begin{sidewaystable*}[!htbp]
  \centering\small
  \renewcommand{\arraystretch}{1.4}
  \begin{tabularx}{0.8\textwidth}{| *{4}{C|} *{4}{c|} }
  \hline
    \multicolumn{8}{|c|}{\textbf{ANALYSE DE RISQUE}}\\
  \hline
    \multicolumn{8}{|c|}{\textbf{Poste de travail ou machine:}}\\
  \hline
    \multicolumn{4}{|c|}{\textbf{Risque}} & \textbf{Indice} &  \textbf{Situation Dangereuse} &  \textbf{Conséquence} &  \textbf{Mesure Corrective}\\
   \hline
%%%%%%%%%%%%%%%%%%% Part to duplicate if needed %%%%%%%%%%%
   \multicolumn{4}{|c|}{} & \multirow{ 2}{*}{} &  \multirow{ 2}{*}{} &  \multirow{ 2}{*}{} &   \multirow{ 2}{*}{}\\
   \cline{1-4}
   G: & F: & O: & P: & & & & \\
   \hline
%%%%%%%%%%%%%%%%%%% Part to duplicate if needed %%%%%%%%%%%
   \multicolumn{4}{|c|}{} & \multirow{ 2}{*}{} &  \multirow{ 2}{*}{} &  \multirow{ 2}{*}{} &   \multirow{ 2}{*}{}\\
   \cline{1-4}
   G: & F: & O: & P: & & & & \\
   \hline
%%%%%%%%%%%%%%%%%%% Part to duplicate if needed %%%%%%%%%%%
   \multicolumn{4}{|c|}{} & \multirow{ 2}{*}{} &  \multirow{ 2}{*}{} &  \multirow{ 2}{*}{} &   \multirow{ 2}{*}{}\\
   \cline{1-4}
   G: & F: & O: & P: & & & & \\
   \hline
%%%%%%%%%%%%%%%%%%% Part to duplicate if needed %%%%%%%%%%%
   \multicolumn{4}{|c|}{} & \multirow{ 2}{*}{} &  \multirow{ 2}{*}{} &  \multirow{ 2}{*}{} &   \multirow{ 2}{*}{}\\
   \cline{1-4}
   G: & F: & O: & P: & & & & \\
   \hline
  \end{tabularx}

  % snug up the second 'tabularx' environment to the one above it
  \vspace{-3\arrayrulewidth}
  % now insert a second `tabularx` environment
  \begin{tabularx}{0.8\textwidth}{|C|C|}
  & \\
  & \\
  \hline
  \end{tabularx}
\end{sidewaystable*}
\end{document}

相关内容