Skip to content

Instantly share code, notes, and snippets.

@Drup
Last active February 15, 2023 12:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Drup/8607ca590257d5a98277514cf40dcd6c to your computer and use it in GitHub Desktop.
Save Drup/8607ca590257d5a98277514cf40dcd6c to your computer and use it in GitHub Desktop.
A simple package to make horizontal timeline. Taken and heavily modified from somewhere in the internet.
\NeedsTeXFormat{LaTeX2e}%
\ProvidesPackage{chronology}[2015/03/27 v1.1.1 Horizontal timeline]%
\RequirePackage{calc}%
\RequirePackage{tikz}%
\RequirePackage{xparse}%
% Defining counters and lengths
\newcounter{step}\newcounter{stepstart}\newcounter{stepstop}%
\newcounter{yearstart}\newcounter{yearstop}\newcounter{deltayears}%
\newlength{\xstart}\newlength{\xstop}%
\newlength{\unit}\newlength{\timelinewidth}%
\newlength{\ticklength}\newlength{\timelineheight}
\newsavebox{\timelinebox}%
% Defining TikZ styles for labels and events
\tikzset{%
,chronevent/.style={fill=black,draw=none,opacity=0.5}
,chronperiod/.style={fill=black,draw=none,opacity=0.5}
,chronlabel/.style={opacity=1}
,chrontickslabel/.style={scale=0.7,chronlabel}
,chroneventlabel/.style={chronlabel}
,eventlabel/.style={chroneventlabel,anchor=south west,yshift=.2\unit,rotate=45}
,flippedeventlabel/.style={chroneventlabel,anchor=north west,yshift=-.2\unit,rotate=-45}
}
\makeatletter%
\newif\ifchronology@star%
\newenvironment{chronology}{%
\@ifstar{\chronology@startrue\chronology@i*}{\chronology@starfalse\chronology@i*}%
}{%
\end{tikzpicture}%
\end{lrbox}%
\resizebox{\timelinewidth}{!}{\usebox{\timelinebox}}%
}%
\def\chronology@i*{%
\@ifnextchar[{\chronology@ii*}{\chronology@ii*[{5}]}%
}%
\def\chronology@ii*[#1]#2#3#4{%
\@ifnextchar[{\chronology@iii*[{#1}]{#2}{#3}{#4}}{\chronology@iii*[{#1}]{#2}{#3}{#4}[{#4}]}%
}%
\def\chronology@iii*[#1]#2#3#4[#5]{%
\newif\ifflipped%
\ifchronology@star%
\flippedtrue%
\else%
\flippedfalse%
\fi%
\setcounter{step}{#1}%
\setcounter{yearstart}{#2}\setcounter{yearstop}{#3}%
\setcounter{deltayears}{\theyearstop-\theyearstart}%
\setlength{\unit}{#5/\thedeltayears}%
\setlength{\timelinewidth}{#4}%
\setlength{\ticklength}{.12\unit}%
\setlength{\timelineheight}{.1\unit}%
\pgfmathsetcounter{stepstart}{\theyearstart-mod(\theyearstart,\thestep)}%
\pgfmathsetcounter{stepstop}{\theyearstop-mod(\theyearstop,\thestep)}%
\addtocounter{step}{\thestepstart}%
\begin{lrbox}{\timelinebox}%
\begin{tikzpicture}[
% For compatibity with babel. Handling of active characters in TikZ.
handle active characters in code,
handle active characters in nodes,
]
\draw [->] (0,0) -- (\thedeltayears*\unit+\unit, 0);%
\addtocounter{deltayears}{1}%
\foreach \x in {\thestepstart,\thestep,...,\thestepstop}{%
\pgfmathsetlength\xstop{(\x-\theyearstart)*\unit}%
\draw[xshift=\xstop] (0,-\ticklength) -- (0,\ticklength);%
\ifflipped%
\node[chrontickslabel,above,inner sep=0pt]
at (\xstop,\ticklength) {\x};%
\else%
\node[chrontickslabel,below,inner sep=0pt]
at (\xstop,-\ticklength) {\x};%
\fi%
}%
}%
\makeatother%
\DeclareDocumentCommand \event {s m m}{%
\pgfmathsetlength\xstop{(#2-\theyearstart)*\unit}%
\IfBooleanTF{#1}{\flippedtrue}{\flippedfalse}
\ifflipped%
\draw[chronevent]%
(\xstop, 0) -- (\xstop+\timelineheight/2,0)
arc(0:-180:\timelineheight/2) -- cycle
node[flippedeventlabel] {#3};%
\else%
\draw[chronevent]%
(\xstop, 0) -- (\xstop+\timelineheight/2,0)
arc(0:180:\timelineheight/2) -- cycle
node[eventlabel] {#3};%
\fi%
}
\DeclareDocumentCommand \period {s m m m}{%
\pgfmathsetlength\xstart{(#2-\theyearstart)*\unit}%
\pgfmathsetlength\xstop{(#3-\theyearstart)*\unit}%
\IfBooleanTF{#1}{\flippedtrue}{\flippedfalse}
\ifflipped%
\draw[chronperiod,rounded corners=\timelineheight/2]%
(\xstart,-\timelineheight) rectangle%
node[flippedeventlabel] {#4} (\xstop,0);%
\else%
\draw[chronperiod,rounded corners=\timelineheight/2]%
(\xstart,0) rectangle%
node[eventlabel] {#4} (\xstop,\timelineheight);%
\fi%
}
\newcommand{\decimaldate}[3]{(#1-1)/31/12+(#2-1)/12+#3}%
\endinput%
\documentclass[aspectratio=169,dvipsnames,svgnames,10pt]{beamer}
\usepackage{tikz}
\usepackage{chronology}
\begin{document}
\begin{frame}[t]
\noindent
\begin{chronology}
[2] % Granularity of the scale
{2014} % Start
{2020} % Stop
{\textwidth} % External Width
[10cm] % Internal Width, Allow to adjust the scale
% Let's use horizontal labels at the top
\tikzset{eventlabel/.style={chroneventlabel,text depth=0pt,yshift=.2\unit}}
% A simple period
\period{2014.2}{2014.7}{Period}
% A Flipped period
\period*{2017}{2017.3}{Period Under}
% We can redefine the style locally
\tikzset{chronperiod/.style={fill=RoyalBlue,fill opacity=0.8}}
\period{2014.7}{2017.7}{\color{RoyalBlue}Blue Period}
% An individual event
\event{2018}{Event}
% An event with custom filling, smaller label, and on the other side
\tikzset{chronevent/.style={fill=black}}
\tikzset{flippedeventlabel/.style={
chroneventlabel,anchor=north west,yshift=-.2\unit,rotate=-45,scale=0.7
}}
\event*{2019}{Event Under}
% We can also use overlays
\tikzset{chronperiod/.style={fill=Red,fill opacity=0.8}}
\visible<2>{\period{2018.7}{2019.7}{\color{Red}Period later}}
\end{chronology}
\end{frame}
\end{document}
%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:
@MarcoWpunkt
Copy link

Is there a way to specify the size of the event point on the line? I want it to be a little smaller if possible. I guess its "chronevent". But which property do I need to set in style to which value?

@Drup
Copy link
Author

Drup commented Feb 15, 2023

Is there a way to specify the size of the event point on the line? I want it to be a little smaller if possible. I guess its "chronevent". But which property do I need to set in style to which value?

You can try to play around with \timelineheight for that. I didn't really make that more customizable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment