# Nodes near coords formatting is incorrect when non-default sci formatting is specified

Upon showing some nodes near coords in my plots I stumbled upon a formatting issue related to the use of /pgf/number format/sci generic options.

More specifically, when changing the default mantissa with

\pgfmathprintnumber[sci,sci generic={mantissa sep=\times,exponent={10^{#1}}}]\pgfplotspointmeta


The exponent is missing.

I checked the behavior directly in pure TikZ output, but this works fine. The problem only seems to show up in pgfplots. At first I thought this would be a precision issue since I ran into this problem with very small values, but even with standard (around unit) values, it shows up.

Does anybody have a clue on a workaround/patch for this ?

EDIT : Clarification for solution selection

sci,sci generic={mantissa sep=\cdot,exponent={10^{####1}}}


which shows that there are multiple intricate levels in this call.

• While I first did not consider using a style as proposed in the accepted solution, Ulrike Fischer also advised for this approach, hence the accepted proposal.

Here is a MWE showing the issue and some tests.

% arara: lwpdflatex
\documentclass{article}
\usepackage[margin=1cm]{geometry}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17,width=5cm,height=5cm}
\begin{document}
Correct output in pure TikZ

\pgfmathprintnumber[sci,sci generic={mantissa sep=\times,exponent={10^{#1}}}]{12.345}

\pgfmathprintnumber[sci,sci generic={mantissa sep=\times,exponent={10^{#1}}}]{12.345e-8}

Correct output in pgfplots with only "nodes near coords"

\begin{tikzpicture}
\begin{axis}
\end{axis}
\end{tikzpicture}

Correct output in pgfplots with "nodes near coords" with sci formatting and "$\cdot$" (default) mantissa separator

\begin{tikzpicture}
\begin{axis}
\end{axis}
\end{tikzpicture}

Wrong output in pgfplots with "nodes near coords" with sci formatting and manually set "$\cdot$" mantissa separator

\begin{tikzpicture}
\begin{axis}
\addplot[nodes near coords={\pgfmathprintnumber[sci,sci generic={mantissa sep=\cdot,exponent={10^{#1}}}]\pgfplotspointmeta},domain=0:1,samples=5] {x};
\end{axis}
\end{tikzpicture}

Wrong output in pgfplots with "nodes near coords" with sci formatting and "$\times$" mantissa separator

\begin{tikzpicture}
\begin{axis}
\addplot[nodes near coords={\pgfmathprintnumber[sci,sci generic={mantissa sep=\times,exponent={10^{#1}}}]\pgfplotspointmeta},domain=0:1,samples=5] {x};
\end{axis}
\end{tikzpicture}
\end{document}


I found if you define the style via \tikzset and use it as the option for \pgfmathprintnumber the output will be correct. But I don't know why.

To format the number format, you can also use package siunitx.

\documentclass{article}
\usepackage[margin=1cm]{geometry}
\usepackage{pgfplots}
\usepackage{siunitx}
\pgfplotsset{compat=1.17,width=5cm,height=5cm}
\tikzset{
/pgf/number format/scinum/.style={
sci,
sci generic={mantissa sep=\times,exponent={10^{##1}}},
},
}
\begin{document}
Correct output in pure TikZ

\pgfmathprintnumber[sci,sci generic={mantissa sep=\times,exponent={10^{#1}}}]{12.345}

\pgfmathprintnumber[sci,sci generic={mantissa sep=\times,exponent={10^{#1}}}]{12.345e-8}

Correct output in pgfplots with only "nodes near coords"

Wrong output in pgfplots with "nodes near coords" with sci formatting and "$\times$" mantissa separator

\begin{tikzpicture}
\begin{axis}
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}
\addplot[nodes near coords={\num[scientific-notation = true,round-mode=places,round-precision=1]{\pgfmathresult}},domain=0:1,samples=5] {x};
\end{axis}
\end{tikzpicture}
\end{document}

• Thanks for the proposal, this observation about style definition is very interesting. I will check if it works on my code later. – BambOo Jun 29 '20 at 10:00

Possible reason: the content of nodes near coords is passed to nodes near coords*, and nodes near coords* is defined as

        /pgfplots/nodes near coords*/.style={%
/pgfplots/scatter/true*,%
/pgfplots/set point meta if empty=f(x),%
scatter/@post marker code/.append code={%
% ok, place the node.
\t@pgfplots@tokb=\expandafter{\pgfplots@current@point@coordinatestyle}% see 'coordinate style'%
\edef\pgfplots@loc@TMPa{%
/pgfplots/nodes near coords/@@eval alignment,%
\the\t@pgfplots@tokb,%
/pgfplots/every node near coord%
}%
\expandafter\node\expandafter[\pgfplots@loc@TMPa]%
{%
\if1\csname pgfpmeta@\pgfplotspointmetainputhandler @issymbolic\endcsname%
% point meta=explicit symbolic
% do not try to invoke \pgfmathprintnumber on
% symbolic data... this check should keep the user
% interface simpler.
\def\pgfplots@loc@TMPa{\pgfmathprintnumber\pgfplotspointmeta}%
\def\pgfplots@loc@TMPb{##1}%
\ifx\pgfplots@loc@TMPa\pgfplots@loc@TMPb
\pgfplotspointmeta
\else
##1%
\fi
\else
##1%
\fi
};
},%
},%


So #1 in exponent={10^{#1}} is not expanded correctly because of the outer wrapper of nodes near coords*.

I fail to figure out where is wrong. But there is a work around: set the key outside like

\pgfqkeys{/pgf/number format}{sci,sci generic={mantissa sep=\cdot,exponent={10^{#1}}}}


Example:

\documentclass{article}
\usepackage[margin=1cm]{geometry}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17,width=5cm,height=5cm}
\newcommand{\sciset}[1]{\pgfqkeys{/pgf/number format}{#1}}

\begin{document}
Correct output in pgfplots with "nodes near coords" with sci formatting and manually set "$\cdot$" mantissa separator

\sciset{sci,sci generic={mantissa sep=\cdot,exponent={10^{#1}}}}
\begin{tikzpicture}
\begin{axis}
\end{axis}
\end{tikzpicture}

Correct output in pgfplots with "nodes near coords" with sci formatting and "$\times$" mantissa separator

\sciset{sci,sci generic={mantissa sep=\times,exponent={10^{#1}}}}
\begin{tikzpicture}
\begin{axis}

• Yes, I guess it works too well ... unfortunately I need it do only be focused on the nodes near coords formattin. Thanks anyway. – BambOo Jun 26 '20 at 8:50
• I try to hack the code of nodes near coordinates by xpatch and /pgfplots/scatter/@pre marker code, but failed. – ZhiyuanLck Jun 26 '20 at 9:16
• Do not worry to much, I think an issue in the pgfplots tacker is in order – BambOo Jun 26 '20 at 9:48