2

When align the tikz figure and another node,they are not aligned perfectly. For example

\documentclass{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[name=main,
        ]
        \addplot [only marks, mark=*, mark options={fill=red,draw=none},mark size=5pt] coordinates {(2.47,-1.97)};
    \end{axis}
    \node[above right, align=center,draw] at (main.south east) {%
            \textbf{hello} };
\end{tikzpicture}

\end{document}

enter image description here

What is the reason cause this untidy?

2 Answers 2

3

I'd use an anchor here as I think it is a lot clearer. Moreover, the above right syntax is partly deprecated by the positioning library. Either way, you need to adjust for the line width. For example,

\documentclass{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[name=main,
    ]
    \addplot [only marks, mark=*, mark options={fill=red,draw=none},mark size=5pt] coordinates {(2.47,-1.97)};
  \end{axis}
  \node[anchor=south west, align=center,draw,yshift=-.5\pgflinewidth,xshift=.5\pgflinewidth] at (main.south east) {%
    \textbf{hello} };
\end{tikzpicture}

\end{document}

aligned node nudged into place

2
  • How did you know that is the -.5\pgflinewidth?
    – Y. zeng
    Commented May 26 at 2:29
  • 2
    @Y.zeng Because \pgflinewidth is always the current line width and line are drawn centred on the path. So half protrudes one way and half the other. That's true for all lines in tikz/pgf, including the borders of nodes. Actually, I did it with .2pt first, but I changed it so it will still work if you change the line width. (Though it will fail if you use different line widths for the plot and the node. Then you'd need to use an explicit dimension.)
    – cfr
    Commented May 26 at 2:33
3
  • nodes shape has defined option outer sep which care about that two successive nodes only touch each other and not overlap their border line.
  • if you want, that border lines overlap, than set ˙outer sep=0pt ...
  • if your node contain just one line text, than you not need to define how the text is aligned in node.
\documentclass{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[name=main]
        \addplot [only marks, mark=*, mark options={fill=red,draw=none},mark size=5pt] coordinates {(2.47,-1.97)};
    \end{axis}
    \node[draw,
          outer sep=0pt,% <y---
          above right] at (main.south east) { \textbf{hello} };
\end{tikzpicture}

\end{document}

enter image description here

Addendum:
In the case, when you like that nod s touch at vertical lines but aligned bottom horizontal lines, than just eliminate vertical outer sep with outer ysep=0:

\documentclass{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[name=main]
        \addplot [only marks, mark=*, mark options={fill=red,draw=none},mark size=5pt] coordinates {(2.47,-1.97)};
    \end{axis}
    \node[draw,
          outer ysep=0pt,  % <y---
          above right] at (main.south east) { \textbf{hello} };
\end{tikzpicture}

\end{document}

enter image description here

1
  • Thank you, I feel that you have a deep understanding of this package.
    – Y. zeng
    Commented May 26 at 7:26

You must log in to answer this question.

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