Creating several rulers with TikZ

  1. The order in the \foreach loop has no bearing on the order of drawing. If you specify (\x cm,0cm), then the coordinate is at (\x cm,0cm) (in the canvas system).
    I suggest simply to invert the x direction of the TikZ coordinate system: x=-1cm. This means that if you do not specify a unit for the x variable of a coordinate, TikZ will multiply that number with -1cm. Of course, you could have simply used -\x cm but this solution is a little bit more flexible.

  2. line cap=rect makes the same line end as butt (the default) just extended for half the line width.

  3. We simply enclose one meter in another \foreach loop that goes from 0 (the first meter) to 13 (the fourteenth meter). The first step is to yshift, for the meter 0 about 00cm, then 10cm up to 130cm. If you want a different shift, i.e. distance between the meter, you can use PGF math: yshift=\i*7cm.

Code

\documentclass[tikz,convert=false,12pt]{standalone}
\renewcommand*\familydefault{\sfdefault} 
\renewcommand\familydefault{\sfdefault} 
\usepackage[T1]{fontenc}
\begin{document}
\begin{tikzpicture}[x=-1cm,line cap=rect]
\foreach \i in {0,...,13}{
  \tikzset{yshift=\i0cm}

  \foreach \x in {1,3,...,99}
  \draw [color=red, line width=6mm](\x,0cm) -- (\x,1.5cm) 
  node[color=red, rotate=90, anchor=west] {\pgfmathprint{int(\x+\i*100)}};   

  \foreach \x in {0,2,...,98}
  \draw [color=black, line width=6mm](\x,0cm) -- (\x,1.5cm) 
  node[color=black, rotate=90, anchor=west] {\pgfmathprint{int(\x+\i*100)}};

  \foreach \x in {0,10,...,100}
  \draw [color=black,line width=6mm] (\x,4cm) -- (\x,0cm)
  node[anchor=north] {\Huge \textbf{{\pgfmathprint{int(\x+\i*100)}}}};


  \foreach \x in {5,15,...,95}
  \draw [color=red, line width=6mm] (\x,4cm) -- (\x,0cm)
  node[anchor=north] {\Huge \textbf{\pgfmathprint{int(\x+\i*100)}}};

  \draw [line width=6mm](0, 0cm) -- coordinate (x axis mid) (100,0cm);
}
\end{tikzpicture}
\end{document}

Output

enter image description here


Thank you for this post & discussion! I had a similar problem, it helped a lot to get started. But then there were some extra features .... this is why I would like to share my result.

Basically these are two two-lines rulers for a xy-plotter like arrangement with marked coordinates. enter image description here

Here you go:

\documentclass[tikz,convert=false,12pt,a3paper]{standalone}
\renewcommand*\familydefault{\sfdefault} 
\renewcommand\familydefault{\sfdefault} 
\usepackage[T1]{fontenc}
\begin{document}
\begin{tikzpicture}[x=-1cm,line cap=rect]
\def\zeile{36}
\def\tickl{1cm}
\def\tickxl{3cm}
\def\tickm{.6cm}
\def\ticks{.4cm}
\def\spaceb{1cm}
\def\spacet{4cm}

%  draw labels below ruler
%% x labels
\foreach \p [count=\mycount from 1] in {20,30,31,42,53,54.3}                         {
  \pgfmathparse{ifthenelse(\p > \zeile,{\p-\zeile+1},{\p}} 
  \global\let\pos=\pgfmathresult;
  \pgfmathparse{ifthenelse(\p > \zeile,4,0} 
  \global\let\shift=\pgfmathresult;
  \draw [color=red,line width=3pt](\pos,\shift) -- ++    (0,\tickxl)
  node[color=red, rotate=90, anchor=west,above left] {\large     {$\mathsf{ x_\mycount}$}};
}

% y labels
\foreach \p [count=\mycount from 1] in     {15,20,31,35,36.5,53,54.3}{
  \pgfmathparse{ifthenelse(\p > \zeile,{\p-\zeile+1},{\p}} 
  \global\let\pos=\pgfmathresult;
  \pgfmathparse{ifthenelse(\p > \zeile,3*4,2*4} 
  % TODO: how to use variable \spacet without unit cm? 
  \global\let\shift=\pgfmathresult;
  \draw [color=blue,line width=3pt](\pos,\shift) -- ++    (0,\tickxl)
  node[color=blue, rotate=90, anchor=west,above left]     {\large {$\mathsf{ y_\mycount}$}};
}

\draw [dashed](0, -1cm) -- ++ (\zeile,0cm);

\foreach \j in {0,2,4}{ % one x, one y, one blank
  \foreach \i in {0,1}{ % two lines per dim required
    \tikzset{yshift=(\i+\j)*\spacet}         

    % mm ticks without label
    \foreach \x in {0,0.1,0.2,...,\zeile}
    \draw [color=black, line width=1pt](\x,0cm) --     (\x,\ticks);
    \foreach \x in {0,0.5,1,...,\zeile}
    \draw [color=black, line width=1pt](\x,0cm) --     (\x,\tickm);

    % cm ticks with label 
    \foreach \x in {1,6,...,\zeile}
    \draw [color=black, line width=2pt](\x,0cm) --     (\x,\tickm)  node[color=black, rotate=90, anchor=west]     {\pgfmathprint{int(\x+\i*\zeile-1*\i)}};   
    \foreach \x in {2,7,...,\zeile}
    \draw [color=black, line width=2pt](\x,0cm) --     (\x,\tickm)   node[color=black, rotate=90, anchor=west]     {\pgfmathprint{int(\x+\i*\zeile-1*\i)}};
    \foreach \x in {3,8,...,\zeile}
    \draw [color=black, line width=2pt](\x,0cm) --     (\x,\tickm)   node[color=black, rotate=90, anchor=west]     {\pgfmathprint{int(\x+\i*\zeile-1*\i)}};
    \foreach \x in {4,9,...,\zeile}
    \draw [color=black, line width=2pt](\x,0cm) --     (\x,\tickm)   node[color=black, rotate=90, anchor=west]     {\pgfmathprint{int(\x+\i*\zeile-1*\i)}};

    % each 5cm large tick
    \foreach \x in {0,5,...,\zeile}
    \draw [color=black,line width=3pt](\x,0cm) --     (\x,\tickl)
    node[color=black, rotate=90, anchor=west] {\large     \textbf{{\pgfmathprint{int(\x+\i*\zeile-1*\i)}}}};

    % axis line & cut line
    \draw [line width=1mm](0, 0cm) --  (\zeile,0cm);
    \draw [dashed](0,3cm) -- ++ (\zeile,0cm);
  }
}

\end{tikzpicture}
\end{document}

Tags:

Tikz Pgf