I decided that I don't like the behaviour of the label functionality of TikZ. I want to get labels that are positioned using the anchor opposite the specified label angle. So for example if the label would be stated in the node options list as [..., label = {210: my label}, ...] then the label should be positioned at angle 210 using its (the label's) 30 anchor (210 +/- 180 is 30 modulo 360). (The TikZ label positioning logic doesn't do this. Instead (per the 3.00 manual) it chooses one of the 8 anchors north, north east, east, ..., north west for the label, dependent upon the angle.)

Since the labelled nodes are going to be created inside a macro and not by actually typing TikZ code directly, it seemed that it would be simple enough to just create the label node myself without actually using the "label" functionality. However, one still has to get the label distance right. And I want the label distance to respond to changes to the TikZ key label distance, just like real TikZ labels do.

However, there doesn't seem to be a /tikz/ or /pgf/ key that stores the label distance. There's /tikz/label distance, but that's seemingly used for setting the label distance rather than storing it; if you try to read from it using \pgfkeysgetvalue, you get \relax. Looking through the code for TikZ, it seems that the current label distance is stored in a macro called \tikz@label@distance. So I tried code that copies the value of this macro and uses it to position the ersatz label, and it appears to work correctly.

My question is, is this the way that I "should" be determining the label distance? By this I mean that what I'm doing here is looking "under the hood" of Tikz/PGF and tinkering with its internals. If a later version of TikZ/PGF renames the \tikz@label@distance macro or starts using, say, a PGFkeys key to store it, my code will break. Is there a better way?

  • By the way, I know that there are a few questions already asking how to precisely position TikZ labels. My question here is really about what the problems are with this method of "spying on" the internal workings of PGF/TikZ. The fact that it relates to labels is somewhat incidental. – Hammerite Jan 1 '14 at 16:32
  • One alternative approach that occurred to me is to actually draw a node and label it, giving both the node and the label a name, and then compute the distance between the two relevant anchors. Obviously you don't want to actually draw anything, so you would use {} or {~} for the node and label texts and set draw = none, fill = none. Also you don't want to influence the bounding box, so you'd have to set an empty bounding box for the scope (I assume this can be done). The main problem with doing it this way is that it would presumably be quite slow. – Hammerite Jan 2 '14 at 17:27

Your Answer

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

Browse other questions tagged or ask your own question.