7

I'm trying to recreate this table from Word in LaTeX:

enter image description here

I'm getting close, but the "Effectiveness" section is not aligned properly, and the "Probability" section is not fully filled in with color.

enter image description here

Here is the code I have so far, can someone point me in the right direction?

\documentclass[11pt]{article}
\usepackage{multirow}
\usepackage[table]{xcolor}  

\begin{document}
\begin{center}
\begin{table}[!htbp]
\noindent
\begin{tabular}{|c|p{3cm}|p{3cm}|p{3cm}|}
\hline
\multirow{2}{3cm}{\centering \cellcolor{gray!50} Effectiveness of Controls} &    \multicolumn{3}{p{9cm}|}{\centering \cellcolor{gray!50} Probability of Threat Occurrence (Natural or Environmental Threats) or Threat Motivation and Capability (Human Threats)} \\
\cline{2-4}
\rowcolor{gray!50}
& Low & Moderate & High \\
\hline
\rowcolor{white!50}
\cellcolor{gray!50} Low & Moderate & High & High \\
\hline
\rowcolor{white!50}
\cellcolor{gray!50} Moderate & Low & Moderate & High \\
\hline
\rowcolor{white!50}
\cellcolor{gray!50} High & Low & Low & Moderate \\
\hline
\end{tabular}
\caption{Risk Classifications}
\label{tab:xyz}

\end{table}
\end{center}
\end{document}
  • You want \multicolumn{3}{p{\dimexpr 9cm+2\arrayrulewidth+4\tabcolsep\relax}|}{ to span over the rules and cell padding as well as the 3 cells, although I needed to reduce the 3cm and 9cm to make the table fit on a page. – David Carlisle Mar 28 '14 at 15:17
  • Ah, that looks much better. The command is a bit convoluted, I don't expect I would have sorted that out on my own. Thanks! – Jon Buys Mar 28 '14 at 15:26
12

You can align Effectiveness... by

\multirow{2}{\dimexpr0.25\textwidth-2\arrayrulewidth-2\tabcolsep\relax}[-0.5cm]{...

Note [-0.5cm] and adjust it for vertical positioning. I have adjusted the column width such that they fit well within the text width.

\documentclass[11pt,draft]{article}
\usepackage{multirow}
\usepackage[table]{xcolor}
\usepackage{array}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}

\begin{document}
\begin{center}
\begin{table}[!htbp]
\noindent
\begin{tabular}{|c|C{\dimexpr0.25\textwidth-\arrayrulewidth-2\tabcolsep\relax}
           |C{\dimexpr0.25\textwidth-\arrayrulewidth-2\tabcolsep\relax}
           |C{\dimexpr0.25\textwidth-\arrayrulewidth-2\tabcolsep\relax}|}
\hline
\multirow{2}{\dimexpr0.25\textwidth-2\arrayrulewidth-2\tabcolsep\relax}[-0.5cm]{\centering \cellcolor{gray!50} Effectiveness of Controls} &    \multicolumn{3}{p{\dimexpr 0.75\textwidth-\arrayrulewidth-2\tabcolsep\relax}|}{\cellcolor{gray!50}\centering Probability of Threat Occurrence (Natural or Environmental Threats) or Threat Motivation and Capability (Human Threats)} \\
\cline{2-4}
\cellcolor{gray!50}
& \cellcolor{gray!50} Low & \cellcolor{gray!50} Moderate & \cellcolor{gray!50} High \\
\hline
%\rowcolor{white!50}
\cellcolor{gray!50} Low & Moderate & High & High \\
\hline
\rowcolor{white!50}
\cellcolor{gray!50} Moderate & Low & Moderate & High \\
\hline
\rowcolor{white!50}
\cellcolor{gray!50} High & Low & Low & Moderate \\
\hline
\end{tabular}
\caption{Risk Classifications}
\label{tab:xyz}

\end{table}
\end{center}
\end{document}

enter image description here

1

This type of tabulars are very easy to build in cals. As you will see, no white lines in the gray, all rules equally visible:

\documentclass{article}
\usepackage{cals, caption, xcolor}

\let\nc=\nullcell           % CALS Shortcuts for spanning cells
\let\sc=\spancontent


\begin{document}

\begin{table}[!htbp]
\begin{calstable}[c]  % Centred CALS tabular

% Calculate the column width
\colwidths{{\dimexpr(\columnwidth)/4\relax}
            {\dimexpr(\columnwidth)/4\relax}
            {\dimexpr(\columnwidth)/4\relax}
            {\dimexpr(\columnwidth)/4\relax}
    }

% The tabular fills the text area if sum of all columns is 4

% Set up the tabular
\makeatletter
\def\cals@framers@width{0.4pt}   % Outside horizontal frame rules
\def\cals@framecs@width{0,4pt}   % Outside vertical frame rules
\def\cals@bodyrs@width{0.4pt}    % Rule between header and tabular body
\def\cals@cs@width{0.4pt}        % Inside vertical rules
\def\cals@rs@width{0.4pt}        % Inside horizontal rules
\def\cals@bgcolor{}              % To avoid undefined

\def\gray{\ifx\cals@bgcolor\empty     % "Switch" to turn on and off colour
    \def\cals@bgcolor{gray!30}
\else \def\cals@bgcolor{} \fi}

% R1H1     % Just a comment: Row one, header row one
\thead{
\brow
    \gray\alignC\nc{lrt}    % Switch to gray background and centred text, works until switched off
    \nc{ltb}
    \nc{tb}
    \nc{rtb}\sc{\vfil Probability of Threat Occurrence (Natural or Environmental Threats) or Threat Motivation and Capability (Human Threats)}
\erow
%R2H2       % Row two, header row two
\brow
    \nc{lrb}\sc{\vfil Effectiveness of Controls}  % "vfil" centres the cell content vertically
    \cell{\vfil Low}
    \cell{\vfil Moderate}
    \cell{\vfil High}\gray % Always reset colour at end of header 
\erow
%R3B1     % Row three, body row one
\brow
    \gray\cell{Low}\gray   % Switch to gray background for first cell (column 1)
    \cell{\vfil Moderate}
    \cell{\vfil High}
    \cell{\vfil High}
\erow
%R4B2
\brow
    \gray\cell{Moderate}\gray
    \cell{\vfil Low}
    \cell{\vfil Moderate}
    \cell{\vfil High}
\erow
%R5B3
\brow
    \gray\cell{High}\gray
    \cell{\vfil Low}
    \cell{\vfil Low}
    \cell{\vfil Moderate}
\erow
\makeatletter
\end{calstable}\par
\caption{Risk Classifications}
\label{tab:xyz}
\end{table}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.