箭头:当节点太大时翻转 ||

箭头:当节点太大时翻转 ||

当节点太大时,我们倾向于将箭头切换为->| 2cm |<-。有没有一种聪明的方法可以做到这一点,而无需绘制两条单独的线?


从图中我们可以看到,由于占据了整个线段,[>= stealth, |<->|]箭头尖几乎看不见。19.02 mm

在此处输入图片描述

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\draw[>= stealth, |<->|] (0, -0.3cm) -- ++(1cm, 0)
  node[pos = 0.5, fill = white, font = \tiny, inner sep = 0] {$19.02$ mm};
\end{tikzpicture}
\end{document}

答案1

只是基础知识,很多需要参数化才能得到一个合适的解决方案,但下面展示了一种使用装饰和\pgfnodepositionlater技巧的方法。正如我使用的node contents密钥一样,这需要最新的 PGF 版本:

\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{decorations}
\def\pgfdimgetwidth{%
  \pgfmathparse{\pgfpositionnodelatermaxx-\pgfpositionnodelaterminx}%
  \global\let\pgfdimwidth=\pgfmathresult%
}
\pgfdeclaredecoration{dimension}{draw}{%
\state{draw}[width=\pgfdecoratedpathlength]{
  % Get the width of the node without drawing it.
  \pgfpositionnodelater\pgfdimgetwidth%
  \node [node contents=, dimension label/.try];
  \pgfpositionnodelater\relax
  \pgfmathparse{\pgfdecoratedpathlength-\pgfdimwidth}%
  \ifdim\pgfdecoratedpathlength<0.375cm\relax
     \path (0,0) -- (\pgfdecoratedpathlength,0)
         node [rotate=\pgfdecoratedangle, midway, above=.25cm,, node contents=, dimension label/.try];
       \draw [->|] (-.25cm,0) -- (0,0);
       \draw [->|] (\pgfdecoratedpathlength+.25cm,0) -- (\pgfdecoratedpathlength,0);
  \else%
    \ifdim\pgfmathresult pt<0cm\relax%
       \draw [|<->|] (0,0) -- (\pgfdecoratedpathlength,0)
        node [rotate=\pgfdecoratedangle, midway, above=.25cm, node contents=, dimension label/.try];
    \else%
      \ifdim\pgfmathresult pt<.25cm\relax%
        \path (0,0) -- (\pgfdecoratedpathlength,0)
           node [rotate=\pgfdecoratedangle, midway, node contents=, dimension label/.try];
         \draw [->|] (-.25cm,0) -- (0,0);
         \draw [->|] (\pgfdecoratedpathlength+.25cm,0) -- (\pgfdecoratedpathlength,0);
      \else%
        \draw [|<->|] (0,0) --  (\pgfdecoratedpathlength,0)
        node [rotate=\pgfdecoratedangle, midway, node contents=, dimension label/.try] ;
      \fi%
    \fi%
  \fi%
}
}
\tikzset{%
  dimension/.style={
    decoration=dimension, decorate,
    dimension label/.style={
      node contents=#1, fill=white, font=\tiny, inner sep=0pt
    }
  }
}
\begin{document}
\begin{tikzpicture}[>=stealth]
\foreach \i [count=\y] in {0.125,0.25,0.5,1,2}
\draw [dimension=\i cm] (0,\y) -- (\i,\y);
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容