答案1
tikz-dimline
是为此目的而设计的。以下代码是其文档中的一个示例。
\documentclass[tikz, border=2mm]{standalone}
\usepackage{tikz-dimline}
\begin{document}
\begin{tikzpicture}[scale=0.5]
\draw[help lines] (-1,-1) grid (4,7);
\draw (0,0) -- (0,4) -- (.5,4) -- (.5,5) -- (1.,5) -- (1.,4) -- (1.5,4) -- (1.5,0)
-- cycle;
\dimline[color=blue,
extension start length=-0.25, extension end length=-0.25]{(2.75,0)}{(2.75,4)}{4.0};
\dimline[color=red,
line style={arrows=dimline reverse-dimline reverse},
label style={above=0.8ex,font=\tiny},
extension start length=1,
extension end length=1]{(0.5,5.6)}{(1.0,5.6)}{0.5};
\end{tikzpicture}
\end{document}
答案2
另一种方法是使用至路径操作:
\documentclass[border=5mm,tikz]{standalone}
\usetikzlibrary{arrows.meta,calc}
\tikzset{
diameter/.style={to path={},
execute at end to={
\draw[Stealth-](\tikztostart)--($(\tikztostart)!-0.5cm!(\tikztotarget)$);
\draw[Stealth-](\tikztotarget)--($(\tikztotarget)!-0.5cm!(\tikztostart)$);
\path (\tikztostart)--(\tikztotarget) \tikztonodes;
},
}
}
\begin{document}
\begin{tikzpicture}
\draw(0,0)--(2,0);
\draw(0,1)--(2,1);
\path[diameter] (0.5,0)to node{\(\phi\)}(1,1);
\path[diameter] (1.5,0)to node{\(\phi\)}(1.5,1);
\end{tikzpicture}
\end{document}