Here is a code that computes the length of a line and attaches it to the line. So far, so good, but in math mode somehow additional zeros get appended to the distance.
\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{decorations.text,decorations.pathreplacing,decorations.pathmorphing}
\makeatletter
\tikzset{%
type/.code={\pgfextra{
\pgfmathsetmacro{\MyLen}{veclen(\the\pgf@pathmaxx-\the\pgf@pathminx,\the\pgf@pathmaxy-\the\pgf@pathminy)/28.45365}
\global\xdef\NewLen{\MyLen}}}
}
\makeatother
\tikzset{
dimstyle/.style={postaction={type,decorate,
decoration={text along path,raise=2pt,
text={$L=\NewLen$},text align=center}},
},decoration={brace},decorate
}
\begin{document}
\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (3,4);
\draw [dimstyle] (A)--(B);
\end{tikzpicture}
\end{document}
On the other hand, if I replace in the above text={$L=\NewLen$} by text={$L=$\NewLen} (i.e. print in text rather than math mode), I get
Question: Where do the additional zeros come from?


\global\xdefis redundant. As far as I understand\xdefis the short form of\global\edef, so\global\xdefwould be\global\global\edef. – Phelype Oleinik Feb 27 '18 at 20:32$L={}$\NewLen– Steven B. Segletes Feb 27 '18 at 20:36text={$L={\NewLen}$}– Steven B. Segletes Feb 27 '18 at 20:40\NewLenin\text{}. – Feb 27 '18 at 20:40\globalwon't change the situation. – Feb 27 '18 at 20:42\pgfmathprintnumberandnumber formatstarting on page 945 (section 92). – John Kormylo Feb 27 '18 at 20:49text={$L=\the\dimexpr\NewLen pt\relax$}. This means that the additional zeros aren't a number truncation thing, but it's TikZ repeating the last character of the input string... – Phelype Oleinik Feb 27 '18 at 20:52\draw [dimstyle] (A)--(B);` will give you much more digits.
– Feb 27 '18 at 20:54text={$L=\NewLen$~}. Very strange... – Phelype Oleinik Feb 27 '18 at 20:55number format={assume math mode=true}– John Kormylo Feb 27 '18 at 20:58text={$a$}suffices to trigger the repetition. It has nothing to do with number printing. – egreg Feb 27 '18 at 21:11texmf-dist/tex/generic/pgf/libraries/decorations/pgflibrarydecorations.text.code.tex. In there, the macro\pgf@lib@dec@text@scancharscans the text recursively until it is\pgfutil@empty(which is an empty macro). The problem is that the text never gets emptied. Once the scanner reaches the second$it starts the scanning again. This is as far as I could go :( – Phelype Oleinik Feb 27 '18 at 21:17\pgf@lib@dec@text@scancharis the same for both questions. But I don't know why it happens, I just discovered where. – Phelype Oleinik Feb 27 '18 at 21:24text={$L=\NewLen${}}avoids the repetition. – egreg Feb 27 '18 at 21:36text effects along path.text along pathalways does this: text, numbers, symbols, whatever. The example here is needlessly complex: most of the included elements are not needed to trigger the issue. – cfr Feb 27 '18 at 23:27text along pathdecoration docs: "However, even modestly complex mathematical typesetting is unlikely to be successful along a path (or even desirable)." – Mark Wibrow Feb 28 '18 at 07:30