9

I would like to place nodes with given distances relative to each other. For consistence with other drawings these distances have to be very accurate. Node border anchors are placed at the outer borders of the nodes (see pgfmanual 3.0.0, page 702). Therefore, effectively pgflinewidth (or fractions/multiples of it depending on the nodes shape) is added to the given distance (red nodes in the MWE).

A way to counteract this would be to subtract (fractions/multiples) of pgflinewidth (blue nodes in the MWE). Another alternative could be to place the nodes w.r.t. their centers. In this case, one would need to know the nodes' widths beforehand which might not be easy for arbitrary node shapes.

My question: Is there an easy way to have node border anchors centered on the borders?

\documentclass{standalone}

\usepackage{tikz}

\begin{document}

        \begin{tikzpicture}[ultra thick, MyNode/.style={draw, minimum height=1cm}]

                \draw[step=1cm,gray,ultra thin] (-1cm,-3cm) grid (5cm,2cm);

                % default case
                \begin{scope}[red]
                    \path (0,0) node[MyNode, anchor=south west, minimum width=1cm] (n1) {};
                    \path (n1.east) ++(1cm,0) node[MyNode, anchor=west, minimum width=2cm] {};
                \end{scope}

                % supstract line width
                \begin{scope}[blue, shift={(0,-2)}]
                    \path (0,0) ++(-0.5\pgflinewidth,-0.5\pgflinewidth) node[MyNode, anchor=south west, minimum width=1cm] (n1) {};
                    \path (n1.east) ++(1cm,0) ++(-\pgflinewidth,0) node[MyNode, anchor=west, minimum width=2cm] {};
                \end{scope}

    \end{tikzpicture}

\end{document}

enter image description here

2 Answers 2

11

The simplest thing to do is specify outer sep=0:

Sample output

\documentclass{standalone}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[ultra thick, MyNode/.style={draw, minimum height=1cm}]
  \draw[step=1cm,gray,ultra thin] (-1cm,-3cm) grid (5cm,2cm);

  % default case
  \begin{scope}[red]
    \path (0,0) node[MyNode, anchor=south west, minimum
    width=1cm,outer sep=0] (n1) {};
    \path (n1.east) ++(1cm,0) node[MyNode, anchor=west, minimum
    width=2cm,outer sep=0] {};
  \end{scope}

  \begin{scope}[blue, shift={(0,-2)}]
    \path (0,0) ++(-0.5\pgflinewidth,-0.5\pgflinewidth)
      node[MyNode, anchor=south west, minimum width=1cm] (n1) {};
    \path (n1.east) ++(1cm,0) ++(-\pgflinewidth,0)
      node[MyNode, anchor=west, minimum width=2cm] {};
  \end{scope}

\end{tikzpicture}

\end{document}

However, if connecting these nodes by lines or arrows, you will need to shorten them to avoid overlapping the borders.

2

A probably much more useless approach than Andrew's would be to draw the node with no line width and decorate it with a fat border.

Change one line:

\begin{tikzpicture}[MyNode/.style={draw, minimum height=1cm, line width=0pt, postaction={ultra thick, draw}}]

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .