[Tex/LaTex] tikz-timing: is there a straightforward way to add a numbered time axis

tikz-pgftikz-timing

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.

Best Answer

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}

Result


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}
Related Question