[Tex/LaTex] How to align a dashrule

formattingrules

I've been playing around with the dashrule package. But easy as it's shown in the manual, I don't get a proper alignment or width from the dashlines.

\documentclass{article}
\usepackage{dashrule}
\begin{document}

\setlength{\parindent}{0mm}
\rule[0mm]{\linewidth}{1mm}
\hdashrule[0.5ex][x]{\linewidth}{0.5pt}{1.5mm}
\hdashrule[0.5ex][c]{\linewidth}{0.5pt}{1.5mm}
\rule[0mm]{\linewidth}{1mm}

\end{document}

enter image description here

I want the leftmost dash at the left margin and the rightmost dash at the right margin – means: widened gaps – (argument [x])
OR
equal remaining space on the left and right margin (argument [c]). Both should be possible.

Best Answer

You could do this with leaders, but you can instead make TeX count how many copies of the dash will fit when a minimum gap between them is added and substitute this minimum gap with \hfill to cover up your tracks.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\fillwithdashes}{O{}m}
 {
  \group_begin:
  \keys_set:nn { musicman/dashes } { #1 }
  \hbox_set:Nn \l_musicman_dash_box
   {
    \rule[\l_musicman_height_dim]{\l_musicman_width_dim}{\l_musicman_thickness_dim}
   }
  \dim_set:Nn \l_musicman_target_dim { #2 - \box_wd:N \l_musicman_dash_box }
  \dim_set:Nn \l_musicman_piece_dim
   {
    \box_wd:N \l_musicman_dash_box + \l_musicman_gap_dim
   }
  \leavevmode
  \box_use:N \l_musicman_dash_box
  \prg_replicate:nn
   {
    \int_div_truncate:nn { \l_musicman_target_dim } { \l_musicman_piece_dim }
   }
   { \hfill \box_use:N \l_musicman_dash_box }
  \group_end:
 }

\keys_define:nn { musicman/dashes }
 {
  thickness .dim_set:N = \l_musicman_thickness_dim,
  height    .dim_set:N = \l_musicman_height_dim,
  width     .dim_set:N = \l_musicman_width_dim,
  min-gap   .dim_set:N = \l_musicman_gap_dim,
  thickness .initial:n = 0.4pt,
  height    .initial:n = 0pt,
  width     .initial:n = 1.5mm,
  min-gap   .initial:n = 1.5mm,
 }
\box_new:N \l_musicman_dash_box
\dim_new:N \l_musicman_target_dim
\dim_new:N \l_musicman_piece_dim

\ExplSyntaxOff


\begin{document}

\setlength{\parindent}{0mm}

\hrule height 1pt
\strut\fillwithdashes[height=0.5ex,width=1.5mm,min-gap=1mm,thickness=0.4pt]{\linewidth}
\hrule height 1pt

\bigskip

\hrule height 1pt
\strut\fillwithdashes{\linewidth}
\hrule height 1pt

\end{document}

enter image description here