9

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.

Tarass
  • 16,912

3 Answers3

15

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}

enter image description here

Mark Wibrow
  • 70,437
2

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}

enter image description here

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}

enter image description here

  • 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 01 '14 at 12:50
  • @Tarass: OK. The multido in my answer is trivial. – kiss my armpit Feb 01 '14 at 12:53
  • My question was not clear enought. – Tarass Feb 01 '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 02 '14 at 08:03
0

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).
  • 1
    I need a tikz style like dotted densely, but with a way to prescribe the number of points in a cm. – Tarass Feb 01 '14 at 12:47