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.
\x
is a blasphemy regardingexpl3
naming convention ;-)