# Time-line for time intervals

I search for a command that do something like this: I can enter two time intervals: the first is the total length of the time-line, and the second is the period to be highlighted. For example the following command:

\timeline{1900-2000}{1950}{2000}


produce something like this:

Another example:

\timeline{1900-2000}{1925}{1965}


Optionally I can print the years (for example with the * symbol):

\timeline{*1900-*2000}{*1945}{1976}


If I want to print all the years, I can enter both the following commands:

\timeline{*1900-*2000}{*1950}{*2000}
\timeline[*]{1900-2000}{1950}{2000}


Another example:

\timelineyears[*]{1900-2000}{1925}{1965}


If I want I can enter an inexact year (for example with the ~ symbol), that produce a colour gradient in the direction of that year. Example:

\timelineyears{1900-2000}{1945}{~1976}


\timelineyears{1900-2000}{~1945}{1976}


\timelineyears{1900-2000}{~1945}{~1976}


(Perhaps, if I enter an inexact year and I print it, it is better to differentiate it in some way respect to the exact years.)

I can also set the time interval:

\settimeinterval{1900-2000}


in this way I have only to specify the range to highlight:

\timeline{1912}{1920}


The time interval remains the same until the next \settimeinterval declaration (but I can overwrite it with the default command \timeline{a-b}{c}{d}).

The command is structured as follows

\Timeline[ <option> ]{ <start>-<end> }{ <startint> }{ <endint> }


Adding * to the option will print all the years, otherwise, none will be printed. The first argument <start>-<end> will set the beginning and the end year respectively. The last two arguments set the interval. Adding ~ before the year will activate the gradient effect.

With the help of David Carlisle the tilde has been fixed. Now it will appear when you add a year that's supposed to be inexact.

## Code

\documentclass[margin=10pt]{standalone}
\usepackage{tikz}
\usepackage{xstring}
\usepackage[T1]{fontenc}
\usepackage{newtxtext}

\newcommand\Timeline[4][]{%
% Some options
\def\printy{*}
\def\option{#1}
% Setting Start-End bar
\StrBehind{#2}{-}[\endyear]
\StrBefore{#2}{-}[\startyear]
% Setting Start-End Interval
\StrRight{#3}{4}[\startint]
\StrRight{#4}{4}[\endint]
% Some calculations
\pgfmathsetmacro\endy{(\endyear-\startyear)/10}
\pgfmathsetmacro\fromyear{(\startint-\startyear)/10}
\pgfmathsetmacro\toyear{(\endint-\startyear)/10}
\pgfmathsetmacro\midy{(\fromyear+\toyear)/2}
% Fill interval
\fill (\fromyear,0) rectangle (\toyear,.2);
\IfSubStr{#3}{~}{%
\fill[right color=black, left color=white] (\fromyear,0) rectangle (\midy,.2);
\node[anchor=north] at (\fromyear,0) {\string~\phantom{#3}};
}{}
\IfSubStr{#4}{~}{%
\fill[right color=white, left color=black] (\midy,0) rectangle (\toyear,.2);
\node[anchor=north] at (\toyear,0) {\string~\phantom{#4}};
}{}
\draw[white] (\fromyear,0) rectangle (\toyear,.2);
% Print years
\ifx\option\printy\relax
\node[anchor=north] at (0,0) {\startyear};
\node[anchor=north] at (\endy,0) {\endyear};
\node[anchor=north] at (\fromyear,0) {#3};
\node[anchor=north] at (\toyear,0) {#4};
\fi
% Print rectangle
\draw (0,0) rectangle (\endy,.2);
}

\begin{document}
\begin{tikzpicture}

\Timeline[*]{1900-2000}{~1950}{1974}
\end{tikzpicture}
\end{document}

• The chronosys package does this, in color, with its "chronoperiod" function. Although I do not think it will do a gradient. – A Feldman Feb 26 '16 at 12:44