我希望一些文本沿着一条线出现,但不会被线本身遮挡。我尝试了以下方法:
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{decorations.text}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot[mark=none,
dotted,
decoration={
text along path,
text={{My text \(\alpha\)}{}},
text align={center},
raise=-0.5ex},
postaction={decorate},
]
{x^2};
% The following has the correct fill but then the text is even more mis-aligned
% \addplot[mark=none,
% dotted,
% decoration={
% text effects along path,
% text={{My text \(\alpha\)}{}},
% text align={center},
% raise=-0.5ex,
% text effects/.cd,
% every character/.style={fill=white, yshift=-0.5ex}},
% postaction={decorate},
% ]
% {x^2};
\end{axis}
\end{tikzpicture}
\end{document}
但第一次尝试时,文本被线条遮挡,无法 100% 对齐(无法沿着线条正确弯曲)。第二次尝试时,文本具有正确的背景(白色),因此线条不再遮挡文本,但文本与曲线的错位更大。
我该如何解决这些问题?
答案1
这是您想要实现的吗?
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{decorations.text}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot[mark=none,
dotted,
decoration={
text effects along path,
text={My text {\(\alpha\)}},
text align={center},
raise=-0.5ex,
text effects/.cd,
characters={text along path, fill=white},
},
postaction={decorate},
]
{x^2};
\end{axis}
\end{tikzpicture}
\end{document}