3

I need to create a table like this:

enter image description here

  • table with two columns and width equal to width of the page; border around whole table;
  • first row spanning two columns; with background color, bold font and typeset as paragraph, with proper line wrapping of long text;
  • second row with short label in first column, with additional left padding on the left; second column empty;
  • a few rows listing some name-value pairs; item name in first column (same additional left padding as in label cell); item value in second column formatted as "xxx,xx zł" aligned to the right but with additional right padding;
  • last row similar to the first, but without background color.

My best try so far does not handle first and last row properly, resulting in too wide rows. Is there some way to find the combined width of all columns and use it \multicolumn spec for those two rows?

Is there maybe some way to achieve this layout without using tabular environment?

\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 Answer 1

4

You're setting the p{...} column too wide for the page. You need to take into account the width of the rules and the column separation space.

\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}

Also, note the placement of \noindent. Without it, you'll have a paragraph indentation before your table.

To handle the additional space you want to add in front of the middle lines of the table, I would suggest adding a new column. Your table then would be formatted as:

\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}

The @{} in the column declarations removes the intercolumn spacing that would otherwise be placed there. Also notice that the \multicolumn command is now formatted to span 4 (not 2) columns.

As suggested by @GonzaloMedina , you can completely avoid the use of the \hspace*{...} commands by fixing the width of those dummy columns as here:

\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}
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .