如何重用 pgfpointadd

如何重用 pgfpointadd

我正在尝试通过使用如下方法来改善我的xfrm形状以修复两个问题(对角线超出角落;不使用矩形边界的线条样式)\pgfdeclareshape在命令后附加中使用 \tikzlastnode 的线型和颜色

我的问题是:我如何重复使用\pgfpointadd命令?

绘制矩形轮廓的部分定义是(参考:https://svn.ssec.wisc.edu/repos/geoffc/LaTeX/beamerposter_UW-SSEC/pgfmoduleshapes.code.tex) 是

    \pgfpathrectanglecorners
    {\pgfpointadd{\southwest}{\pgfpoint{\pgfkeysvalueof{/pgf/outer xsep}}{\pgfkeysvalueof{/pgf/outer ysep}}}}
    {\pgfpointadd{\northeast}{\pgfpointscale{-1}{\pgfpoint{\pgfkeysvalueof{/pgf/outer xsep}}{\pgfkeysvalueof{/pgf/outer ysep}}}}}

我想补充一下

    \pgfmoveto{\pgfpointadd{\southwest}{\pgfpoint{\pgfkeysvalueof{/pgf/outer xsep}}{\pgfkeysvalueof{/pgf/outer ysep}}}}
    \pgflineto{{\pgfpointadd{\northeast}{\pgfpointscale{-1}{\pgfpoint{\pgfkeysvalueof{/pgf/outer xsep}}{\pgfkeysvalueof{/pgf/outer ysep}}}}}

但这是重复相同的坐标,我想知道如何在 pgf 中重用计算的坐标。

我尝试使用

  \def\swofs{\pgfpointadd{\southwest}{\pgfpoint{\pgfkeysvalueof{/pgf/outer xsep}}{\pgfkeysvalueof{/pgf/outer ysep}}}}
  \def\neofs{\pgfpointadd{\northeast}{\pgfpointscale{-1}{\pgfpoint{\pgfkeysvalueof{/pgf/outer xsep}}{\pgfkeysvalueof{/pgf/outer ysep}}}}
  \backgroundpath{
    \pgfpathrectanglecorners{\swofs}{\neofs}
    \pgfmoveto{\swofs}\pgflineto{\neofs}    
  }

但那不起作用。(我没有收到错误,但它似乎没有定义任何形状。)

完整示例:

在此处输入图片描述

\documentclass[border=1mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes,shadows,arrows,positioning,fit,backgrounds,calc}
\pgfdeclareshape{xfrmshape}{
  \inheritsavedanchors[from={rectangle}]
  \inheritbackgroundpath[from={rectangle}]
  \inheritanchorborder[from={rectangle}]
  \foreach \x in {center,north east,north west,north,south,south east,south west,east,west}{
    \inheritanchor[from={rectangle}]{\x}
  }
  \backgroundpath{
    \pgfpathrectanglecorners
    {\pgfpointadd{\southwest}{\pgfpoint{\pgfkeysvalueof{/pgf/outer xsep}}{\pgfkeysvalueof{/pgf/outer ysep}}}}
    {\pgfpointadd{\northeast}{\pgfpointscale{-1}{\pgfpoint{\pgfkeysvalueof{/pgf/outer xsep}}{\pgfkeysvalueof{/pgf/outer ysep}}}}}
    \pgfmoveto{\pgfpointadd{\southwest}{\pgfpoint{\pgfkeysvalueof{/pgf/outer xsep}}{\pgfkeysvalueof{/pgf/outer ysep}}}}
    \pgflineto{{\pgfpointadd{\northeast}{\pgfpointscale{-1}{\pgfpoint{\pgfkeysvalueof{/pgf/outer xsep}}{\pgfkeysvalueof{/pgf/outer ysep}}}}}
  }
}
\begin{document}
\begin{tikzpicture}[node distance=5mm, 
       blockcolors/.style={
        % The rest
        thick,draw=black,
        top color=white,
        bottom color=black!10,
        font=\sffamily\small
    },
    block/.style={
        % The shape:
        rectangle, minimum size=6mm, minimum height=10mm, minimum width=12mm,
        node distance=5mm,
        blockcolors,
        drop shadow
    },
    xfrm/.style={block,
      append after command={
             \pgfextra{\let\lastnode\tikzlastnode} 
            (\lastnode.south west) edge [-, thick, line join=round] (\lastnode.north east)
          }
    },      
    every node/.style={
        font=\sffamily\small
    }
    ]

\node (xfrm1) [xfrm, label={below:xfrm}] {};
\node (xfrm2) [block, xfrmshape, label={below:xfrmshape}] at (1.6, 0) {};
\end{tikzpicture}
\end{document}

答案1

实现你想要的最简单的方法之一可能是使用

xfrm/.style={block, path picture={
\draw (path picture bounding box.south west) -- (path picture bounding box.north east);} }

不过,为了回答你的问题,让我先提一下 TiZ 内部使用辅助维度,例如\pgf@xa等。但是,由于通常需要花费很大力气才能找出其中哪些已被使用,因此您可能只需定义一些(本地)宏:

  \backgroundpath{
    \southwest
    \pgfmathsetmacro{\myxa}{\pgf@x+\pgfkeysvalueof{/pgf/outer xsep}}%
    \pgfmathsetmacro{\myya}{\pgf@y+\pgfkeysvalueof{/pgf/outer xsep}}%
    \northeast
    \pgfmathsetmacro{\myxb}{\pgf@x-\pgfkeysvalueof{/pgf/outer xsep}}%
    \pgfmathsetmacro{\myyb}{\pgf@y-\pgfkeysvalueof{/pgf/outer xsep}}%
    \pgfpathrectanglecorners{\pgfpoint{\myxa pt}{\myya pt}}%
        {\pgfpoint{\myxb pt}{\myyb pt}}%
    \pgfmoveto{\pgfpoint{\myxa pt}{\myya pt}}%
    \pgflineto{\pgfpoint{\myxb pt}{\myyb pt}}%
  }

如您所见,这些宏被使用了两次。这样做比使用维度要慢一些,但我认为除非您生成无数个这样的节点,否则性能上的差异是不可衡量的。

\documentclass[border=1mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{shadows}
\makeatletter
\pgfdeclareshape{xfrmshape}{
  \inheritsavedanchors[from={rectangle}]
  \inheritbackgroundpath[from={rectangle}]
  \inheritanchorborder[from={rectangle}]
  \foreach \x in {center,north east,north west,north,south,south east,south west,east,west}{
    \inheritanchor[from={rectangle}]{\x}
  }
  \backgroundpath{
    \southwest
    \pgfmathsetmacro{\myxa}{\pgf@x+\pgfkeysvalueof{/pgf/outer xsep}}%
    \pgfmathsetmacro{\myya}{\pgf@y+\pgfkeysvalueof{/pgf/outer xsep}}%
    \northeast
    \pgfmathsetmacro{\myxb}{\pgf@x-\pgfkeysvalueof{/pgf/outer xsep}}%
    \pgfmathsetmacro{\myyb}{\pgf@y-\pgfkeysvalueof{/pgf/outer xsep}}%
    \pgfpathrectanglecorners{\pgfpoint{\myxa pt}{\myya pt}}%
        {\pgfpoint{\myxb pt}{\myyb pt}}%
    \pgfmoveto{\pgfpoint{\myxa pt}{\myya pt}}%
    \pgflineto{\pgfpoint{\myxb pt}{\myyb pt}}%
  }
}
\makeatother
\begin{document}
\begin{tikzpicture}[node distance=5mm, 
       blockcolors/.style={
        % The rest
        thick,draw=black,
        top color=white,
        bottom color=black!10,
        font=\sffamily\small
    },
    block/.style={
        % The shape:
        rectangle, minimum size=6mm, minimum height=10mm, minimum width=12mm,
        node distance=5mm,
        blockcolors,
        drop shadow
    },
    xfrm/.style={block,
      path picture={\draw (path picture bounding box.south west)
       -- (path picture bounding box.north east);}
    },      
    every node/.style={
        font=\sffamily\small
    }
    ]

\node (xfrm1) [xfrm, label={below:xfrm}] {};
\node (xfrm2) [block, xfrmshape, label={below:xfrmshape}] at (1.6, 0) {};
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容