3

I have the following table :

problematic table

It uses \rowcolor, \multicolumn, \multirow but it also has a wide white spacing between the cases.

I need the title "Cobertura del transporte en el municipio de Monterrey" to be properly centered, respecting the 2 cases width, the padding and the space between the left, right and the cases below it.

I also need to combine the two green cases rounded by the red ellipse and containing the "91.4%" value. These two cases would then become one (cf. the \multirow attempt below) green case spanning between the two rows and respecting the height and padding.

I've been trying to get my table right with this method, only it does not seem to apply to my particular example because of the white spacing.

The code for this table is the following :

\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{color}
\usepackage{amsmath, amsfonts, amssymb}
\usepackage{mdframed}
\usepackage{multirow}
\usepackage{tikz,pgfplots}
\usepackage{colortbl}
\usetikzlibrary{shapes}

%------Column types--------------
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

%------Space between cases-------
\makeatletter
\newcommand{\thickhline}{%
    \noalign {\ifnum 0=`}\fi \hrule height 2pt
    \futurelet \reserved@a \@xhline
}
\newcolumntype{+}{@{\hskip\tabcolsep\vrule width 2pt\hskip\tabcolsep}}
\makeatother

%-------Colors--------------------
\definecolor{table1}{RGB}{255,129,54}
\definecolor{table2}{RGB}{41,148,178}
\definecolor{table3}{RGB}{255,129,54}
\definecolor{table4}{RGB}{220,220,77}
\definecolor{table5}{RGB}{58,58,58}

\begin{document}

\begin{table}[!ht]
\centering
\color{white}\sffamily
\begin{tabular}{L{3cm}+C{3cm}+C{3cm}+C{3cm}}

    \rowcolor{table5}
    Otra cosa & \multicolumn{2}{c}{Cobertura del transporte en el municipio de Monterrey} & Otra cosa\\

    \thickhline
    \rowcolor{table4}
    - & \cellcolor{table3}\textcolor{table5}{Primer Cuadro} & \multirow{2}{*}{\textcolor{table5}{91.4\%}} & -\\

    \thickhline
    \rowcolor{table4}
    - & \cellcolor{table3}\textcolor{table5}{Centro (Poniente)} &  & -\\

    \thickhline
    \rowcolor{table4}
    - & \cellcolor{table3}\textcolor{table5}{Oeste} & \textcolor{table5}{90.1\%} & -\\
    \hline

\end{tabular}
\end{table}

\end{document}

Note : I obviously tried replacing the line

\multicolumn{2}{c}{Cobertura del transporte en el municipio de Monterrey}

by this one :

\multicolumn{2}{C{6cm}}{Cobertura del transporte en el municipio de Monterrey}

but it did not provide anything good :

enter image description here

Does anyone has a proper solution for this issue?

0

3 Answers 3

4

I simplified your code: thick \hlines are already defined by the makecell package. It also defines thick \clines, unfortunately it didn't work in this context, for some reason, so I had to use a workaround with \hhline. The second title is an argument to the \makecell command, that allows for line breaks inside cells.

The trick to make appear the multirow contents is to write it in the second row, with a negative value (-2) for the number of rows. I used the \multirowcell from the makecell package since it has a slighly simpler syntax than \multirow.

I took the liberty to have column heads in boldface, as I think white characters on a coloured background are not very readable. This is obtained very simply using the \thead command and setting \theadfont to \bfseries in the preamble. In addition I set the first column qualifier to C instead of L as I don't why it should be different from the last column (but of course you may have reasons to stick to L for the real table).

Finally I removed unnecessary packages for the table.

Here is the code:

 \documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\usepackage{amsmath, amsfonts, amssymb}
\usepackage{multirow}
\usepackage{colortbl}

%------Column types--------------
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

%------Space between cases-------
\newcommand\thickhline{\Xhline{2pt}}
%-------Colors--------------------
\definecolor{table1}{RGB}{255,129,54}
\definecolor{table2}{RGB}{41,148,178}
\definecolor{table3}{RGB}{255,129,54}
\definecolor{table4}{RGB}{220,220,77}
\definecolor{table5}{RGB}{58,58,58}

\newcommand*\thickvrule[1][2pt]{\vrule width 2pt}
\usepackage{makecell}
\renewcommand\theadfont{\bfseries}

\usepackage{cellspace}
\setlength\cellspacetoplimit{5pt}
\setlength\cellspacebottomlimit{5pt}
\addparagraphcolumntypes{m}
\usepackage{hhline}
\setlength\doublerulesep{1.2pt}

\begin{document}

\begin{table}[!ht]
\centering
\color{white}\sffamily
\begin{tabular}{C{3cm}!{\thickvrule} S{C{3cm}}!{\thickvrule}C{3cm}!{\thickvrule}C{3cm}}
    \rowcolor{table5}
    \thead{Otra cosa} & \multicolumn{2}{Sc!{\thickvrule}}{ \thead{Cobertura del transporte en\\el municipio de Monterrey}} & \thead{Otra cosa} \\
    \thickhline
  \rowcolor{table4}
    -- & \cellcolor{table3}\textcolor{table5}{Primer Cuadro} && -- \\
 \hhline{>{\arrayrulecolor{white}}=|=|~|=}
    \rowcolor{table4}
    -- & \cellcolor{table3}\textcolor{table5}{Centro (Poniente)} & \multirowcell{-2}{\makebox[3cm]{\textcolor{table5}{91.4\%}}} & -- \\
    \thickhline
    \rowcolor{table4}
   -- & \cellcolor{table3}\textcolor{table5}{Oeste} & \textcolor{table5}{90.1\%} & -- \\
    \hline
\end{tabular}
\end{table}

\end{document} 

enter image description here

5
  • Thanks! Your code is very nice. There's only one little issue : the white spacing is not respected for the \multicolumn case "Cobertura del transporte en el municipio de Monterrey"...
    – Meclassic
    Commented Sep 8, 2014 at 2:53
  • You managed to get combine the two cells -- impressive. For the spacing issue, just look on my solution -- you may grab it;-) It's ok if the OP accepts your answer
    – user31729
    Commented Sep 8, 2014 at 8:22
  • @Christian Hupfer: Yes, it's a good solution that would spare me loading makecell. However makecell also lets me have a common formatting for column heads (e.g. boldface, which would look better for black background) and easily define thick hlines.
    – Bernard
    Commented Sep 8, 2014 at 9:23
  • @jrojasqu: I'm not sure I understand well: you want to keep the white column under Monterrey that we see in your first figure?
    – Bernard
    Commented Sep 8, 2014 at 10:54
  • @Bernard Well, sorry the delay... In fact I was talking about the white spacing at the right and left of the "Cobertura...Monterrey" case. But then I now see that your code is flawless! Thanks.
    – Meclassic
    Commented Sep 8, 2014 at 14:39
4

The OP forgot the \tabcolsep width to the left and the right of in the relevant \multicolumn command. It can be reduced to 0pt using the @{} statement. The inner two \tabcolsep widths must be added to the width of 6cm.

\multicolumn{2}{@{}C{6cm+2\tabcolsep+2pt}@{}}{Cobertura del transporte en el municipio de Monterrey}

However, there is an additional \vrule command, having a with of 2pt, this must be added to.

I would suggest to introduce a \newlength{\myvrulewidth} having the value 2pt, to simplify the maintenance of the code.


\documentclass[11pt]{article}
\usepackage{calc}
\usepackage[utf8]{inputenc}
\usepackage{color}
\usepackage{amsmath, amsfonts, amssymb}
\usepackage{mdframed}
\usepackage{multirow}
\usepackage{tikz,pgfplots}
\usepackage{colortbl}
\usetikzlibrary{shapes}

%------Column types--------------
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

%------Space between cases-------
\makeatletter
\newcommand{\thickhline}{%
    \noalign {\ifnum 0=`}\fi \hrule height 2pt
    \futurelet \reserved@a \@xhline
}
\newcolumntype{+}{@{\hskip\tabcolsep\vrule width 2pt\hskip\tabcolsep}}
\makeatother

