2

Say I have a simple rectangular node I wish to lengthen some sides of, such that they extend further from the corners. Simple, here is my attempt:

\documentclass[tikz,border=1mm]{standalone}

\begin{document}

\begin{tikzpicture}
    \node[draw] (x) {foo};
    \draw (x.north west) -- ++(0, +1mm);
\end{tikzpicture}

\end{document}

The result:

Result of MWE

I see what the problem is: x.north west is exactly at the corner of the shape and draw draws the line from this corner in such a way that the drawn path protrudes by pgflinewidth/2 in both directions along the trajectory I specified (if that makes sense; sorry, it is rather hard to explain).

My question is, how do I draw the line so that it looks like it extends the original side of the x node?

  • I suppose the x.north east anchor is where it is, there is no other anchor that just happens to be where I need it
  • One thing I thought of is making the draw command \draw[xshift=.5\pgflinewidth], but it did not cause any change and feels a bit hacky

2 Answers 2

5

Try

\documentclass[tikz,border=1mm]{standalone}

\begin{document}

\begin{tikzpicture}
    \node[draw,outer sep=0pt] (x) {foo};
    \draw (x.north west) -- ++(0, +1mm);
\end{tikzpicture}

\end{document}

enter image description here

Anchor of nodes is on outside of node's borders, i.e. for outer sep moved from middle of borders lines. This you can change by settings outside sep=0pt

1
  • This is perfect, thank you! One can confirm what is happening by adding eg \draw[red,line width=.1pt] plot[mark=x] coordinates{(x.north west)};, to visualize where the anchor is.
    – bp99
    Mar 5 at 10:54
2

Improving on my attempted ‘hacky’ solution:

As far as I understand, xshift does not apply to points referenced by nodes, in this case (x.north west). Instead, one can use transform canvas, like this:

\documentclass[tikz,border=1mm]{standalone}

\begin{document}

\begin{tikzpicture}
    \node[draw] (x) {foo};
    \draw[transform canvas={xshift=.5\pgflinewidth}] (x.north west) -- ++(0, +1mm);
\end{tikzpicture}

\end{document}

The result:

Result of LaTeX code

This ultimately solves my problem, but does not feel quite right. Is there a better way to do this? ☺

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.