2

Problem

I would like to indicate the time duration of an event as a colored patch inside a horizontal bar, the latest representing a 24-hour timeline (i.e. a 24 units long bar), that should help the reader just as a "reference" bar for the time in a day. Then, I would like to add both the patch and the 24-hour timeline bar inside a cell of a table.

My desired output would be something similar to the figure here below. As an example, I drew 2 red patches indicating the time duration of two distinct events and two "reference" timeline bars of 24 hours length (with reference ticks every 6 hours, i.e. at 00:00, at 06:00, at 12:00, at 18:00 and at 24:00). The best would be to draw these two figures just giving the start and the end hours of an event as inputs.

How to draw both the patch and the "reference" bar representing a 24-hour timeline?

enter image description here

My starting point (MWE)

I tried to start working on the method presented in Is it possible to create a barchart in a table?, but I do not know how to continue...

\documentclass{article}
\usepackage{xfp} 
\usepackage{color}

\begin{document}

% (1) define the 24 hours bar \def\mybar#1{ {\color{black}\rule{\fpeval{#1/\myscale*\barwidth} cm}{\barheight}} #1 } \newcommand{\barwidth}{5} % cm max bar widths \newcommand{\barheight}{4pt} % height of each bar \newcommand{\myscale}{24} % max scale for hours bars

% (2) draw the 24 hours bar \mybar{24}

\end{document}

Which produces this bar:

enter image description here

Ommo
  • 835
  • 1
    Graphics is a whole lot easier using graphics software. Even a picture environment would be a ginat leap foreward. – John Kormylo Feb 22 '23 at 15:43
  • Thanks a lot @JohnKormylo for your comment! :-) However, don't you think this method is faster for producing small stylized graphical objects to be added in Latex Tables, than using graphics software? For example, the simple solution provided here below by Jasper Habicht, allows to complete your Latex document without passing from one software/editor to another one... it is a kind of embedded function that supports your text and tables readability..This is just my thoughts :-) – Ommo Feb 23 '23 at 12:46

2 Answers2

4

A quick solution based on TikZ:

\documentclass{article}
\usepackage{tikz}

\begin{document}

\NewDocumentCommand{\hoursbar}{ O{5cm} O{4pt} O{red} m m }{% \begin{tikzpicture}[baseline] \fill[#3] ({(#4/24)#1},0) rectangle ({(#5/24)#1},{#2}); \draw (0,0) rectangle ({0.25#1},{#2}); \draw ({0.25#1},0) rectangle ({0.5#1},{#2}); \draw ({0.5#1},0) rectangle ({0.75#1},{#2}); \draw ({0.75#1},0) rectangle ({#1},{#2}); \end{tikzpicture}% }

\hoursbar{9}{17}

\hoursbar[3cm]{15}{24}

\hoursbar[4cm][8pt]{8}{20}

\hoursbar[5cm][8pt][cyan]{8}{20}

\end{document}

The arguments of the macro \hoursbar are:

  • #1 (optional): width of the bar
  • #2 (optional): height of the bar
  • #3 (optional): color of the inner bar
  • #4: start hour (in 24h format)
  • #5: end hour (in 24h format)

enter image description here

1

This shows how to use a picture environment.

I changed \barheight into a length to reduce the number of \dimexpr commands. For example, -0.5\barheight becomes {\dimexpr -0.5*\barheight} when using a macro.

\unitlength is built in and is used whenever no units are supplied.

\line is a bit strange in that is uses length instead of coordinates and can only handle a limited number of slopes.

\documentclass{article}
\usepackage{xfp} 
\usepackage{color}

\newlength{\barheight} \setlength{\barheight}{4pt} \newcommand{\barwidth}{5cm} % max bar widths

\begin{document}

\fboxsep=0pt \setlength{\unitlength}{\dimexpr \barwidth/24}% set de3fault scale Current location: % draw red bar from 6am to 6pm \framebox{\begin{picture}(24,\barheight)(0pt,-0.5\barheight) \put(6,0pt){\color{red}\linethickness{\barheight}\line(1,0){12}} \end{picture}} \end{document}

John Kormylo
  • 79,712
  • 3
  • 50
  • 120