%-------Colors--------------------
\definecolor{table1}{RGB}{255,129,54}
\definecolor{table2}{RGB}{41,148,178}
\definecolor{table3}{RGB}{255,129,54}
\definecolor{table4}{RGB}{220,220,77}
\definecolor{table5}{RGB}{58,58,58}

\begin{document}

\begin{table}[!ht]
\centering
\color{white}\sffamily
\begin{tabular}{L{3cm}+C{3cm}+C{3cm}+C{3cm}}

    \rowcolor{table5}
    Otra cosa & \multicolumn{2}{@{}C{6cm+2\tabcolsep+2pt}@{}}{Cobertura del transporte en el municipio de Monterrey} & Otra cosa\\

    \thickhline
    \rowcolor{table4}
    - & \cellcolor{table3}\textcolor{table5}{Primer Cuadro} & \multirow{-1}{*}{\textcolor{table5}{91.4\%}} & -\\

    \thickhline
    \rowcolor{table4}
    - & \cellcolor{table3}\textcolor{table5}{Centro (Poniente)} &  & -\\

    \thickhline
    \rowcolor{table4}
    - & \cellcolor{table3}\textcolor{table5}{Oeste} & \textcolor{table5}{90.1\%} & -\\
    \hline

\end{tabular}
\end{table}


