Macro expansion in TikZ node












3















I am having some problems with macro expansion in TikZ nodes. The MWE below is supposed to draw nodes at specified coordinates and display their coordinate rounded to the nearest number, but it shows the coordinates of the last node for both of them.



documentclass{article}

usepackage{pgfplots}
usepackage{xparse}

ExplSyntaxOn

tl_new:N x
tl_new:N x_round

NewDocumentCommand mynode { m } {
tl_set:Nn x {
fp_eval:n { #1 }
}

tl_set:Nn x_round {
fp_eval:n { round ( x ) }
}

addplot [style={mark=none}] coordinates { ( x , x ) } node { x_round } ;
}

ExplSyntaxOff

begin{document}
begin{tikzpicture}
begin{axis}
mynode{1.1}
mynode{2.2}
end{axis}
end{tikzpicture}
end{document}


I assume the problem is that the x_round is not expanded in the node until the end of the environments. How can I ensure x_round is expanded immediately?



I have noticed that replacing the command with



NewDocumentCommand mynode { m } {
addplot [style={mark=none}] coordinates { ( #1 , #1 ) } node { fp_eval:n { round ( #1 ) } } ;
}


does fix the problem, but for my real code, it is inadequate as the code is more complex.










share|improve this question


















  • 2





    x is a blasphemy regarding expl3 naming convention ;-)

    – Christian Hupfer
    Feb 4 at 9:29











  • Hehe, I wanted to keep it simple for the MWE. My actual code as proper naming ;)

    – slo
    Feb 4 at 10:52
















3















I am having some problems with macro expansion in TikZ nodes. The MWE below is supposed to draw nodes at specified coordinates and display their coordinate rounded to the nearest number, but it shows the coordinates of the last node for both of them.



documentclass{article}

usepackage{pgfplots}
usepackage{xparse}

ExplSyntaxOn

tl_new:N x
tl_new:N x_round

NewDocumentCommand mynode { m } {
tl_set:Nn x {
fp_eval:n { #1 }
}

tl_set:Nn x_round {
fp_eval:n { round ( x ) }
}

addplot [style={mark=none}] coordinates { ( x , x ) } node { x_round } ;
}

ExplSyntaxOff

begin{document}
begin{tikzpicture}
begin{axis}
mynode{1.1}
mynode{2.2}
end{axis}
end{tikzpicture}
end{document}


I assume the problem is that the x_round is not expanded in the node until the end of the environments. How can I ensure x_round is expanded immediately?



I have noticed that replacing the command with



NewDocumentCommand mynode { m } {
addplot [style={mark=none}] coordinates { ( #1 , #1 ) } node { fp_eval:n { round ( #1 ) } } ;
}


does fix the problem, but for my real code, it is inadequate as the code is more complex.










share|improve this question


















  • 2





    x is a blasphemy regarding expl3 naming convention ;-)

    – Christian Hupfer
    Feb 4 at 9:29











  • Hehe, I wanted to keep it simple for the MWE. My actual code as proper naming ;)

    – slo
    Feb 4 at 10:52














3












3








3








I am having some problems with macro expansion in TikZ nodes. The MWE below is supposed to draw nodes at specified coordinates and display their coordinate rounded to the nearest number, but it shows the coordinates of the last node for both of them.



documentclass{article}

usepackage{pgfplots}
usepackage{xparse}

ExplSyntaxOn

tl_new:N x
tl_new:N x_round

NewDocumentCommand mynode { m } {
tl_set:Nn x {
fp_eval:n { #1 }
}

tl_set:Nn x_round {
fp_eval:n { round ( x ) }
}

addplot [style={mark=none}] coordinates { ( x , x ) } node { x_round } ;
}

ExplSyntaxOff

begin{document}
begin{tikzpicture}
begin{axis}
mynode{1.1}
mynode{2.2}
end{axis}
end{tikzpicture}
end{document}


I assume the problem is that the x_round is not expanded in the node until the end of the environments. How can I ensure x_round is expanded immediately?



I have noticed that replacing the command with



NewDocumentCommand mynode { m } {
addplot [style={mark=none}] coordinates { ( #1 , #1 ) } node { fp_eval:n { round ( #1 ) } } ;
}


does fix the problem, but for my real code, it is inadequate as the code is more complex.










share|improve this question














I am having some problems with macro expansion in TikZ nodes. The MWE below is supposed to draw nodes at specified coordinates and display their coordinate rounded to the nearest number, but it shows the coordinates of the last node for both of them.



documentclass{article}

usepackage{pgfplots}
usepackage{xparse}

ExplSyntaxOn

tl_new:N x
tl_new:N x_round

NewDocumentCommand mynode { m } {
tl_set:Nn x {
fp_eval:n { #1 }
}

tl_set:Nn x_round {
fp_eval:n { round ( x ) }
}

addplot [style={mark=none}] coordinates { ( x , x ) } node { x_round } ;
}

ExplSyntaxOff

begin{document}
begin{tikzpicture}
begin{axis}
mynode{1.1}
mynode{2.2}
end{axis}
end{tikzpicture}
end{document}


I assume the problem is that the x_round is not expanded in the node until the end of the environments. How can I ensure x_round is expanded immediately?



I have noticed that replacing the command with



NewDocumentCommand mynode { m } {
addplot [style={mark=none}] coordinates { ( #1 , #1 ) } node { fp_eval:n { round ( #1 ) } } ;
}


does fix the problem, but for my real code, it is inadequate as the code is more complex.







tikz-pgf expansion expl3






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Feb 4 at 9:27









sloslo

1304




1304








  • 2





    x is a blasphemy regarding expl3 naming convention ;-)

    – Christian Hupfer
    Feb 4 at 9:29











  • Hehe, I wanted to keep it simple for the MWE. My actual code as proper naming ;)

    – slo
    Feb 4 at 10:52














  • 2





    x is a blasphemy regarding expl3 naming convention ;-)

    – Christian Hupfer
    Feb 4 at 9:29











  • Hehe, I wanted to keep it simple for the MWE. My actual code as proper naming ;)

    – slo
    Feb 4 at 10:52








2




2





x is a blasphemy regarding expl3 naming convention ;-)

– Christian Hupfer
Feb 4 at 9:29





x is a blasphemy regarding expl3 naming convention ;-)

– Christian Hupfer
Feb 4 at 9:29













Hehe, I wanted to keep it simple for the MWE. My actual code as proper naming ;)

– slo
Feb 4 at 10:52





Hehe, I wanted to keep it simple for the MWE. My actual code as proper naming ;)

