6

I'm still struggling to position labels with respect to their anchors in a way such that the anchors are correctly transformed by negative scaling (mirroring). In thread tikz: label positioning that is robust with respect to negative scaling? the problem seemed to be solved. The new anchors work with (negative) scaling and rotation, but break under shifts. Any idea how to modify the code for anchor at angle to respect shifting?

In the test images below, the bounding boxes of the labels as well as the anchor is shown. The first image is the original, untransformed version. The second image shows that mirroring at the y-axis works as expected: all anchors are mirrored correctly. The third and fourth image show that this is no longer true when shifting the image. The anchor seems to be where the line from the center following the shift vector meets the bounding box. Correctly, the third and fourth image should be identical to the first one.

\documentclass[tikz]{standalone}
\makeatletter
\tikzset
 {anchor at angle/.code=
   {\pgfpointtransformed{\pgfpointpolar{#1}{1pt}}
    \pgfmathanglebetweenpoints{\pgfqpoint{-\pgf@x}{-\pgf@y}}{\pgfpointorigin}
    \def\tikz@anchor{\pgfmathresult}
   }
 }
\newcommand\test[1]%
 {\tikz[#1] \draw (0,0) node{#1}
  \foreach \A/\L in {0/a,30/b,60/c,90/d,120/e,150/f,180/g,210/h,240/i,270/j,300/k,330/l}
  { (\A:1.5cm) node[circle,draw,inner sep=0,minimum width=5mm] {} % circle
   +(\A:  4mm) node[anchor at angle=\A+180,inner sep=0,draw] {\L} % label
               node[circle,inner sep=0,fill=black,minimum width=0.5mm] {} % anchor marker
  };
 }
\begin{document}
\test{}
\test{xscale=-1}
\test{shift={(1,1)}}
\test{shift={(1,0)}}
\end{document}

What I need it for: I construct graphs that I want to be able to flip upside down or left-right by need. The problem is to attach labels to the nodes whose positions (anchors) will also flip. Standard tikz labels are unaffected by negative scaling and will end up in the wrong position after flipping. As it happens, these graphs have some repetitive structures that I construct by shifting sub-graphs to the right position. This is the point were label positioning currently fails. enter image description here

gernot
  • 49,614
  • can you give your actual problem ? Because you shouldn't need this at all if you only have circles or designated repeating patterns – percusse Aug 11 '16 at 19:42
  • @percusse My test graphs are misleading, my aim is not to draw some repeating patterns. I have added a description of my setting as well as an example of a real graph. – gernot Aug 11 '16 at 21:29
  • How are you intending for the shift to behave, for your circle figure? – Steven B. Segletes Aug 12 '16 at 14:24
  • 1
    @Steven The shifted circle figure should look identical to the original one. Admittedly it doesn't make sense to shift the whole tikz picture, it is just meant to show the problem. The real use is to have several copies of a sub-graph, each shifted by a different amount. In the graph above, there are four types of parallelograms that repeat several times. The label positions are usually different for each copy. As long as the shift is (0,0) the label anchor is the point of the bounding box closest to the node. As soon as a shift happens, the anchor is at a seemingly random position. – gernot Aug 12 '16 at 15:10
  • I've modified my example to show the bounding box of the labels as well as the position of the anchor. It seems that with a shift, the anchor point is determined by intersecting the box with a line from the center in the direction of the shift vector. – gernot Aug 12 '16 at 15:27
  • 1
    Have you tried using pics for the subgraphs? – cfr Aug 12 '16 at 22:29
  • 1
    Works fine for shift but not for mirroring. – cfr Aug 12 '16 at 22:53
  • Thanks for the pic idea, I've learned again something new. But robustness with respect to mirroring is the more essential requirement. – gernot Aug 13 '16 at 08:50
  • My solution for the moment is to replace shifting by explicit coordinate calculations; in my case this does not require too much rewriting. So, instead of a scope with a shift of (#1,#2) I will replace the relevant positions (,) in the subgraph by (#1+,#2+); the subgraphs are macros anyway. Not as elegant as a general shift, but it works. – gernot Aug 13 '16 at 09:03

2 Answers2

1

It looks to me like the issue is that \pgfpointorigin yields the unshifted origin. I don't know enough about pgf to tell you how to shift it without also rotating it.

However, I do have an approximate solution hack: Since the code just tries to get the real angle of the transformed label angle, you can make this difference negligible (as long as your image is about page-sized) by replacing the 1pt offset to \pgfpointpolar by a very large offset (like 500cm).

Emma
  • 3,453
  • 12
  • 20
1

@Emma said the correct answer: the origin is the origin without transformation. On the other hand, \pgfpointpolar is with transformation.

So a nature solution is to refer to both points with transformation, like the following:

\pgfpointtransformed{\pgfpointpolar{#1}{1pt}}\pgf@xa\pgf@x\pgf@ya\pgf@y
\pgfpointtransformed{\pgfpointpolar{#1}{0pt}}\pgf@xb\pgf@x\pgf@yb\pgf@y
\pgfmathanglebetweenpoints{\pgfqpoint{-\pgf@xa}{-\pgf@ya}}{\pgfqpoint{-\pgf@xb}{-\pgf@yb}}
\def\tikz@anchor{\pgfmathresult}
Symbol 1
  • 36,855