如何创建一个单元格跨越所有列并且文本正确换行的表格?

如何创建一个单元格跨越所有列并且文本正确换行的表格?

我需要创建一个这样的表格:

在此处输入图片描述

  • 表格有两列,宽度与页面宽度相等;整个表格周围有边框;
  • 第一行跨越两列;使用背景颜色,字体加粗,排版为段落,长文本适当换行;
  • 第二行第一列有短标签,左侧有额外的左填充;第二列为空;
  • 几行列出了一些名称-值对;第一列中的项目名称(与标签单元格中的额外左填充相同);第二列中的项目值格式为“xxx,xx zł”,右对齐,但有额外的右填充;
  • 最后一行与第一行类似,但没有背景颜色。

到目前为止,我尽了最大努力,但还是无法正确处理第一行和最后一行,导致行太宽。有没有办法找到所有列的总宽度,并对这两行使用 \multicolumn spec?

是否有某种方法可以在不使用表格环境的情况下实现这种布局?

\documentclass[10pt]{article}
\usepackage{tabularx}
\usepackage[table]{xcolor}

\begin{document}
\begin{tabularx}{\textwidth}{ | X r | }
    \hline
    \rowcolor[gray]{0.6}
    \multicolumn{2}{|>{\bfseries}p{\textwidth}|}{First row of the table - some long text in bold font with background color. This needs to wrap properly. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc venenatis nunc dui, sed pellentesque ante laoreet nec.}\\
    \hspace{2mm}Label of the list:       &             \\
    \hspace{2mm}first item of the list   &   377,26 zl\hspace{2mm} \\
    \hspace{2mm}second item of the list  &    56,30 zl\hspace{2mm} \\
    \hspace{2mm}last item of the list    &     0,00 zl\hspace{2mm} \\
    \multicolumn{2}{|>{\bfseries}p{\textwidth}|}{Last row of the table - some text in bold font}\\
    \hline
\end{tabularx}
\end{document}

答案1

您设置的p{...}列对于页面来说太宽了。您需要考虑规则的宽度和列分隔空间。

\documentclass[10pt]{article}
\usepackage{tabularx}
\usepackage[table]{xcolor}
\usepackage{showframe}
\begin{document}
\noindent
\begin{tabularx}{\textwidth}{ | X r | }
    \hline
    \rowcolor[gray]{0.6}
    \multicolumn{2}{|>{\bfseries}p{\dimexpr\textwidth-2\tabcolsep-2\arrayrulewidth}|}{First row of the table - some long text in bold font with background color. This needs to wrap properly. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc venenatis nunc dui, sed pellentesque ante laoreet nec.}\\
    \hspace{2mm}Label of the list:       &             \\
    \hspace{2mm}first item of the list   &   377,26 zl\hspace{2mm} \\
    \hspace{2mm}second item of the list  &    56,30 zl\hspace{2mm} \\
    \hspace{2mm}last item of the list    &     0,00 zl\hspace{2mm} \\
    \multicolumn{2}{|>{\bfseries}p{\dimexpr\textwidth-2\tabcolsep-2\arrayrulewidth}|}{Last row of the table - some text in bold font}\\
    \hline
\end{tabularx}
\end{document}

另外,请注意 的位置\noindent。如果没有它,表格前就会出现段落缩进。

为了处理您想要在表格中间线前面添加的额外空间,我建议添加一个新列。然后您的表格将被格式化为:

\noindent
\begin{tabularx}{\textwidth}{ | c@{} Xr @{}c| }
    \hline
    \rowcolor[gray]{0.6}
    \multicolumn{4}{|>{\bfseries}p{\dimexpr\textwidth-2\tabcolsep-2\arrayrulewidth}|}{First row of the table - some long text in bold font with background color. This needs to wrap properly. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc venenatis nunc dui, sed pellentesque ante laoreet nec.}\\
    \hspace*{2mm} & Label of the list:      &           & \hspace*{2mm} \\
                  & first item of the list  & 377,26 zl &               \\
                  & second item of the list & 56,30 zl  &               \\
                  & last item of the list   & 0,00 zl   &               \\
    \multicolumn{4}{|>{\bfseries}p{\dimexpr\textwidth-2\tabcolsep-2\arrayrulewidth}|}{Last row of the table - some text in bold font}\\
    \hline
\end{tabularx}

列声明中的@{}删除了原本会放置在那里的列间距。还请注意,该\multicolumn命令现在被格式化为跨越 4 列(而不是 2 列)。

\hspace*{...}正如@GonzaloMedina 所建议的,您可以通过固定这些虚拟列的宽度来完全避免使用这些命令,如下所示:

\noindent
\begin{tabularx}{\textwidth}{ | p{2mm}@{} Xr @{}p{2mm} | }
    \hline
    \rowcolor[gray]{0.6}
    \multicolumn{4}{|>{\bfseries}p{\dimexpr\textwidth-2\tabcolsep-2\arrayrulewidth}|}{First row of the table - some long text in bold font with background color. This needs to wrap properly. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc venenatis nunc dui, sed pellentesque ante laoreet nec.}\\
      & Label of the list:      &           & \\
      & first item of the list  & 377,26 zl & \\
      & second item of the list & 56,30 zl  & \\
      & last item of the list   & 0,00 zl   & \\
    \multicolumn{4}{|>{\bfseries}p{\dimexpr\textwidth-2\tabcolsep-2\arrayrulewidth}|}{Last row of the table - some text in bold font}\\
    \hline
\end{tabularx}

相关内容