1

How can I go about using a pgfkey to place an anchor if the key is a string?

Previous answers imply that setting anchors based on keys is possible using numerical keys (like this question on Creating Tikz anchors based on pgfkey value) or boolean ("if") keys (like this question on How to define an anchor with location depending on a parameter) but I haven't had any luck finding similar examples which use strings or in modifying these examples to work with strings.

As a simple example, in the (nonfunctional) code below I'd like to be able to use the elevation key to change the location of the north east anchor (it's my understanding that the ifthen package is considered obsolete but I've used it here because I thought it most clearly conveyed the behavior I was after):

\documentclass[border=10pt]{standalone}
\usepackage{tikz, ifthen}
\makeatletter
\pgfkeys{/tikz/elevation/.initial=low}
\pgfdeclareshape{myLine}{   
    \savedanchor{\anchorA}{
        \pgf@x=3cm
        \ifthenelse{\equal{low}{\pgfkeysvalueof{/tikz/elevation}}}
            {\pgf@y=1cm}{}
        \ifthenelse{\equal{medium}{\pgfkeysvalueof{/tikz/elevation}}}
            {\pgf@y=3cm}{}
        \ifthenelse{\equal{high}{\pgfkeysvalueof{/tikz/elevation}}}
            {\pgf@y=5cm}{}
    }   
    \anchor{center}{\pgfpointorigin}
    \anchor{top right}{\anchorA}
    
    \backgroundpath{
        \pgfpathmoveto{\pgfpointorigin}
        \pgfpathlineto{\anchorA}
        \pgfusepath{stroke}
    }
}
\makeatother
\begin{document}
    \begin{tikzpicture}
        \draw [help lines, dashed, gray] (0,0) grid (5,5);
        \node [myLine, elevation=value] (L) {};
        \draw [red] (L.top right) circle (0.2cm);
    \end{tikzpicture}
\end{document}

The resulting (desired) behavior based on keys provided to \node [myLine, elevation=value] is shown in the images below.

enter image description here

3
  • Maybe I am not getting the problem (BTW: welcome!) but it's working here...
    – Rmano
    Commented Jun 2, 2021 at 6:50
  • Same here. Maybe the OP didn't replace value with low / medium / high in the document body?
    – frougon
    Commented Jun 2, 2021 at 7:37
  • I think you're both right! I have no idea how but in the process of trying to make a "broken" example I appear to have fixed it. I'll close it.
    – Aaron
    Commented Jun 2, 2021 at 18:10

1 Answer 1

0

The sample code in fact does do what I wanted it to do. Apparently in the process of trying to create a "broken" answer I solved it.

Thanks to Rmano and frougon for pointing out my oversight.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .