Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
1  
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 at 11:46
    
This could provide some directions -- tex.stackexchange.com/a/45276/34618 –  Jesse Feb 1 at 12:32
    
@Jesse, it goes in the good direction. It works but still a unwanted shift of half a linewith. –  Tarass Feb 1 at 13:14
    
You could update your question and give a more specific concerns, experts here are ready to help out. –  Jesse Feb 1 at 13:33
    
I did it and I put a try in the direction you've give. –  Tarass Feb 1 at 13:51

3 Answers 3

up vote 10 down vote accepted

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

share|improve this answer
    
It works just fine, the grid is perfect. –  Tarass Feb 1 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}

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

share|improve this answer
    
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 at 12:50
    
@Tarass: OK. The multido in my answer is trivial. –  Who is crazy first Feb 1 at 12:53
    
My question was not clear enought. –  Tarass Feb 1 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 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).
share|improve this answer
1  
I need a tikz style like dotted densely, but with a way to prescribe the number of points in a cm. –  Tarass Feb 1 at 12:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.