On page 628 of the TikZ manual, it describes how to code the text anchor for a custom shape so that the label text is placed with its centre at the origin. I've done this to create a simple shape, which draws a square of side length 10pt. The text label should then be centered at the origin.

However, it doesn't seem to work correctly. Here's a screenshot:enter image description here

On the left is a node defined with my new shape, with label \fbox{$f$}. On the right is a standard TikZ node with the same label, created with the draw option and inner sep=1pt. Both nodes are positioned with the same y coordinate - but clearly, the label for my shape hasn't been placed at the same height, and it is clearly not correctly centred in its box.

Question: how can I get the text label for a custom shape properly centred at the origin?

Here's the code.

\node [myshape] at (0,0) {\fbox{$f$}};
\node [draw, inner sep=1pt, red] at (12pt,0) {\fbox{$f$}};
  • 1
    I can't check the proper syntax right now but I guess you are ignoring the depth dependence (Try it with different characters like h or g. Add \advance\pgf@y by -.5\dp\pgfnodeparttextbox% to the savedanchor part (just like in the manual). This centers but does not solve everything though. You still need to modify the rectangle further. – percusse Jan 28 '12 at 0:26
  • This seems good enough to solve my problem - thanks! If you post this as an answer, I can accept. – Jamie Vicary Jan 28 '12 at 0:53

\newanswer{comment above}

The apparent problem seems to be the calculation of the center that is independent from the text depth. Including the code given in the manual p.626 is enough for Jamie to carry on from there.

The proposed change is

    \advance\pgf@y by -.5\dp\pgfnodeparttextbox

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.