2

I am trying to use the TikZ environment to annotate a graphic with labels in multiple specific locations. Instead of repeating the node command many times and changing the node location and label, I would really like for TikZ to use coordinates and labels from custom strings that I input, along with the \foreach command.

Also, these coordinates should be given as dimensionless decimal scalars (0-1) of the total graphic width and height, so that the nodes are in respective locations even if total width/height changes. I get the scaled figure's height from specified width thanks to Will Robertson's answer to a question here.

I have a non-functioning MWE here which should take this first image and add labels like in the second. Perhaps someone could tell me what I'm doing wrong? Thanks in advance!

enter image description here

enter image description here

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{pgfmath,pgffor}
\usepackage{calc}

\def\site{{1,2,3,4,5}} %Load labels
\def\xdim{{0.038,0.149,0.488,0.668,0.872
}} %Load dimensionless x-coordinates for labels
\def\ydim{{0.57,0.908,0.713,0.632,0.688
}} %Load dimensionless y-coordinates for labels

\begin{document}
    \newlength\gw
    \setlength\gw{10cm} %Load graphic width
    \def\mygraphic{\includegraphics[width=\gw]{label_this.jpg}}
    \newlength\gh
    \setlength\gh{\heightof{\mygraphic}} %Graphic height
    \begin{figure}
        \centering
        \begin{tikzpicture}
            \node at (0,0) {\includegraphics[width=\gw]{label_this.jpg}};
            \foreach \i in {0,...,4}
            {
            \node at (\xdim[\i]\gw,\ydim[\i]\gh) {\site[\i]};
            }
        \end{tikzpicture}
        \caption{Labelled Graphic}
    \end{figure}
\end{document}
  • I'm not sure you can access list items with [number] for example. – Sigur Mar 5 at 17:56
3

You were almost there. You forgot multiplication signs, forgot to parse the node content, and do not need the calc package, the library of the same name allows you to do the same.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{calc}

\def\site{{1,2,3,4,5}} %Load labels
\def\xdim{{0.038,0.149,0.488,0.668,0.872
}} %Load dimensionless x-coordinates for labels
\def\ydim{{0.57,0.908,0.713,0.632,0.688
}} %Load dimensionless y-coordinates for labels

\begin{document}
    \newlength\gw
    \setlength\gw{10cm} %Load graphic width
    \def\mygraphic{\includegraphics[width=\gw]{label_this.jpg}}
%     \newlength\gh
%     \setlength\gh{\heightof{\mygraphic}} %Graphic height
    \begin{figure}
        \centering
        \begin{tikzpicture}
            \node[inner sep=0pt] (grph) at (0,0) {\includegraphics[width=\gw]{label_this.jpg}};
            \path let \p1=($(grph.north)-(grph.south)$) in
            \pgfextra{\xdef\gh{\y1}};
            \foreach \i in {0,...,4}
            {
            \node at (\xdim[\i]*\gw-4.5cm,\ydim[\i]*\gh-2.5cm) {\pgfmathparse{\site[\i]}%
            \pgfmathresult};
            }
        \end{tikzpicture}
        \caption{Labelled Graphic}
    \end{figure}
\end{document}

enter image description here

  • 1
    Yes, that fixed it! The only thing I then did differently from this was instead of subtracting manually half the box size (about 4.5cm by 2.5cm): \node at (\xdim[\i]*\gw-4.5cm,\ydim[\i]*\gh-2.5cm)... I subtracted half of the graphic size in both directions: \node at (\xdim[\i]*\gw-0.5*\gw,\ydim[\i]*\gh-0.5*\gh)... To preserve the scalability that I mentioned. Thank you! – tdstoff Mar 5 at 18:58

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.