如何将节点文本位置设置为自定义形状的正确位置

如何将节点文本位置设置为自定义形状的正确位置

我正在摆弄 pgfdeclareshape 并得到了一个我喜欢的形状,但如何将文本放在正确的位置?这里它位于边界框的中心,我希望 x 位置位于矩形区域内的左侧 + 0.375*width 中心。

在此处输入图片描述

\documentclass[tikz,border=2pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{shadows, positioning}

\makeatletter
\pgfdeclareshape{arrowlabel}
{
  \inheritsavedanchors[from=rectangle] % this is nearly a rectangle
  \inheritanchorborder[from=rectangle]
  \inheritanchor[from=rectangle]{north}
  \inheritanchor[from=rectangle]{north west}
  \inheritanchor[from=rectangle]{north east}
  \inheritanchor[from=rectangle]{center}
  \inheritanchor[from=rectangle]{west}
  \inheritanchor[from=rectangle]{east}
  \inheritanchor[from=rectangle]{mid}
  \inheritanchor[from=rectangle]{mid west}
  \inheritanchor[from=rectangle]{mid east}
  \inheritanchor[from=rectangle]{base}
  \inheritanchor[from=rectangle]{base west}
  \inheritanchor[from=rectangle]{base east}
  \inheritanchor[from=rectangle]{south}
  \inheritanchor[from=rectangle]{south west}
  \inheritanchor[from=rectangle]{south east}
  \backgroundpath{%
 % store lower right in xa/ya and upper right in xb/yb
    \southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
    \northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y
    \pgfmathsetlengthmacro\w{\pgf@xb-\pgf@xa}
    \pgfmathsetlengthmacro\h{\pgf@yb-\pgf@ya}
    \pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@ya}}
    \pgfpathlineto{\pgfpoint{\pgf@xa+0.75*\w}{\pgf@ya}}
    \pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@ya+0.5*\h}}
    \pgfpathlineto{\pgfpoint{\pgf@xa+0.75*\w}{\pgf@yb}}    
    \pgfpathlineto{\pgfpoint{\pgf@xa}{\pgf@yb}} 
    \pgfpathclose
  }
}
\makeatother

\begin{document}
\begin{tikzpicture}[every node/.style={font=\sffamily},
  >=latex,
  blockcolors/.style={
    thick,draw=black,
    top color=white,
    bottom color=black!10,
    font=\sffamily\small
   },
   block/.style={rectangle, minimum size=6mm, minimum height=10mm, minimum width=12mm,
      blockcolors, drop shadow
     }
   ]
   
\node[block, draw, label={below:block1}](block1) at (0,0) {};
\node[block, draw, right=of block1, label={below:block2}](block2) {};
\node[block, arrowlabel, draw, left=of block1](in1) {$y$};
\draw[->] (in1) -- (block1);
\draw[->] (block1) -- (block2);

\end{tikzpicture}
\end{document}

答案1

我又摸索了另一种方法,\pgfdeclareshape但不确定是否有更好/更简单的方法。

我使用以下命令将\pgfpointorigin宽度*0.125向左移动。

  \anchor{text}{
     \southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
     \northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y
     \pgfmathsetlengthmacro\pgf@dx{0.125*(\pgf@xb-\pgf@xa)}
     \pgfpointorigin
     \advance\pgf@x by -\pgf@dx%
  }%

下面的完整示例。

在此处输入图片描述

\documentclass[tikz,border=2pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{shadows, positioning}

\makeatletter
\pgfdeclareshape{arrowlabel}
{
  \inheritsavedanchors[from=rectangle] % this is nearly a rectangle
  \inheritanchorborder[from=rectangle]
  \inheritanchor[from=rectangle]{north}
  \inheritanchor[from=rectangle]{north west}
  \inheritanchor[from=rectangle]{north east}
  \inheritanchor[from=rectangle]{center}
  

  \inheritanchor[from=rectangle]{west}
  \inheritanchor[from=rectangle]{east}
  \inheritanchor[from=rectangle]{mid}
  \inheritanchor[from=rectangle]{mid west}
  \inheritanchor[from=rectangle]{mid east}
  \inheritanchor[from=rectangle]{base}
  \inheritanchor[from=rectangle]{base west}
  \inheritanchor[from=rectangle]{base east}
  \inheritanchor[from=rectangle]{south}
  \inheritanchor[from=rectangle]{south west}
  \inheritanchor[from=rectangle]{south east}
  \anchor{text}{
     \southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
     \northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y
     \pgfmathsetlengthmacro\pgf@dx{0.125*(\pgf@xb-\pgf@xa)}
     \pgfpointorigin
     \advance\pgf@x by -\pgf@dx%
  }%
   \backgroundpath{%
 % store lower right in xa/ya and upper right in xb/yb
    \southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
    \northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y
    \pgfmathsetlengthmacro\w{\pgf@xb-\pgf@xa}
    \pgfmathsetlengthmacro\h{\pgf@yb-\pgf@ya}
    \pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@ya}}
    \pgfpathlineto{\pgfpoint{\pgf@xa+0.75*\w}{\pgf@ya}}
    \pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@ya+0.5*\h}}
    \pgfpathlineto{\pgfpoint{\pgf@xa+0.75*\w}{\pgf@yb}}    
    \pgfpathlineto{\pgfpoint{\pgf@xa}{\pgf@yb}} 
    \pgfpathclose

    % just for debugging
    \pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@ya}}
    \pgfpathlineto{\pgfpoint{\pgf@xa+0.75*\w}{\pgf@yb}}
    \pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@yb}}
    \pgfpathlineto{\pgfpoint{\pgf@xa+0.75*\w}{\pgf@ya}}

  }
}
\makeatother

\begin{document}
\begin{tikzpicture}[every node/.style={font=\sffamily},
  >=latex,
  blockcolors/.style={
    thick,draw=black,
    top color=white,
    bottom color=black!10,
    font=\sffamily\small
   },
   block/.style={rectangle, minimum size=6mm, minimum height=10mm, minimum width=12mm,
      blockcolors, drop shadow
     }
   ]
   
\node[block, draw, label={below:block1}](block1) at (0,0) {};
\node[block, draw, right=of block1, label={below:block2}](block2) {};
\node[block, arrowlabel, draw, left=of block1](in1) {$y$};
\draw[->] (in1) -- (block1);
\draw[->] (block1) -- (block2);
\node[block, arrowlabel, draw, left=of in1, minimum width=0.2cm](in2) {$y$};

\end{tikzpicture}
\end{document}

相关内容