这是一段计算线的长度并将其附加到线上的代码。到目前为止,一切顺利,但在数学模式下,距离不知何故会附加额外的零。
\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}
另一方面,如果我text={$L=\NewLen$}
用上面的内容替换text={$L=$\NewLen}
(即以文本而不是数学模式打印),我会得到
问题:额外的零从哪里来的?
答案1
在pgflibrarydecorations.text.code.tex
第 274-277 行
\def\pgf@lib@dec@text@endoftext{% \let\pgfdecoraterestoftext\pgfutil@empty% \let\pgf@lib@dec@text@char\pgfutil@empty% }
它应该是
\pgfdecorationrestoftext
而不是
\pgfdecoraterestoftext
。
梅威瑟:
\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}