What's the dotted line equivalent of \rule?

There is a canonical TeX command: \leaders

https://www.tug.org/utilities/plain/cseq.html#leaders-rp

For example \hbox to 5cm{\leaders\hbox to 10pt{\hss . \hss}\hfil}

UPD: example explanation

  1. \hbox to 5cm{} creates horizontal box. Here 5cm is the total length of the point line
  2. \leaders is a command to create leaders. Leaders can create a horizontal or vertical redublication of any box. See more about leaders in Knuth's TeXBook, ch. 21 p 223.
  3. \hbox to 10pt{\hss . \hss} is a TeX box - the first parameter of the leader. In this box: to 10pt is the size of the box - change it to make dots thicker or thinner, \hss . \hss means "use a dot . as a box content, but surround it with glue with infinite stretching and compression" - so, there will be no underfull or overfull warnings.
  4. \hfil is a glue - the second parameter of the leader. It says how long the leader will be - till the end of the outer box.

TL;DR: to change the total width change the 5cm parameter, to change the width of one element change the 10pt parameter, to make not a dot but any other symbols (one or many) change . parameter


\hdashrule is only an approximation because the dots are little squares, but you can easily do what you need with TikZ.

I have created two commands, \dhorline (for horizontal rules) and \dvertline (for vertical ones), with the same syntax as \rule, where the dots are actual dots.

Here some examples:

\documentclass{article}
\usepackage{dashrule}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\newcommand{\dhorline}[3][0]{%
    \tikz[baseline]{\path[decoration={markings,
      mark=between positions 0 and 1 step 2*#3
      with {\node[fill, circle, minimum width=#3, inner sep=0pt, anchor=south west] {};}},postaction={decorate}]  (0,#1) -- ++(#2,0);}}
\newcommand{\dvertline}[3][0]{%
    \tikz[baseline]{\path[decoration={markings,
      mark=between positions 0 and 1 step 2*#2
      with {\node[fill, circle, minimum width=#2, inner sep=0pt, anchor=south west] {};}},postaction={decorate}] (0, #1) -- ++(0,#3);}}  
\begin{document}
With \verb|\rule{10em}{4pt}| \rule{10em}{4pt}

With \verb|\hdashrule{10em}{4pt}{4pt}|
\hdashrule{10em}{4pt}{4pt}

With \verb|\dhorline{10em}{4pt}| \dhorline{10em}{4pt}

\vspace{4pt}
With \verb|\rule[-4ex]{40pt}{1em}| \rule[-4ex]{40pt}{1em}

\vspace{4pt}
With \verb|\dhorline[-4ex]{40pt}{1em}| \dhorline[-4ex]{40pt}{1em}

With \verb|\rule{4pt}{10em}| \rule{4pt}{10em}
with \verb|\dvertline{4pt}{10em}|
\dvertline{4pt}{10em}

\vspace{4pt}
With \verb|\rule[3ex]{4pt}{10em}| \rule[3ex]{4pt}{10em}
with \verb|\dvertline[3ex]{4pt}{10em}|
\dvertline[3ex]{4pt}{10em}
\end{document}

enter image description here

Inspiration from this answer.

Tags:

Line