1

Sometimes I want to add a line indicating the distance between two points as in the example

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture} \coordinate (A) at (0,0); \coordinate (B) at (0,3); \coordinate (C) at (8,0); \coordinate (D) at (5,1.125);

% drawing triangle
\draw (A) -- (B) -- (C) -- (A);

% drawing line in triangle
\draw [dashed] (5,0) -- (D);

% drawing d line
\draw  (0,-0.5) -- node[below] {$d$} (8,-0.5);
\draw (0,-0.4) -- (0,-0.6);
\draw (8,-0.4) -- (8,-0.6);

% drawing x line
\draw (5.175,1.593) -- node[above, rotate=-20.556] {$x$} (8.175,0.468);
\draw (5.14,1.499) -- (5.21,1.686);
\draw (8.14,0.374) -- (8.21,0.561);

\end{tikzpicture}

\end{document}

enter image description here

But doing it like this is time consuming as there are a lot of calculations to do. Is there a way to do it automatically so that I can draw the lines with one command without calculations and autorotation? For example:

%drawing d line
\draw [offset=-0.5, amplitude=0.2] (A) -- node[below] {$d$} (C)
% drawing x line
\draw [offset=0.5, amplitude=0.2] (D) -- node[above] {$x$} (C)
Albert
  • 183

2 Answers2

4
\documentclass[tikz, border=1cm]{standalone}
\usepackage{tikz-dimline}
\begin{document}
\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (0,3);
\coordinate (C) at (8,0);
\coordinate (D) at (5,1.125);
\draw (A) -- (B) -- (C) -- cycle;
\draw[dashed] (5,0) -- (D);
\dimline[extension start length=-0.5 cm, extension end length=-0.5 cm] {($(A)!.5cm!-90:(C)$)} {($(C)!.5cm!90:(A)$)}{d};
\dimline[extension start length=0.5 cm, extension end length=0.5 cm] {($(D)!.5cm!90:(C)$)} {($(C)!.5cm!-90:(D)$)}{x};
\end{tikzpicture}
\end{document}

Triangle with dimension lines

If you prefer the more ugly way without dimline :

\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (0,3);
\coordinate (C) at (8,0);
\coordinate (D) at (5,1.125);
\draw (A) -- (B) -- (C) -- cycle;
\draw [dashed] (5,0) -- (D);
\draw[|-|] ($(A)!.5cm!-90:(C)$) --node[below, sloped]{d} ($(C)!.5cm!90:(A)$);
\draw[|-|] ($(D)!.5cm!90:(C)$) --node[above, sloped]{x} ($(C)!.5cm!-90:(D)$);
\end{tikzpicture}
\end{document}

Triangle with dimlines

Edit:

Comment by @Qrrbrbirlbel

The |-| arrow tips will only touch the targeted points and will not be directly orthogonally above the points.

That is actually what you would expect from a dimension line - just like in the tikz-dimline package. -see picture from manual:

End arrow with triangle and bar

If one want the arrow tip centered orthogonal over the point, it can be done with the sep option like this:

\draw[{|[sep=0pt -0.5]}-{|[sep=0pt -0.5]}] ($(D)!.5cm!90:(C)$) --node[above, sloped]{x} ($(C)!.5cm!-90:(D)$);
0

It is quite easy with pstricks:

    \documentclass[pstricks]{standalone}
    \usepackage{pst-eucl}
\begin{document}

\begin{pspicture}(-1,-1)(9,4)
\psset{linejoin=1,PointSymbol=none, PointName = none, dash=3.5pt 2pt}
\pstTriangle(0,0){A}(0,3){B}(8,0){C}
\pnodes(5,0){D}(5,1.125){E}
\psset{linewidth=0.5pt}
\psline[linestyle=dashed](D)(E)
\pcline[offset=-12pt]{|-|}(A)(C) \nbput{$d$}
\pcline[offset=12pt]{|-|}(E)(C) \naput[nrot=:U]{$x$}
\end{pspicture}

\end{document} 

enter image description here

Bernard
  • 271,350