\end{document}

The question/solution is somehow connected to Unwanted padding in tabular columns when cells contain multiple rows

Edit Thanks to the comment by Harish Kumar, the shifted line in the 3rd row could be corrected

enter image description here

9
  • @HarishKumar: I was working on the main issue first ... but thanks -- however, it should rather read -1, shouldn't it?
    – user31729
    Commented Sep 8, 2014 at 0:12
  • @HarishKumar: Yes, but what's the use of having 91.4% hanging between two table rows (as would be the consequence of using -2? It's dubious ;-)
    – user31729
    Commented Sep 8, 2014 at 0:21
  • @HarishKumar: Thanks ;-) By the way: Congrats to your > 100k rep ;-)
    – user31729
    Commented Sep 8, 2014 at 0:27
  • @ChristianHupfer Thanks! But how do I get the "91.4%" value properly centered (by combining/mixing the two green cases into one) vertically? I may be missing the something in your answer, only I just don't get it...
    – Meclassic
    Commented Sep 8, 2014 at 0:49
  • @jrojasqu: That's something I do not understand correctly. You want to shift down the 91.4% down, and the two green cases in the 3rd column, 2nd and 3rd. row being combined?
    – user31729
    Commented Sep 8, 2014 at 0:52
0

You can do that table easily with {NiceTabular} of nicematrix (≥ 5.18 of 2021-07-15).

\documentclass[11pt]{article}
\usepackage{xcolor}
\usepackage{nicematrix}

\begin{document}

\begin{table}
\centering
\sffamily
\begin{NiceTabular}
   [
     rules={color=white,width=2pt},
     hvlines-except-borders,
     columns-width=3cm,
     cell-space-limits=5pt
   ]
   {cccc}
\CodeBefore
  \rowcolor[RGB]{220,220,77}{2-4}
  \columncolor[RGB]{255,129,54}{2}
  \rowcolor[RGB]{58,58,58}{1}
\Body
\RowStyle{\bfseries\color{white}}
Otra cosa & \Block[C]{1-2}{Cobertura del transporte en\\el municipio de Monterrey} &&Otra cosa \\ 
-- & Primer Cuadro     & \Block{2-1}{91.4\%} & -- \\ 
-- & Centro (Poniente) &                     & -- \\ 
-- & Oeste             & 90.1\%              & -- \\ 
\end{NiceTabular}
\end{table}

\end{document} 

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code

You must log in to answer this question.

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