1

Actually, I just tried to copy a figure from my document to another template (mdpi template https://www.overleaf.com/latex/templates/mdpi-article-template/fcpwsspfzsph). I am aware that the template is quite problematic.

The simplified part I copy:

\begin{figure}[ht]
\begin{tikzpicture}
\begin{scope}[ x={(image.south east)},y={(image.north west)}]
\node at (0.5,0.5) { \includegraphics[width=1\columnwidth]{figures/figure.png}};
    %% HERE WILL BE SOME NOTATIONS/ETC (I USE THE COORDINATE SYSTEM TO DRAW THEM ON TOP OF THE IMAGE)
    %% AND GRID that are commented
    %   \draw[help lines,xstep=.1,ystep=.1] (0,0) grid (1,1);
    %   \draw[help lines,xstep=.05,ystep=.05] (0,0) grid (1,1);
    %   \foreach \x in {0,1,...,9} { \node [anchor=north] at (\x/10,0) {0.\x}; }
    %   \foreach \y in {0,1,...,9} { \node [anchor=east] at (0,\y/10) {0.\y};}
 \end{scope}
\end{tikzpicture}
  \caption{Test figure.}
 \label{fig:figure1}
\end{figure}

When I try to compile it, I receive quite an unexpected error to the line \begin{scope}[ x={(image.south east)},y={(image.north west)}] namely Package pgf Error: "No shape named 'image' is known" for another templates, it works perfectly.

What might be a problem?

Thanks to all in advance. Best regards,

  • Well, there is no shape named image in your picture. Whay else did you expect? – Henri Menke Aug 16 '21 at 12:00
  • I think what you want to do is name the node "image" by putting node (image) at... and put this line before \begin{scope}... – Enevevet Aug 16 '21 at 12:04
  • If you just want the figure, just do: \begin{figure}\centering\includegraphics{figures/figure.png}\caption{...}\end{figure}. But if you want to draw something over the figures like in https://tex.stackexchange.com/a/9562/1952, then you need to put the image inside a node before the scope environment. – Ignasi Aug 16 '21 at 12:12
  • @HenriMenke right, that was clear that is undefined, so it is even funny that it worked in another original template.. but I found that 'image' has been defined there in a couple of figures before, weird that it worked normally – osiptogo Aug 16 '21 at 12:12
  • @ignasi thanks, I missed it, was quite stupid :-) – osiptogo Aug 16 '21 at 12:13
  • @Enevevet Thank you, it helped, it was strange that the code worked before (maybe because 'image' has been defined a couple of figures before, although I do not believe it). Thanks for the quick reply – osiptogo Aug 16 '21 at 12:15
  • TikZ nodes are globally defined, meaning that if you define a node in one picture then it can be used in a later one. Of course, it may not make sense in that second picture but TeX won't complain about it. If your images in the other document were (roughly) the same size then you may not have noticed that it was using the previously-defined image node in the later picture. – Andrew Stacey Aug 16 '21 at 12:31
  • I did not expect them to be global but it was quite obvious because it did not fail. Thank you for clarification @AndrewStacey, I added this information in the answer (although the issue is quite stupid by me). – osiptogo Aug 16 '21 at 12:34
  • Node names are global in TikZ. That is why it seemed to work when you had a shape image in a previous picture. – Henri Menke Aug 16 '21 at 12:45
  • 4
    I’m voting to close this question because it was answered in comments and is no longer a question, since the issue was only a missing line. – SebGlav Aug 16 '21 at 12:52
  • 1
    Node names are global because the interaction between TeX groups and TikZ is quite complicated and the node definition could quite easily be buried in several TeX groups. Making them global avoids a lot of complicated code for very little advantage. About the only disadvantage to making them global is the one that you've encountered: old nodes persist into new TikZ pictures, and once you're aware of the possibility then it's quite easy to spot (and if not manually, judicious use of the node prefix key can help). – Andrew Stacey Aug 16 '21 at 13:04
  • 1
    @SebGlav Since the OP has written up those comments as an answer I don't think that closing the question is necessary. The answer will remove it from the unanswered queue, and it was a reasonable question to ask. – Andrew Stacey Aug 16 '21 at 13:06
  • @AndrewStacey thank you for the very detailed response, it helped to understand the mechanism behind TikZ that I was not aware of – osiptogo Aug 17 '21 at 13:07
  • @SebGlab thank you for pointing that out, but I would agree with the commenter, the question was still reasonable as in some cases this code without this line will work, which might be well known for experienced users, but can be very confusing. Although, it is closed already – osiptogo Aug 17 '21 at 13:09

1 Answers1

1

It was nothing to do with the template. The answer is simplest, a line was missing:

\begin{figure}[ht]
\begin{tikzpicture}
\node[anchor=south west, inner sep=0] (image) at (0,0) {\includegraphics[width=0.8\columnwidth]{figures/figure1.png}};
\begin{scope}[x={(image.south east)},y={(image.north west)}]
% \node at (0.5,0.5) { \includegraphics[width=1\columnwidth]{figures/figure1.png}};
    %% HERE WILL BE SOME NOTATIONS/ETC (I USE THE COORDINATE SYSTEM TO DRAW THEM ON TOP OF THE IMAGE)
    %% AND GRID that are commented
      \draw[help lines,xstep=.1,ystep=.1] (0,0) grid (1,1);
      \draw[help lines,xstep=.05,ystep=.05] (0,0) grid (1,1);
      \foreach \x in {0,1,...,9} { \node [anchor=north] at (\x/10,0) {0.\x}; }
      \foreach \y in {0,1,...,9} { \node [anchor=east] at (0,\y/10) {0.\y};}
 \end{scope}
\end{tikzpicture}
  \caption{Test figure.}
 \label{fig:figure1}
\end{figure}

Thanks to the commentators. The reason why it did not fail in another template/environment is that the nodes are globally defined and the image from a previous instance was used.