pgfkeys 的默认值问题

pgfkeys 的默认值问题

我尝试制作一个精细的宏来显示技术图形内线条的尺寸。这个问题出现在几个问题和答案中,但我想添加一些改进。下一个代码基于几个代码,实际上我有这个:

\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

  1. 如果没有文本,您可以将填充设置为空:fill=\ifx&#1&none\else white\fi
  2. 类似地,如果没有指定距离,则 可以定义为distance:。0ptdistance=\ifx&#2&0pt\else#2\fi
  3. 为了能够dim在不指定任何选项的情况下进行调用,您可以设置.defaultdim/.default={,0pt,}
  4. 您不需要使用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&#2&0pt\else#2\fi},
                decorate,
                postaction = {%
                   decorate,
                   decoration={%
                        raise=\ifx&#2&0pt\else#2\fi,
                        markings,
                        mark=at position .5 with {\node[inner sep=0pt,
                                                        font=\footnotesize,
                                                        fill=\ifx&#1&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} 

相关内容