在 latex-tikz 中将距离线添加到两点之间

在 latex-tikz 中将距离线添加到两点之间

有时我想添加一条线来表示两点之间的距离,如示例中所示

\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}

在此处输入图片描述

但这样做很耗时,因为需要进行大量计算。有没有办法自动完成,以便我可以用一个命令绘制线条,而无需计算和自动旋转?例如:

%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)

答案1

\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}

带尺寸线的三角形

如果你更喜欢没有的更丑陋的方式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}

带斜线的三角形

编辑:

评论来自 @Qrrbrbirlbel

|-| 箭头尖端只会接触目标点,而不会直接正交于点上方。

这实际上就是您所期望的尺寸线 - 就像包装中一样tikz-dimline。 - 参见手册中的图片:

带三角形和横线的末端箭头

如果希望箭头尖端与点正交,可以使用sep如下选项:

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

答案2

这很容易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} 

在此处输入图片描述

相关内容