– slo
Feb 4 at 10:52










1 Answer
1






active

oldest

votes


















1














You should use tl_set:Nx, but it's quite pointless anyway:



documentclass{article}

usepackage{pgfplots}
usepackage{xparse}

ExplSyntaxOn

NewDocumentCommand mynode { m }
{
addplot [style={mark=none}]
coordinates { ( fp_eval:n { #1 } , fp_eval:n { #1 } ) }
node { fp_eval:n { round(#1) } } ;
}

ExplSyntaxOff

begin{document}

begin{tikzpicture}
begin{axis}
mynode{1.1}
mynode{2.2}
end{axis}
end{tikzpicture}

end{document}


Avoid names such as x and x_round, use the proper naming conventions.



enter image description here



You can use variables (here I use fp ones, but tl can be good as well, provided you set them with tl_set:Nx). However, text in the node has to be expanded before passing it to PGF:



documentclass{article}

usepackage{pgfplots}
usepackage{xparse}

ExplSyntaxOn

fp_new:N l_slo_coord_x_fp
fp_new:N l_slo_coord_x_round_fp

NewDocumentCommand mynode { m }
{
fp_set:Nn l_slo_coord_x_fp { #1 }
fp_set:Nn l_slo_coord_x_round_fp { round ( l_slo_coord_x_fp ) }
use:x
{
exp_not:N addplot [style={mark=none}]
coordinates { ( fp_use:N l_slo_coord_x_fp , fp_use:N l_slo_coord_x_fp ) }
node { fp_use:N l_slo_coord_x_round_fp } ;
}
}

ExplSyntaxOff

begin{document}

begin{tikzpicture}
begin{axis}
mynode{1.1}
mynode{2.2}
end{axis}
end{tikzpicture}

end{document}





share|improve this answer


























  • Perhaps worth using an auxiliary? I'd tend to avoid evaluating the same expression multiple times, both for efficiency and as it could have a random component.

    – Joseph Wright
    Feb 4 at 9:47











  • @JosephWright It doesn't really work here, because one has to expand the argument to node.

    – egreg
    Feb 4 at 9:55











  • Thanks, it works great. I actually tried using tl_set:Nx, but I guess node has to be unexpanded if I understand things correctly, right?

    – slo
    Feb 4 at 11:54











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%2f473294%2fmacro-expansion-in-tikz-node%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









1














You should use tl_set:Nx, but it's quite pointless anyway:



documentclass{article}

usepackage{pgfplots}
usepackage{xparse}

ExplSyntaxOn

NewDocumentCommand mynode { m }
{
addplot [style={mark=none}]
coordinates { ( fp_eval:n { #1 } , fp_eval:n { #1 } ) }
node { fp_eval:n { round(#1) } } ;
}

ExplSyntaxOff

begin{document}

begin{tikzpicture}
begin{axis}
mynode{1.1}
mynode{2.2}
end{axis}
end{tikzpicture}

end{document}


Avoid names such as x and x_round, use the proper naming conventions.



enter image description here



You can use variables (here I use fp ones, but tl can be good as well, provided you set them with tl_set:Nx). However, text in the node has to be expanded before passing it to PGF:



documentclass{article}

usepackage{pgfplots}
usepackage{xparse}

ExplSyntaxOn

fp_new:N l_slo_coord_x_fp
fp_new:N l_slo_coord_x_round_fp

NewDocumentCommand mynode { m }
{
fp_set:Nn l_slo_coord_x_fp { #1 }
fp_set:Nn l_slo_coord_x_round_fp { round ( l_slo_coord_x_fp ) }
use:x
{
exp_not:N addplot [style={mark=none}]
coordinates { ( fp_use:N l_slo_coord_x_fp , fp_use:N l_slo_coord_x_fp ) }
node { fp_use:N l_slo_coord_x_round_fp } ;
}
}

ExplSyntaxOff

begin{document}

begin{tikzpicture}
begin{axis}
mynode{1.1}
mynode{2.2}
end{axis}
end{tikzpicture}

end{document}





share|improve this answer


























  • Perhaps worth using an auxiliary? I'd tend to avoid evaluating the same expression multiple times, both for efficiency and as it could have a random component.

    – Joseph Wright
    Feb 4 at 9:47











  • @JosephWright It doesn't really work here, because one has to expand the argument to node.

    – egreg
    Feb 4 at 9:55











  • Thanks, it works great. I actually tried using tl_set:Nx, but I guess node has to be unexpanded if I understand things correctly, right?

    – slo
    Feb 4 at 11:54
















1














You should use tl_set:Nx, but it's quite pointless anyway:



documentclass{article}

usepackage{pgfplots}
usepackage{xparse}

ExplSyntaxOn

NewDocumentCommand mynode { m }
{
addplot [style={mark=none}]
coordinates { ( fp_eval:n { #1 } , fp_eval:n { #1 } ) }
node { fp_eval:n { round(#1) } } ;
}

ExplSyntaxOff

begin{document}

begin{tikzpicture}
begin{axis}
mynode{1.1}
mynode{2.2}
end{axis}
end{tikzpicture}

end{document}


Avoid names such as x and x_round, use the proper naming conventions.



enter image description here



You can use variables (here I use fp ones, but tl can be good as well, provided you set them with tl_set:Nx). However, text in the node has to be expanded before passing it to PGF:



documentclass{article}

usepackage{pgfplots}
usepackage{xparse}

ExplSyntaxOn

fp_new:N l_slo_coord_x_fp
fp_new:N l_slo_coord_x_round_fp

NewDocumentCommand mynode { m }
{
fp_set:Nn l_slo_coord_x_fp { #1 }
fp_set:Nn l_slo_coord_x_round_fp { round ( l_slo_coord_x_fp ) }
use:x
{
exp_not:N addplot [style={mark=none}]
coordinates { ( fp_use:N l_slo_coord_x_fp , fp_use:N l_slo_coord_x_fp ) }
node { fp_use:N l_slo_coord_x_round_fp } ;
}
}

ExplSyntaxOff

begin{document}

begin{tikzpicture}
begin{axis}
mynode{1.1}
mynode{2.2}
end{axis}
end{tikzpicture}

end{document}





share|improve this answer


























  • Perhaps worth using an auxiliary? I'd tend to avoid evaluating the same expression multiple times, both for efficiency and as it could have a random component.

    – Joseph Wright
    Feb 4 at 9:47











  • @JosephWright It doesn't really work here, because one has to expand the argument to node.

    – egreg
    Feb 4 at 9:55











  • Thanks, it works great. I actually tried using tl_set:Nx, but I guess node has to be unexpanded if I understand things correctly, right?

    – slo
    Feb 4 at 11:54














1












1








1







You should use tl_set:Nx, but it's quite pointless anyway:



documentclass{article}

usepackage{pgfplots}
usepackage{xparse}

ExplSyntaxOn

NewDocumentCommand mynode { m }
{
addplot [style={mark=none}]
coordinates { ( fp_eval:n { #1 } , fp_eval:n { #1 } ) }
node { fp_eval:n { round(#1) } } ;
}

ExplSyntaxOff

begin{document}

begin{tikzpicture}
begin{axis}
mynode{1.1}
mynode{2.2}
end{axis}
end{tikzpicture}

end{document}


Avoid names such as x and x_round, use the proper naming conventions.



enter image description here



You can use variables (here I use fp ones, but tl can be good as well, provided you set them with tl_set:Nx). However, text in the node has to be expanded before passing it to PGF:



documentclass{article}

usepackage{pgfplots}
usepackage{xparse}

ExplSyntaxOn

fp_new:N l_slo_coord_x_fp
fp_new:N l_slo_coord_x_round_fp

NewDocumentCommand mynode { m }
{
fp_set:Nn l_slo_coord_x_fp { #1 }
fp_set:Nn l_slo_coord_x_round_fp { round ( l_slo_coord_x_fp ) }
use:x
{
exp_not:N addplot [style={mark=none}]
coordinates { ( fp_use:N l_slo_coord_x_fp , fp_use:N l_slo_coord_x_fp ) }
node { fp_use:N l_slo_coord_x_round_fp } ;
}
}

ExplSyntaxOff

begin{document}

begin{tikzpicture}
begin{axis}
mynode{1.1}
mynode{2.2}
end{axis}
end{tikzpicture}

end{document}





share|improve this answer















You should use tl_set:Nx, but it's quite pointless anyway:



documentclass{article}

usepackage{pgfplots}
usepackage{xparse}

ExplSyntaxOn

NewDocumentCommand mynode { m }
{
addplot [style={mark=none}]
coordinates { ( fp_eval:n { #1 } , fp_eval:n { #1 } ) }
node { fp_eval:n { round(#1) } } ;
}

ExplSyntaxOff

begin{document}

begin{tikzpicture}
begin{axis}
mynode{1.1}
mynode{2.2}
end{axis}
end{tikzpicture}

end{document}


Avoid names such as x and x_round, use the proper naming conventions.



enter image description here



You can use variables (here I use fp ones, but tl can be good as well, provided you set them with tl_set:Nx). However, text in the node has to be expanded before passing it to PGF:



documentclass{article}

usepackage{pgfplots}
usepackage{xparse}

ExplSyntaxOn

fp_new:N l_slo_coord_x_fp
fp_new:N l_slo_coord_x_round_fp

NewDocumentCommand mynode { m }
{
fp_set:Nn l_slo_coord_x_fp { #1 }
fp_set:Nn l_slo_coord_x_round_fp { round ( l_slo_coord_x_fp ) }
use:x
{
exp_not:N addplot [style={mark=none}]
coordinates { ( fp_use:N l_slo_coord_x_fp , fp_use:N l_slo_coord_x_fp ) }
node { fp_use:N l_slo_coord_x_round_fp } ;
}
}

ExplSyntaxOff

begin{document}

begin{tikzpicture}
begin{axis}
mynode{1.1}
mynode{2.2}
end{axis}
end{tikzpicture}

end{document}






share|improve this answer














share|improve this answer



share|improve this answer








edited Feb 4 at 9:59

























answered Feb 4 at 9:45









egregegreg

719k8719053205




719k8719053205













  • Perhaps worth using an auxiliary? I'd tend to avoid evaluating the same expression multiple times, both for efficiency and as it could have a random component.

    – Joseph Wright
    Feb 4 at 9:47











  • @JosephWright It doesn't really work here, because one has to expand the argument to node.

    – egreg
    Feb 4 at 9:55











  • Thanks, it works great. I actually tried using tl_set:Nx, but I guess node has to be unexpanded if I understand things correctly, right?

    – slo
    Feb 4 at 11:54



















  • Perhaps worth using an auxiliary? I'd tend to avoid evaluating the same expression multiple times, both for efficiency and as it could have a random component.

    – Joseph Wright
    Feb 4 at 9:47











  • @JosephWright It doesn't really work here, because one has to expand the argument to node.

    – egreg
    Feb 4 at 9:55











  • Thanks, it works great. I actually tried using tl_set:Nx, but I guess node has to be unexpanded if I understand things correctly, right?

    – slo
    Feb 4 at 11:54

















Perhaps worth using an auxiliary? I'd tend to avoid evaluating the same expression multiple times, both for efficiency and as it could have a random component.

– Joseph Wright
Feb 4 at 9:47





Perhaps worth using an auxiliary? I'd tend to avoid evaluating the same expression multiple times, both for efficiency and as it could have a random component.

– Joseph Wright
Feb 4 at 9:47













@JosephWright It doesn't really work here, because one has to expand the argument to node.

– egreg
Feb 4 at 9:55





@JosephWright It doesn't really work here, because one has to expand the argument to node.

– egreg
Feb 4 at 9:55













Thanks, it works great. I actually tried using tl_set:Nx, but I guess node has to be unexpanded if I understand things correctly, right?

– slo
Feb 4 at 11:54





Thanks, it works great. I actually tried using tl_set:Nx, but I guess node has to be unexpanded if I understand things correctly, right?

– slo
Feb 4 at 11:54


















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%2f473294%2fmacro-expansion-in-tikz-node%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?