How can I produce a dash-dotted line .-.-.-.-
? I tried to find something in the tikz
manual, but didn't find anything.
3 Answers
The various pre-defined dash patterns are documented in section 15.3.2 Graphic Parameters: Dash Pattern of the manual (for version 3.0.1.a dated 29 August 2015). They are dotted
, dashed
, dash dot
and dash dot dot
. Each of these have denser and looser variants, e.g. densely dashed
and loosely dotted
. Equivalently for the others.
In addition you can specify a custom pattern using e.g. dash pattern={on 4pt off 1pt on 2pt off 3pt}
, which I guess is self explanatory.
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw [thick,dash dot] (0,1) -- (5,1);
\draw [thick,dash pattern={on 7pt off 2pt on 1pt off 3pt}] (0,0) -- (5,0);
\end{tikzpicture}
\end{document}
Addendum
If you want to put a line like this in the text, and aligned better to the surrounding text, then you can add the baseline=<length>
option to the tikzpicture
. By default the bottom end of the tikzpicture
is placed on the baseline of the surrounding text. If you add baseline=10pt
then the tikzpicture
will be placed so that y=10pt in its internal coordinate system is on the baseline of the surrounding text.
Here is an example. \tikz
is a short form of the tikzpicture
environment, intended for simple pictures placed in the text.
\documentclass{article}
\usepackage{tikz}
\begin{document}
Lorem ipsum \tikz\draw [thick,dash dot] (0,0) -- (3,0); dolor sit amet
Lorem ipsum \tikz[baseline=-0.5ex]\draw [thick,dash dot] (0,0) -- (3,0); dolor sit amet
\end{document}
-
(x1, y1)
and(x2, y2)
seem to be coordinates. Is it right, that the coordinate with the lower x part is set to be wherever the environment ends? My tests tell so. Than how can I set the line in the middle line high, like appearing like this:before --- after
– user1Sep 4, 2016 at 20:21 -
-
1@Ben I'm not entirely sure what you mean, but I think you're after the
baseline
key, see the addition to my answer. Sep 4, 2016 at 20:32 -
I am sorry, but how can it be written if the length is needed to be a fraction of textwidth or 2cm for example?– DiaaSep 4, 2016 at 20:40
-
2@DiaaAbidou As mentioned above, the default unit vectors are 1cm, so a line from (0,0) to (2,0) would be 2cm. That said, you can also use explicit units, so
\draw (0,0) -- (2cm,0);
works. And TeX lengths work as well, so you can say e.g.\draw (0,0) -- (0.1\linewidth,0);
Sep 4, 2016 at 20:52
Here is a version using leaders. While all the parameters can be changed, the main ones are \dashdotline{<length>}
to draw the line, with these configurations: \dashfrac{<percent of repetition used by dash>}
; \replength=<repetition-length>\relax
.
\documentclass[10pt]{article}
\newlength\replength
\newcommand\repfrac{.40}% PERCENT OF REPETITION USED BY DASH
\newcommand\dashfrac[1]{\renewcommand\repfrac{#1}}% MACRO TO ALTER \repfrac
\setlength\replength{8.5pt}% REPITITION LENGTH
\newcommand\rulewidth{.6pt}% DASH WIDTH
\def\dashht{.5\dimexpr\ht\strutbox-\dp\strutbox\relax}
\newcommand\tdashfill[1][\repfrac]{\cleaders\hbox to \replength{%
\smash{\rule[\dashht]{\repfrac\replength}{\rulewidth}%
\kern.5\dimexpr\replength-\repfrac\replength-2.5pt\relax%
\raisebox{\dimexpr\dashht-.3pt}{.}}}\hfill}
\newcommand\dashdotline[1]{%
\makebox[#1][l]{\tdashfill\hfil}}
\begin{document}
x\dashdotline{2in}y\par
\replength 17.5pt
x\dashdotline{1in}y\par
\dashfrac{.66}
x\dashdotline{1in}y
\end{document}
Yet another solution with leaders, not perfect but as simple as possible:
\xleaders\hbox to 1em{$- \cdot$}\hfill $-$
If you will use this often, define some macro as \dashdotted
,
If you want a specific length instead of filling the line, simply enclose it in a \makebox
(e.g.,\makebox[2cm]{\dashdotted}
). MWE:
\documentclass[a5paper,twocolumn]{article}
\def\dashdotted{\xleaders\hbox to 1em{$- \cdot$}\hfill $-$}
\begin{document}
x\dashdotted 1\par
x\makebox[2cm]{\dashdotted}1\par
xxxxx\dashdotted 1\par
xxxxxxxxxx\dashdotted 1\par
xxxxxxxxxxxxxxx\dashdotted 1
\end{document}