# tikz-timing: is there a straightforward way to add a numbered time axis?

Loving tikz-timing for preparing timing diagrams for my teaching. One thing I've not been able to find, though, is how I might add a numbered time axis to a timing diagram. This is useful when showing the basics of testbench design, and wanting to indicate the correspondence in the diagram with a line in the Verilog source. I imagine a TikZ overlay could work, but I'm not that much of a pro, and would prefer something where the numbers are added automatically. I am using the vertical help lines, so somehow adding numbers above or below those would be great.

-

There is no dedicated macro provided by tikz-timing, but it is possible to draw such a labeled axis using the size macros and nodes provided.

The number of rows is given by \nrows and the widest row by \twidth. The rows are also provided as nodes called row<number> and the last row node is also named last row. To draw extra material place the drawing code in an extracode environment after the last row as shown in the example below. Please see the manual of tikz-timing for more details.

\documentclass[border=2mm]{standalone}
\usepackage{tikz-timing}
\begin{document}
\begin{tikztimingtable}
clk  &  20{C}   \\
sig  &   5{HLZD{}}  \\
\begin{extracode}
\begin{background}
\vertlines[help lines]{}
\horlines[help lines]{}
\show\horlines
\draw [->,>=latex] (0,-\nrows-1) -- (\twidth+1,-\nrows-1);
\foreach \n in {0,1,...,\twidth}
\draw (\n,-\nrows-1+.1) -- +(0,-.2)
node [below,inner sep=2pt] {\scalebox{.75}{\tiny\n}};
\end{background}
%\tablegrid
\end{extracode}
\end{tikztimingtable}%
\end{document}


If you need this more often you could define an own macro:

\documentclass[border=2mm,png]{standalone}
\usepackage{tikz-timing}

\newcommand{\timingaxis}[1][]{%
\begin{scope}[#1]
\draw [timing/table/axis] (0,-\nrows-1) -- (\twidth+1,-\nrows-1);
\foreach \n in {0,1,...,\twidth} {
\draw [timing/table/axis ticks]
(\n,-\nrows-1+.1) -- +(0,-.2)
node [below,inner sep=2pt] {\scalebox{.75}{\tiny\n}};
}
\end{scope}
}
\tikzset{%
timing/table/axis/.style={->,>=latex},
timing/table/axis ticks/.style={},
}

\begin{document}
\begin{tikztimingtable}
clk  &  20{C}   \\
sig  &   5{HLZD{}}  \\
\begin{extracode}
\timingaxis\relax
\end{extracode}
\end{tikztimingtable}%
\end{document}

-

Here is another possible way, using pgfplots and the axis environment; it would look like this:

... using this MWE:

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{tikz}
\usepackage{tikz-timing}
\usetikzlibrary{intersections} %% named intersections
\usetikzlibrary{fit}
\usetikzlibrary{calc}

\makeatletter
% http://tex.stackexchange.com/questions/33703/extract-x-y-
\newcommand{\gettikzxy}[3]{%
\tikz@scan@one@point\pgfutil@firstofone#1\relax
\edef#2{\the\pgf@x}%
\edef#3{\the\pgf@y}%
}
\makeatother

\begin{document}

\begin{axis}[clip=false,ymin = 0, ymax = 2, xmin = 0, xmax = 5]

%\addplot [] node [] (origin) at (0,0) {}; %no
\node[] (refsize) at ({axis cs:1,1}) {};

\pgfplotsextra{ % must have, else \gettikzxy will not work!
\gettikzxy{(refsize)}{\rx}{\ry}
% re-convert \rx back to length, so can scale it directly in style= below:
\newlength{\trx}
\setlength{\trx}{\rx}
\typeout{rx \rx - \the\trx, ry \ry}

\timing[very thick,style={x=0.5\trx,y=\ry},name=tgraph1] at ({axis cs:1,0.5}) { HLHLHL };

% debug:
% add frame border around tgraph1
\node[fit=(tgraph1), draw, color=gray] {};
% indicate origin of tgraph1 (is lower left corner):
\node[fill,circle,color=gray] at (tgraph1.origin) {};
} % end \pgfplotsextra

\end{axis}
\end{tikzpicture}

\end{document}


Note that one needs to do a bit of arithmetic, so as to scale the units of the \timing graph to those units shown on the graph - maybe my example above won't be pixel-perfect aligned, but at least it doesn't look all that difficult to set up...

-