4

I'm trying to create a custom TikZ shape consisting of a rectangle with small arrows on the border (representing signal input/output ports).

For the most part I can inherit from rectangle but I'm having some problems creating the input/output port arrows. Ideally, I would define anchors at the port locations and then use those to draw arrows in the backgroundpath section. Problem is, backgroundpath can't use normal anchors.

One option is to make the port locations saved anchors, but then I don't have access to \northeast and \southwest. Without those, I'm having troubles getting the anchors to be on the actual border.

The other option is just to duplicate the steps it takes to get to each anchor but I don't really like that.

Here is an example of what I would like to do:

\makeatletter
\pgfdeclareshape{MyShape}
{
    \inheritsavedanchors[from=rectangle]
    \inheritanchorborder[from=rectangle]

    \inheritanchor[from=rectangle]{center}
    \inheritanchor[from=rectangle]{south}
    \inheritanchor[from=rectangle]{west}
    \inheritanchor[from=rectangle]{north}
    \inheritanchor[from=rectangle]{east}
    % etc.

    \inheritbeforebackgroundpath[from=rectangle]
    \inheritforegroundpath[from=rectangle]
    \inheritbeforeforegroundpath[from=rectangle]

    \anchor{inputA}{%
        \southwest 
        \pgf@y = \advance \pgf@y by 3mm
    }

    \backgroundpath
    {
        \pgfsetarrowsend{Latex[length=3pt]}
        \pgfpathmoveto{\inputA}
        \pgfpathlineto{\northeast}
        \pgfusepath{stroke}
    }
}
\makeatother

Note: I know that code won't work because inputA is just a normal anchor;

3

First, you need \savedanchor for inputA no matter what but I think that's not a problem for you. One reason why relative referral to other anchors are because they have to be completely collected which is quite cumbersome at the very least since the box sizes are the only info that is available. Instead I would do the following

\documentclass[tikz]{standalone}
\usetikzlibrary{arrows.meta}
\makeatletter
\pgfdeclareshape{MyShape}
{
    \inheritsavedanchors[from=rectangle]
    \inheritanchorborder[from=rectangle]
    \inheritanchor[from=rectangle]{center}
    \inheritanchor[from=rectangle]{south}
    \inheritanchor[from=rectangle]{west}
    \inheritanchor[from=rectangle]{north}
    \inheritanchor[from=rectangle]{east}
    % etc.
    \inheritbeforebackgroundpath[from=rectangle]
    \inheritforegroundpath[from=rectangle]
    \inheritbeforeforegroundpath[from=rectangle]
    %
    \backgroundpath
    {%
        \pgfsetarrowsend{Latex[length=3pt]}%
        \southwest\advance\pgf@y by3mm\relax\pgfpathmoveto{\pgfpoint{\pgf@x}{\pgf@y}}%
        \northeast\pgfpathlineto{\pgfpoint{\pgf@x}{\pgf@y}}%
        \pgfusepath{stroke}%
    }%
}
\makeatother

\begin{document}
\begin{tikzpicture}
\draw[style=help lines] (-2,-2) grid[step=1] (2,2);
\node[MyShape,minimum size=1cm,text=red]{A B C};
\node[draw,minimum size=1cm,text=red]{A B C};
\end{tikzpicture}
\end{document}

enter image description here

  • Unfortunately, to be useful I need the anchors available as well to hook up connections so you answer is basically the second option I was talking about. – ryan0270 Apr 17 '14 at 13:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.