![在 TikZ 中将线的距离尺寸更改为装饰](https://linux22.com/image/358722/%E5%9C%A8%20TikZ%20%E4%B8%AD%E5%B0%86%E7%BA%BF%E7%9A%84%E8%B7%9D%E7%A6%BB%E5%B0%BA%E5%AF%B8%E6%9B%B4%E6%94%B9%E4%B8%BA%E8%A3%85%E9%A5%B0.png)
我如何像下图一样更改距离尺寸。在 \dim={add dim, distance} 中有两个参数,我想添加 1 个参数来表示尺寸距离,如图片 1 所示,并旋转图片 2 中的文本。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations,decorations.markings,decorations.text}
\begin{document}
\pgfkeys{/pgf/decoration/.cd,
distance/.initial=10pt,
distance2/ .initial=10pt
}
\pgfdeclaredecoration{add dim}{final}{
\state{final}{%
\pgfmathsetmacro{\dist}{45pt*\pgfkeysvalueof{/pgf/decoration/distance}
/abs(\pgfkeysvalueof{/pgf/decoration/distance})}
\pgfpathmoveto{\pgfpoint{0pt}{0pt}}
\pgfpathlineto{\pgfpoint{0pt}{\dist}}
\pgfpathmoveto{\pgfpoint{\pgfdecoratedpathlength}{0pt}}
\pgfpathlineto{\pgfpoint{(\pgfdecoratedpathlength}{\dist}}
\pgfsetarrowsstart{latex}
\pgfsetarrowsend{latex}
\pgfpathmoveto{\pgfpoint{0pt}{\dist}}
\pgfpathlineto{\pgfpoint{\pgfdecoratedpathlength}{\dist}}
\pgfusepath{stroke}
\pgfpathmoveto{\pgfpoint{0pt}{0pt}}
\pgfpathlineto{\pgfpoint{\pgfdecoratedpathlength}{0pt}}
}}
\tikzset{dim/.style args={#1,#2,#3}{decoration={add dim,distance=#2},
decorate,
postaction={decorate,decoration={text along path,
raise=#2,
text align={align=center},
text={#1}}}}}
\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (4,2);
\draw[dim={35 cm,34pt,34pt}] (A) -- (B);
\draw[fill=gray] (A) circle(2pt);
\draw[fill=gray] (B) circle(2pt);
\end{tikzpicture}
\end{document}
答案1
您需要稍微改造一下代码。距离由第二个参数 #2 给出
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations,decorations.markings,decorations.text}
\begin{document}
\pgfkeys{/pgf/decoration/.cd, distance/.initial = 10pt}
\pgfdeclaredecoration{add dim}{final}{
\state{final}{%
\pgfmathsetmacro{\dist}{\pgfkeysvalueof{/pgf/decoration/distance}}
\pgfpathmoveto{\pgfpoint{0pt}{0pt}}
\pgfpathlineto{\pgfpoint{0pt}{1.2*\dist}}
\pgfpathmoveto{\pgfpoint{\pgfdecoratedpathlength}{0pt}}
\pgfpathlineto{\pgfpoint{(\pgfdecoratedpathlength}{1.2*\dist}}
\pgfsetarrowsstart{latex}
\pgfsetarrowsend{latex}
\pgfpathmoveto{\pgfpoint{0pt}{\dist}}
\pgfpathlineto{\pgfpoint{\pgfdecoratedpathlength}{\dist}}
\pgfusepath{stroke}
\pgfpathmoveto{\pgfpoint{0pt}{0pt}}
\pgfpathlineto{\pgfpoint{\pgfdecoratedpathlength}{0pt}}
}}
\tikzset{
dim/.style args={#1,#2,#3}{%
decoration = {add dim,distance=\ifx&0pt\else#2\fi},
decorate,
postaction = {%
decorate,
decoration={%
raise=#2,
markings,
mark=at position .5 with {%
\node[inner sep=0pt,
font=\footnotesize,
fill=white,
#3] at (0,0) {#1};}
}
}
},
dim/.default={,0pt,}
}
\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (4,2);
\draw[dim={35 cm,10pt,}] (A) -- (B);
\draw[fill=gray] (A) circle(2pt);
\draw[fill=gray] (B) circle(2pt);
\end{tikzpicture}
\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (4,2);
\draw[dim={35 cm,20pt,transform shape}] (A) -- (B);
\draw[fill=gray] (A) circle(2pt);
\draw[fill=gray] (B) circle(2pt);
\end{tikzpicture}
\end{document}