我尝试制作一个精细的宏来显示技术图形内线条的尺寸。这个问题出现在几个问题和答案中,但我想添加一些改进。下一个代码基于几个代码,实际上我有这个:
\documentclass{scrartcl}
\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}{2*\dist}}
\pgfpathmoveto{\pgfpoint{\pgfdecoratedpathlength}{0pt}}
\pgfpathlineto{\pgfpoint{(\pgfdecoratedpathlength}{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=#2},
decorate,
postaction = {%
decorate,
decoration={%
markings,
mark=at position .5 with {\node[inner sep=0pt,
font=\footnotesize,
fill=white,
#3] at (0,0) {#1};}
}}}}
\tikzset{dim sloped/.style args={#1,#2}{%
decoration = {add dim,distance=#2},
decorate,
postaction = {%
decorate,
decoration={%
text along path,
raise = 3pt+\pgfkeysvalueof{/pgf/decoration/distance},
text align = {align=center},
text = {#1}
}}}}
\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (4,2);
\coordinate (C) at (8,-2);
\draw[dim={5 cm,10pt,above=12pt}] (A) -- (B);
\draw[dim sloped={7 cm,10pt}] (B) -- (C);
\draw[fill=gray] (A) circle(1pt);
\draw[fill=gray] (B) circle(1pt);
\draw[fill=gray] (C) circle(1pt);
\end{tikzpicture}
\end{document}
结果很好,这就是我想要的。也许可以添加一些样式来获得更友好的效果,但现在的问题是:
1)dim
可以书写dim={,10pt,}
,但在这种情况下,主行上会出现一个小的白色空白。我可以书写, \draw[dim={,10pt,above=12pt}]
但这并不好笑
2) 如果我可以写入dim={,10pt,}
,我就不能写入\draw[dim={5 cm, ,above=12pt}]
。我收到错误“缺少一个数字”。distance
在这种情况下如何使用 的默认值或初始值?
3) 如果可以写的话,我认为写或者也许\draw[dim={,,}]
会更好 。也许 需要类似这样的内容?\draw[dim={}]
\draw[dim=]
\draw[dim default]
4) 我希望只有一个键,dim
而不是两个键,dim sloped
但我尝试修改其中一个键,但没有成功。这并不重要,因为混合是一件非常糟糕的事情。倾斜或不倾斜,键的用户需要做出选择!但我想知道是否有解决方案。
答案1
- 如果没有文本,您可以将填充设置为空:
fill=\ifx&none\else white\fi
。 - 类似地,如果没有指定距离,则 可以定义为
distance
:。0pt
distance=\ifx&0pt\else#2\fi
- 为了能够
dim
在不指定任何选项的情况下进行调用,您可以设置.default
:dim/.default={,0pt,}
。 - 您不需要使用
text along path
沿直线倾斜的文本的装饰:您只需提供transform shape
给装饰的节点即可markings
。
\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}{2*\dist}}
\pgfpathmoveto{\pgfpoint{\pgfdecoratedpathlength}{0pt}}
\pgfpathlineto{\pgfpoint{(\pgfdecoratedpathlength}{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=\ifx&0pt\else#2\fi,
markings,
mark=at position .5 with {\node[inner sep=0pt,
font=\footnotesize,
fill=\ifx&none\else white\fi,
#3] at (0,0) {#1};}
}
}
},
dim/.default={,0pt,}
}
\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (4,2);
\coordinate (C) at (8,-2);
\draw[dim={5cm,7pt,}] (A) -- (B);
\draw[dim={7cm,10pt,transform shape}] (B) -- (C);
\draw[dim={X,,}] (A) -- (C);
\draw[fill=gray] (A) circle(1pt);
\draw[fill=gray] (B) circle(1pt);
\draw[fill=gray] (C) circle(1pt);
\end{tikzpicture}
\end{document}