1

Hi i'm trying to centre the text in the cell, there's on numbers so it's quite small, also i need to add color for the cells, (1 is green and 6 is red) but because I am using \cline it overwrites the lines.

\documentclass[a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{array,makecell,multirow}
\renewcommand\theadfont{\normalfont\bfseries}

\usepackage[margin=20mm]{geometry}
\newcommand\mc[1]{\multicolumn{2}{c|}{\thead{#1}}}

 \begin{document}
    \begin{table}[h]
    \centering
    \renewcommand{\arraystretch}{1.4}
\begin{tabular}{|>{\bfseries}l| >{\bfseries}p{2cm}|c|c|c|c|}
    \cline{3-6}
\mc{}
        &   \mc{Gravité du Dommage G1}
            &   \mc{Gravité du Dommage G2}                  \\
    \cline{3-6}
\mc{}
        &   \thead{Fréquence F1} & \thead{Fréquence F2}
            &   \thead{Fréquence F1} & \thead{Fréquence F2} \\
    \hline
\multirowcell{5}{Probabilité\\ Occurence O1}
    &   Possibilité Evitement P1
        & 1 & 1 & 2 & 3  \\
    \cline{2-6}
    &   Possibilité Evitement P2
        & 1 & 1 & 2 & 4 \\
    \hline
\multirowcell{5}{Probabilité\\ Occurence O2}
    &   Possibilité Evitement P1
        &1 & 1 & 2 & 4 \\
    \cline{2-6}
    &   Possibilité Evitement P2
        & 1 & 1 & 3 & 5 \\
    \hline
\multirowcell{5}{Probabilité\\ Occurence O3}
    &   Possibilité Evitement P1
        & 1 & 2 & 3 & 5 \\
    \cline{2-6}
    &   Possibilité Evitement P2
        & 1 & 2 & 4 & 6\\
    \hline
\end{tabular}
    \end{table}
 \end{document}

enter image description here

1
  • And what are colors for other numbers (2, 3, 4 and 5)? Are they white?
    – Zarko
    Jan 28, 2016 at 22:09

2 Answers 2

2

Here is a solution. I replaces \line with \hhline and I supposed you want to colour the background of some cells, not the numbers:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[showframe]{geometry}
\usepackage{array}
\usepackage[table, x11names]{xcolor}
\usepackage{array, multirow}
\usepackage{makecell, hhline}
\renewcommand\theadfont{\bfseries}
\renewcommand\theadalign{lc}
\newcommand\whitecell[1]{\cellcolor{white}{#1}}

    \begin{document}

\begin{table*}[!htbp]
  \centering\footnotesize
  \renewcommand{\arraystretch}{1.4}
  \begin{tabular}{|l|l| *{2}{>{\columncolor{DarkSeaGreen3!75}}c|}c|c|}
    \cline{3-6}
    \multicolumn{2}{c|}{} & \multicolumn{2}{c|}{\textbf{Gravité du Dommage G1}} & \multicolumn{2}{c|}{\textbf{Gravité du Dommage G2}}\\
    \cline{3-6}
    \multicolumn{2}{c|}{}&\multicolumn{1}{c|}{\bfseries Fréquence F1} & \multicolumn{1}{c|}{\bfseries Fréquence F2} & \textbf{Fréquence F1} & \textbf{Fréquence F2}\\
    \hline
   \multirowthead{3}{Probabilité \\ Occurrence O1}&\thead[lc]{Possibilité\\ Évitement P1}& 1 & 1 & 2 & 3 \\
    \hhline{|~|*{5}{-|}}
                                   & \thead[lc]{Possibilité \\ Évitement P2}& 1 & 1 & 2 & 4 \\
    \hline
    \multirowthead{3}{Probabilité \\ Occurrence O2}&\thead[lc]{Possibilité\\ Évitement P1}& 1 & 1 & 2 & 4 \\
    \hhline{|~|*{5}{-|}}
                                   & \thead{Possibilité \\ Évitement P2}& 1 & 1 & 3 & 5 \\
    \hline
    \multirowthead{3}{Probabilité \\ Occurrence O3}&\thead[lc]{Possibilité\\ Évitement P1}& 1 & 1 & 3 & 5 \\
    \hhline{|~|*{5}{-|}}
                                   & \thead{Possibilité \\ Évitement P2}& 1 & \whitecell{2} & 4 & \cellcolor{Tomato1!90}{6} \\
    \hline
  \end{tabular}
\end{table*}

\end{document} 

enter image description here

12
  • actually I still get soething wrong, the lines are disappearing, mays I am using a package that overwriting Jan 28, 2016 at 22:54
  • You're welcome. B.t.w., I've corrected some misspellings. Don't forget capital letters can have an accent in French.
    – Bernard
    Jan 28, 2016 at 22:55
  • Which lines disappear?
    – Bernard
    Jan 28, 2016 at 22:55
  • The vertical lines on each side of the 1st green column and 3 horizontal lines in the green column. Jan 28, 2016 at 22:56
  • Are you sure this is not a problem with your pdf viewer? I don't have this phenomenon, as you can see from the image.
    – Bernard
    Jan 28, 2016 at 22:58
2

Temporary solution (until the desired colors wouldn't be known):

\documentclass[a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{array,makecell,multirow}
\renewcommand\theadfont{\normalfont\bfseries}

\usepackage{hhline}
\usepackage[table]{xcolor}% <-- needed for cell coloring


\usepackage[margin=20mm]{geometry}
\newcommand\mc[1]{\multicolumn{2}{c|}{\thead{#1}}}

\begin{document}
    \begin{table}[h]
    \centering
    \renewcommand{\arraystretch}{1.4}
\begin{tabular}{|>{\bfseries}l| 
                 >{\bfseries}m{25mm}|
                 >{\columncolor{green!30}}c|
                 >{\columncolor{green!30}}c|c|c|}
    \cline{3-6}
\mc{}
        &   \mc{Gravité du Dommage G1}
            &   \mc{Gravité du Dommage G2}          \\
    \hhline{~~|-|-|-|-|}
\mc{}
        &   \cellcolor{white}{\thead{Fréquence F1} }
            &   \cellcolor{white}{\thead{Fréquence F2}}
                &   \thead{Fréquence F1} 
                    &   \thead{Fréquence F2}        \\
    \hline
\multirowcell{3}{Probabilité\\ Occurence O1}
    &   Possibilité Evitement P1
        & 1 & 1 & 2 & 3  \\
    \hhline{~|-|-|-|-|-|}
    &   Possibilité Evitement P2
        & 1 & 1 & 2 & 4 \\
    \hline
\multirowcell{3}{Probabilité\\ Occurence O2}
    &   Possibilité Evitement P1
        &1 & 1 & 2 & 4 \\
    \hhline{~|-|-|-|-|-|}
    &   Possibilité Evitement P2
        & 1 & 1 & 3 & 5 \\
    \hline
\multirowcell{3}{Probabilité\\ Occurence O3}
    &   Possibilité Evitement P1
        &   1   &   \cellcolor{white}{2}
                    &   3   &   5 \\
    \hhline{~|-|-|-|-|-|}
    &   Possibilité Evitement P2
        &   1   &   \cellcolor{white}{2}
                    &   4 
                            &   \cellcolor{red}{6}\\
    \hline
\end{tabular}
    \end{table}
 \end{document}

enter image description here

This MWE has problem with \clines in colored cells (known problem), I will manage this when I will know what are colors of other cells.

Edit (1): With help of package hhline are fixed \cline appearance in colored cells/columns. Instead of \cline{2-6} is used\hhline{~|-|-|-|-|-|}`.

If some colors are mising, they can be add on the similar way as it is done in colored cells/columns.

Edit (2): Regarding vertical position of numbers in cells beside solution provided in Bernard solution is another solution:

  • instead >{\bfseries}p{2cm} use >{\bfseries}m{25mm}
  • reduce number of lines, which span multirowcell, from 5 to 3

With this measures (now considered in above MWE) gives very similar result as it is present in Bernard answer.

You must log in to answer this question.

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