7

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}

enter image description here

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

enter image description here

Question: Where do the additional zeros come from?

  • 1
    Off-topic: I think that \global\xdef is redundant. As far as I understand \xdef is the short form of \global\edef, so \global\xdef would be \global\global\edef. – Phelype Oleinik Feb 27 '18 at 20:32
  • 2
    You want to see something really crazy, try text={$L={\NewLen}$} – Steven B. Segletes Feb 27 '18 at 20:40
  • 2
    Check out \pgfmathprintnumber and number format starting on page 945 (section 92). – John Kormylo Feb 27 '18 at 20:49
  • 2
    Just text={$a$} suffices to trigger the repetition. It has nothing to do with number printing. – egreg Feb 27 '18 at 21:11
  • 2
    See tex.stackexchange.com/questions/75256/… Indeed, text={$L=\NewLen${}} avoids the repetition. – egreg Feb 27 '18 at 21:36
6

In pgflibrarydecorations.text.code.tex line 274-277

\def\pgf@lib@dec@text@endoftext{%
  \let\pgfdecoraterestoftext\pgfutil@empty%
  \let\pgf@lib@dec@text@char\pgfutil@empty%
}

it should be
\pgfdecorationrestoftext instead of
\pgfdecoraterestoftext.

MWE:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.text}

\makeatletter
\def\pgf@lib@dec@text@endoftext{%
    \let\pgfdecorationrestoftext\pgfutil@empty%
    \let\pgf@lib@dec@text@char\pgfutil@empty%
}

\begin{document}
\begin{tikzpicture}
   \coordinate (A) at (0,0);
   \coordinate (B) at (3,4);
   \draw[decorate,decoration={text along path,text={$abc$},text align=center}](A)--(B);
   \draw(A)--(B);
\end{tikzpicture}

\end{document}
|improve this answer|||||
  • Wow! Almost impossible to spot. (Not that I tried.) Thanks!!!! – user121799 Feb 28 '18 at 22:49
  • sourceforge is currently down... If someone sees this remind me to report the bug. (or report it yourself.) – Symbol 1 Feb 28 '18 at 23:24
  • It seems like I cannot report the bug there (would probably need a sourceforge account, which I do not have). – user121799 Mar 5 '18 at 22:06
  • Even with your solution I still get abcc instead of abc above the line (screenshot). At least it is better than before with abcccccccccccccccc. – Henri Menke Dec 23 '18 at 23:00
  • I did some research, text={$abc} will partially solve this. – Symbol 1 Dec 26 '18 at 0:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy