# Headings with coloured boxes

I am needing to write a risk assessment report, the headings within the report are risks that have been discovered, next to each of the headings/risks I'm needing to use a key or coloured boxes to highlight the severity of the risk in question, how am I able to do this in LaTeX, a brief mock up example from word would look like the attached picture.

I'm using the hyperref package to create the menu structure in the PDF menu.

I tried the following from the comments below.

{\section{Heading} \color{red} \rule{0.2in}{0.2in}}


Whilst this gives me a box, I'd of liked to of had the border if possible, and also for it to be on the same line as the heading, with the above code the box is on the next line down with the body text for that heading.

The following code gives errors, but otherwise does what I am after albeit no border around the box..

\section{Heading \color{red} \rule{0.2in}{0.2in}}

• Please show us what you have tried so far! – Kurt Feb 4 at 22:37
• Well, is the box always of the same width or does the width of the box indicate the risk? The box is pretty easy with TikZ, but how is the heading done? – user31729 Feb 4 at 22:40
• If you need just a rectangle without border, a simple \rule{}{} with \textcolor{}{} should work. – Sigur Feb 4 at 22:43

\documentclass{article}
\usepackage{xcolor}
\begin{document}

\def\risk#1{\fcolorbox{black}{red}{\color{red}\makebox[#1em]{x}}}
\setcounter{section}{0}\section*{Risk assessments:}
\section{Tipple \risk{1}}
\section{Drive a car \risk{3}}
\section{Tipple and drive a car \risk{10}}

\vspace{1cm}

\def\risk#1{{\fboxsep0pt\protect\fbox{\color{red}\rule{#1em}{1ex}}}}
\setcounter{section}{0}\section*{Risk assessments:}
\section{Tipple \risk{1}}
\section{Drive a car \risk{3}}
\section{Tipple and drive a car \risk{10}}

\end{document}


Note that used just as sowed in the above MWE, the rule will be printed also in the table of contents and page headings as is really part of the section title, but you can use the optional argument of \section to supply a short title without the rule or anything else, as the risk level as a number instas of a bar. If you are too lazy to type the section title twice, there is one way to simplify:

\documentclass{article}
\def\risk#1:#2 {\section[#1 (risk level: #2)]{#1 \fboxsep0pt\protect\fbox{\color{red}\rule{#2em}{1ex}}}}
\usepackage{xcolor}

• @Werner I do not see any other problem that in case of a ToC you must use the optional argument of section, e.g., \section[Tipple]{Tipple \risk{1}}. – Fran Feb 5 at 0:53
• \section{} break the line (paragraph) and add a vertical space, no anything after will be displaced to the start of the nomal paragraph, unless you redefine this command. – Fran Feb 5 at 22:58