0

I tried to improve the following table as can be seen in the screenshot below, the output is mostly what I want.

enter image description here

Is there any reason the text is not centered vertically when it spans over multiple rows? I would have expected the result to look more like this

enter image description here

Any other ways to get the same output is also welcome. I feel my solution to get the header and first column in bold is convoluted, even if I like the output. Similarly maybe there exists an easier way to center the text vertically and define a minimal height for the cells?

MWE

\documentclass[10pt,a4paper]{article}
\usepackage[margin=1.3in]{geometry} 
\usepackage{array,booktabs,lipsum}
\usepackage[table]{xcolor}
\usepackage{tabularx}
\renewcommand\tabularxcolumn[1]{m{#1}}
\newcolumntype{C}{ >{\vbox to 5ex\bgroup\vfill\centering\arraybackslash}X<{\egroup} }
\newcolumntype{$}{>{\global\let\currentrowstyle\relax}}
\newcolumntype{^}{>{\currentrowstyle}}
\newcommand{\rowstyle}[1]{\gdef\currentrowstyle{#1}%
  #1\ignorespaces
}

\newcommand{\low}{\cellcolor{green!50}}
\newcommand{\med}{\cellcolor{yellow!50}}
\newcommand{\high}{\cellcolor{red!50}}

\begin{document}
\lipsum[66]
\begin{table}[htbp]
\centering
\scriptsize
\def\arraystretch{1.5}%
\caption{Initial risk matrix}
\begin{tabularx}{\textwidth}{| >{\bfseries}C | ^C | ^C | ^C | ^C | ^C |}
\hline \rowstyle{\bfseries}
Frequency/ Consequence & 1-Very Unlikely & 2-Remote & 3-Occasional & 4-Probable & 5-Frequent\\ 
\hline 4-Catastrophic & Auto-ignition \med & Toxic release \high & \high & \high &\high \\
\hline 3-Critical &\low & \med & \med & \high Refuelling systems & \high \\ 
\hline 2-Major & \low & \low & \med & Fire spread from nearby buses \med &\high \\ 
\hline 1-Minor & \low & \low & \low &\med &\med \\ 
\hline
\end{tabularx} \\
\end{table}
\lipsum[75]
\end{document}
2
  • ?? The text seems to be centred in the screenshot.
    – Bernard
    Commented Apr 24, 2020 at 10:55
  • @Bernard But not "Frequency/ Consequence" it is too high, similarly "refuelling systems" is also too high in the cell, and not properly centered. Commented Apr 24, 2020 at 11:16

1 Answer 1

2

I propose this solution, based an cellspace, which defines a minimal vertical padding at the top and bottom of cells in columns with specifier prefixed with the letter S (or any letter you choose with the option [column= some letter]. I just have to use customised X cells.

\documentclass[10pt,a4paper]{article}
\usepackage[margin=1.3in]{geometry}
\usepackage{array,booktabs,lipsum}
\usepackage[table]{xcolor}
\usepackage{tabularx}
\renewcommand\tabularxcolumn[1]{>{\centering\arraybackslash}m{#1}}%
\newcolumntype{C}{>{\centering\arraybackslash}X}
\newcolumntype{$}{>{\global\let\currentrowstyle\relax}}
\newcolumntype{^}{>{\currentrowstyle}}
\newcommand{\rowstyle}[1]{\gdef\currentrowstyle{#1}%
  #1\ignorespaces
}

\newcommand{\low}{\cellcolor{green!50}}
\newcommand{\med}{\cellcolor{yellow!50}}
\newcommand{\high}{\cellcolor{red!50}}

\usepackage[skip=6pt]{caption}
\usepackage[column=O]{cellspace}
\addparagraphcolumntypes{X}
\setlength{\cellspacetoplimit}{6pt}
\setlength{\cellspacebottomlimit}{6pt}

\begin{document}

\lipsum[66]
\begin{table}[htbp]
\centering
\scriptsize
\def\arraystretch{1.5}%
\caption{Initial risk matrix}
\begin{tabularx}{\textwidth}{| >{\bfseries}O{X} | ^X | ^X | ^X | ^X | ^X |}
\hline \rowstyle{\bfseries}
Frequency/ Consequence & 1-Very Unlikely & 2-Remote & 3-Occasional & 4-Probable & 5-Frequent\\
\hline 4-Catastrophic & Auto-ignition \med & Toxic release \high & \high & \high &\high \\
\hline 3-Critical &\low & \med & \med & \high Refuelling systems & \high \\
\hline 2-Major & \low & \low & \med & Fire spread from nearby buses \med &\high \\
\hline 1-Minor & \low & \low & \low &\med &\med \\
\hline
\end{tabularx} \\
\end{table}
\lipsum[75]

\end{document} 

enter image description here

You must log in to answer this question.

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