# Dotted line with prescribed density with TikZ

Is there a way to get a dotted line with prescribed density, for instance 10 dots per centimeter, instead of the default?

With Jesse's comment :

\documentclass[10pt,a4paper,french]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fourier}
\usepackage{tikz}
\usetikzlibrary{calc,intersections,through,backgrounds}

\tikzstyle{cm dotted}=[dash pattern=on \pgflinewidth off .85mm ]

\begin{document}
\begin{tikzpicture}

\draw[cm dotted,line width=.15mm,step=.5] (0,0) grid (5,4) ;

\end{tikzpicture}
\end{document}


Still an unwanted shift of half a linewidth. The horizontal and vertical crossing points are not on at the right place. I understand why, but I don't know how fix it.

-
Welcome to TeX.SX! Usually, we don't put a greeting or a “thank you” in our posts. While this might seem strange at first, it is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Accepting and upvoting answers is the preferred way here to say “thank you” to users who helped you. A suggestion: Do us a favour and change your username to something more telling than "user45209" (any nickname is good). –  egreg Feb 1 '14 at 11:46
This could provide some directions -- tex.stackexchange.com/a/45276/34618 –  Jesse Feb 1 '14 at 12:32
@Jesse, it goes in the good direction. It works but still a unwanted shift of half a linewith. –  Tarass Feb 1 '14 at 13:14
You could update your question and give a more specific concerns, experts here are ready to help out. –  Jesse Feb 1 '14 at 13:33
I did it and I put a try in the direction you've give. –  Tarass Feb 1 '14 at 13:51

I think this does it. The size of the dot is determined by the line width. The trick is to use a dash of length 0. Which I suppose shouldn't work, but seems to.

\documentclass[tikz,border=0.125cm]{standalone}

\tikzset{%
dots/.style args={#1per #2}{%
line cap=round,
dash pattern=on 0 off #2/#1
}
}

\begin{document}

\foreach \i in {1,...,20}{
\begin{tikzpicture}
\draw [help lines] grid (4,1);
\draw [very thick, dots=\i per 1cm]
(0,.5) -- (4,.5)
node [at start, above right] {\i\ dot\ifnum\i>1s\fi\ per 1cm};
\end{tikzpicture}
}

\end{document}


-
It works just fine, the grid is perfect. –  Tarass Feb 1 '14 at 14:20

With dotsep in PSTricks.

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{multido}
\psset{linewidth=1mm,linestyle=dotted}
\begin{document}
\begin{pspicture}(6,8)
\multido{\i=0+1,\n=.1+.1}{9}{\psline[dotsep=\n](0,\i)(6,\i)}
\end{pspicture}
\end{document}


## Edit

\documentclass[pstricks,border=12pt]{standalone}
\psset{linewidth=3pt,linestyle=dotted}
\newlength\dotsep
\def\line#1{%
\dotsep=\dimexpr\dimexpr\psunit-#1\pslinewidth\relax/\numexpr#1-1\relax\relax
\psline[dotsep=\dotsep](0,1)(6,1)}

\begin{document}
\foreach \i in {2,...,10}{%
\begin{pspicture}[showgrid](6,2)
\rput[t](3,1.75){\i\ dots per cm}
\line{\i}
\end{pspicture}}
\end{document}


-
With pstricks I usualy use psgrid and it works find. I know how to do this with metapost. But now I need a tikz command (without foreach), but a style to apply on a line or on a grid. –  Tarass Feb 1 '14 at 12:50
@Tarass: OK. The multido in my answer is trivial. –  kiss my armpit Feb 1 '14 at 12:53
My question was not clear enought. –  Tarass Feb 1 '14 at 13:46
Look carefully the difference with Mark Wibrow tikz solution: the first and the last points are not on the grid, it means that it isn't precise enought especialy to make a grid. Exactly the grid pstrciks does and you use for your example. With you solution, the vertical and horizontal lines don't match at crossing points. –  Tarass Feb 2 '14 at 8:03

The “old” plain TeX actually does this out of the box in one line, without the need of external complex packages:

my text\leaders\hbox to 1em{\hss.\hss}\hfill other text


Other options are:

• use \hskip 5cm instead of \hfill to produce a dotted line 5cm long
• use \hbox to 0.1cm instead of hbox to 1em to change the dot separation to 1mm
• use \cleaders instead of \leaders to center the leaders, instead of having them alined
• use \xleaders to have the leaders expanded to fill out the entire space (in this case the separation provided is a lower limit only).
-
I need a tikz style like dotted densely, but with a way to prescribe the number of points in a cm. –  Tarass Feb 1 '14 at 12:47