從自訂數組字串產生 TikZ 節點和標籤

從自訂數組字串產生 TikZ 節點和標籤

我正在嘗試使用 TikZ 環境在多個特定位置使用標籤來註釋圖形。我真的希望 TikZ 使用我輸入的自訂字串中的座標和標籤以及命令,而不是多次重複節點命令並更改節點位置和標籤\foreach

此外,這些座標應以圖形總寬度和高度的無量綱十進位標量 (0-1) 給出,以便即使總寬度/高度變化,節點也位於各自的位置。感謝威爾·羅伯遜對問題的回答,我從指定的寬度得到了縮放後的圖形的高度這裡

我這裡有一個不起作用的 MWE,它應該拍攝第一張圖像並添加像第二張圖像一樣的標籤。也許有人可以告訴我我做錯了什麼?先致謝!

在此輸入影像描述

在此輸入影像描述

\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}

答案1

你就快到了。你忘了乘號,忘記解析節點內容,而且不需要包calc,同名的函式庫允許你做同樣的事情。

\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}

在此輸入影像描述

相關內容