Generating TikZ Nodes and Labels from Custom Array Strings












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}

defsite{{1,2,3,4,5}} %Load labels
defxdim{{0.038,0.149,0.488,0.668,0.872
}} %Load dimensionless x-coordinates for labels
defydim{{0.57,0.908,0.713,0.632,0.688
}} %Load dimensionless y-coordinates for labels

begin{document}
newlengthgw
setlengthgw{10cm} %Load graphic width
defmygraphic{includegraphics[width=gw]{label_this.jpg}}
newlengthgh
setlengthgh{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}









share|improve this question























  • I'm not sure you can access list items with [number] for example.

    – Sigur
    Mar 5 at 17:56
















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}

defsite{{1,2,3,4,5}} %Load labels
defxdim{{0.038,0.149,0.488,0.668,0.872
}} %Load dimensionless x-coordinates for labels
defydim{{0.57,0.908,0.713,0.632,0.688
}} %Load dimensionless y-coordinates for labels

begin{document}
newlengthgw
setlengthgw{10cm} %Load graphic width
defmygraphic{includegraphics[width=gw]{label_this.jpg}}
newlengthgh
setlengthgh{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}









share|improve this question























  • I'm not sure you can access list items with [number] for example.

    – Sigur
    Mar 5 at 17:56














2












2








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}

defsite{{1,2,3,4,5}} %Load labels
defxdim{{0.038,0.149,0.488,0.668,0.872
}} %Load dimensionless x-coordinates for labels
defydim{{0.57,0.908,0.713,0.632,0.688
}} %Load dimensionless y-coordinates for labels

begin{document}
newlengthgw
setlengthgw{10cm} %Load graphic width
defmygraphic{includegraphics[width=gw]{label_this.jpg}}
newlengthgh
setlengthgh{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}









share|improve this question














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}

defsite{{1,2,3,4,5}} %Load labels
defxdim{{0.038,0.149,0.488,0.668,0.872
}} %Load dimensionless x-coordinates for labels
defydim{{0.57,0.908,0.713,0.632,0.688
}} %Load dimensionless y-coordinates for labels

begin{document}
newlengthgw
setlengthgw{10cm} %Load graphic width
defmygraphic{includegraphics[width=gw]{label_this.jpg}}
newlengthgh
setlengthgh{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}






tikz-pgf foreach






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 5 at 17:51









tdstofftdstoff

253




253













  • I'm not sure you can access list items with [number] for example.

    – Sigur
    Mar 5 at 17:56



















  • I'm not sure you can access list items with [number] for example.

    – Sigur
    Mar 5 at 17:56

















I'm not sure you can access list items with [number] for example.

– Sigur
Mar 5 at 17:56





I'm not sure you can access list items with [number] for example.

– Sigur
Mar 5 at 17:56










1 Answer
1






active

oldest

votes


















2














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}

defsite{{1,2,3,4,5}} %Load labels
defxdim{{0.038,0.149,0.488,0.668,0.872
}} %Load dimensionless x-coordinates for labels
defydim{{0.57,0.908,0.713,0.632,0.688
}} %Load dimensionless y-coordinates for labels

begin{document}
newlengthgw
setlengthgw{10cm} %Load graphic width
defmygraphic{includegraphics[width=gw]{label_this.jpg}}
% newlengthgh
% setlengthgh{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{xdefgh{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






share|improve this answer



















  • 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








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "85"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f477905%2fgenerating-tikz-nodes-and-labels-from-custom-array-strings%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














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}

defsite{{1,2,3,4,5}} %Load labels
defxdim{{0.038,0.149,0.488,0.668,0.872
}} %Load dimensionless x-coordinates for labels
defydim{{0.57,0.908,0.713,0.632,0.688
}} %Load dimensionless y-coordinates for labels

begin{document}
newlengthgw
setlengthgw{10cm} %Load graphic width
defmygraphic{includegraphics[width=gw]{label_this.jpg}}
% newlengthgh
% setlengthgh{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{xdefgh{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






share|improve this answer



















  • 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


















2














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}

defsite{{1,2,3,4,5}} %Load labels
defxdim{{0.038,0.149,0.488,0.668,0.872
}} %Load dimensionless x-coordinates for labels
defydim{{0.57,0.908,0.713,0.632,0.688
}} %Load dimensionless y-coordinates for labels

begin{document}
newlengthgw
setlengthgw{10cm} %Load graphic width
defmygraphic{includegraphics[width=gw]{label_this.jpg}}
% newlengthgh
% setlengthgh{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{xdefgh{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






share|improve this answer



















  • 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
















2












2








2







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}

defsite{{1,2,3,4,5}} %Load labels
defxdim{{0.038,0.149,0.488,0.668,0.872
}} %Load dimensionless x-coordinates for labels
defydim{{0.57,0.908,0.713,0.632,0.688
}} %Load dimensionless y-coordinates for labels

begin{document}
newlengthgw
setlengthgw{10cm} %Load graphic width
defmygraphic{includegraphics[width=gw]{label_this.jpg}}
% newlengthgh
% setlengthgh{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{xdefgh{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






share|improve this answer













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}

defsite{{1,2,3,4,5}} %Load labels
defxdim{{0.038,0.149,0.488,0.668,0.872
}} %Load dimensionless x-coordinates for labels
defydim{{0.57,0.908,0.713,0.632,0.688
}} %Load dimensionless y-coordinates for labels

begin{document}
newlengthgw
setlengthgw{10cm} %Load graphic width
defmygraphic{includegraphics[width=gw]{label_this.jpg}}
% newlengthgh
% setlengthgh{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{xdefgh{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







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 5 at 18:02









marmotmarmot

108k5132248




108k5132248








  • 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
















  • 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










1




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







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




















draft saved

draft discarded




















































Thanks for contributing an answer to TeX - LaTeX Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f477905%2fgenerating-tikz-nodes-and-labels-from-custom-array-strings%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?