3

I am drawing several dozen diagrams with TikZ that will be using equilateral triangles. My current setup is using the regular polygon construction. The anchor names corner 1, corner 2, and corner 3 are not very semantic for my purposes, so I would like to make a global declaration providing them new names. This is how I am currently declaring my triangular nodes:

\tikzset{
delta/.style={
regular polygon, regular polygon sides=3, minimum size=0.4cm, inner sep=0,
outer sep=0.025em, draw=black, very thick, fill=green!10}
}

I know that (node.210) will give me the same anchor position as (node.corner 2), but I'd like to be able to do something like (node.left copy) to give the same anchor position, and (node.right copy) in the place of (node.corner 3) or (node.330).
From the manual, it seems I ought to be able to do something like \anchor{left copy}{\corner2} if \corner2 is the name for that saved anchor, but I don't know where that code would fit into what I have, and I don't know if \corner2 is the correct name.
The following code is functional, but customizing the anchor names will make it easier to follow what's going on in a more complicated diagram.

\begin{tikzpicture}[-, thick]
\node[delta] (dub) {};
\node (copy1) [below left of=dub] {};
\node (copy2) [below right of=dub] {};
\node (input) [above of=dub] {};
\draw (input) -- (dub) (dub.corner 2) -- (copy1);
\draw (dub.corner 3) -- (copy2);
\end{tikzpicture}

The best workaround I have found so far is rotating the border in the style definition, shape border rotate=120, which lowers each of the used corner numbers by 1. Even if I end up using this hack here, rotation is not a panacea; I still want to know how to make new aliases for anchors in predefined shapes.

A comment on another question (Creating pseudo anchors in TikZ drawing) indicated \pgfdeclaregenericanchor is an undocumented macro that can be used to access saved anchors of predefined shapes, but that's where I hit a dead end.

7

The command \pgfdeclareanchoralias{<shape>}{<anchor>}{<alias>} defined as

\newcommand*\pgfdeclareanchoralias[3]{%
    \expandafter\def\csname pgf@anchor@#1@#3\expandafter\endcsname
            \expandafter{\csname pgf@anchor@#1@#2\endcsname}}

can be used to copy the definition of the <anchor> anchor of the <shape> shape under the new name <alias>, so in your case

\pgfdeclareanchoralias{regular polygon}{corner 2}{left copy}
\pgfdeclareanchoralias{regular polygon}{corner 3}{right copy}

should do.

Code

\documentclass[tikz]{standalone}
\usetikzlibrary{shapes.geometric, positioning}
\newcommand*\pgfdeclareanchoralias[3]{%
    \expandafter\def\csname pgf@anchor@#1@#3\expandafter\endcsname
            \expandafter{\csname pgf@anchor@#1@#2\endcsname}}

\pgfdeclareanchoralias{regular polygon}{corner 2}{left copy}
\pgfdeclareanchoralias{regular polygon}{corner 3}{right copy}
\tikzset{
  delta/.style={
    regular polygon, regular polygon sides=3, minimum size=0.4cm, inner sep=0,
    outer sep=0.025em, draw=black, very thick, fill=green!10}
  }
\begin{document}
\begin{tikzpicture}[thick, node distance=.25cm]
\node[delta] (dub) {};
\coordinate [ below left=of dub.left copy]  (copy1);
\coordinate [below right=of dub.right copy] (copy2);
\coordinate [      above=of dub]            (input);
\path (input)          edge (dub)
      (dub.left copy)  edge (copy1)
      (dub.right copy) edge (copy2);
\end{tikzpicture}
\end{document}
  • I tried putting both code blocks in my preamble immediately after \usepackage{tikz} and \usetikzlibrary{shapes.geometric}. I also tried adding it in after the style declaration. That part compiled, but in both cases (dub.left copy) threw an error: Unknown function `left' (in 'left copy'). There was a similar error for right copy. – Jason Nov 16 '13 at 20:02
  • @Jason Yeah, I just checked, the corner <i> anchors are created on-the-fly when they are needed (so in your case when the delta node is created). I’ll change the solution to a more robust version. – Qrrbrbirlbel Nov 16 '13 at 21:12
  • In retrospect, it makes sense that the corners are not saved anchors. This alias maker for anchors, as modified, is just what the doctor ordered. – Jason Nov 17 '13 at 7:21

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.