Generating TikZ Nodes and Labels from Custom Array Strings
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!
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
add a comment |
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!
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
I'm not sure you can access list items with[number]
for example.
– Sigur
Mar 5 at 17:56
add a comment |
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!
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
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!
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
tikz-pgf foreach
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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}
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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}
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
add a comment |
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}
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
add a comment |
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}
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}
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
I'm not sure you can access list items with
[number]
for example.– Sigur
Mar 5 at 17:56