12

I think I just discovered (!) that coordinates names are global in tikz:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[black]
    \draw(0,0) coordinate(alfa) -- (3,0) coordinate(beta);
\end{tikzpicture}

\begin{tikzpicture}[blue]
    \draw (alfa) -- (beta);
\end{tikzpicture}

\begin{tikzpicture}[red]
    \begin{scope}
        \draw(1,0) coordinate(alfa) -- (2,0) coordinate(beta);
    \end{scope}
\end{tikzpicture}

\begin{tikzpicture}[green]
    \draw (alfa) -- (beta);
\end{tikzpicture}

\end{document}

four silly lines

...and I really expected

  1. an error in the second tikzpicture (I am not using remember picture!), and
  2. a (0,0)--(3,0) green line...

So,

  1. am I correct that coordinate names are global to all tikzpictures?

  2. As a bonus question, is it possible to create local coordinate names?

3
  • 1
    +1 I didn't know that either. On page 983 we find this, and I quote: \pgfgetlastxy{macro for x}{macro for y} Since (x,y) coordinates are usually assigned globally, it is safe to use this command after path operations
    – AndréC
    Nov 11, 2018 at 18:00
  • I am intested in the bonus question: Is it possible to create local coordinate names? Anyone can answer?
    – Black Mild
    Feb 13, 2020 at 4:50
  • @BlackMild yes, using name prefix - it's in the accepted answer!
    – Rmano
    Feb 13, 2020 at 6:44

1 Answer 1

8

Yes, I've observed that before. And I think your interpretation is correct, and you can make coordinates "local" by using name prefix.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[black]
    \draw(0,0) coordinate(alfa) -- (3,0) coordinate(beta);
\end{tikzpicture}

\begin{tikzpicture}[blue]
    \draw (alfa) -- (beta);
\end{tikzpicture}

\begin{tikzpicture}[red,name prefix=Rmano]
    \begin{scope}
        \draw(1,0) coordinate(alfa) -- (2,0) coordinate(beta);
    \end{scope}
\end{tikzpicture}

\begin{tikzpicture}[green]
    \draw (alfa) -- (beta);
\end{tikzpicture}

\end{document}

enter image description here

1
  • Ah, name prefix is a nice trick. Thanks!
    – Rmano
    Nov 11, 2018 at 18:28

You must log in to answer this question.

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