我有以下代码:
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\usetikzlibrary{decorations.text}
\begin{tikzpicture}
\path [->,decorate,decoration={raise=-5pt, text along path,
text={\texttt{foo}}, text align=center, text color=red}] (0,0) -- (5,5);
\end{tikzpicture}
\end{document}
尝试编译它似乎会导致某种无限循环。如果我删除它,它就可以\texttt
正常工作(尽管没有产生我期望的结果)。为什么?
答案1
以下示例可能会有所帮助。前三个使用相同的修饰。第四个使用另一种文本修饰,处理方式略有不同。
- 使用时需要用
|
来分隔格式化命令text along path
。 postaction
如果您希望绘制路径或其他内容而不是用文本替换,则需要进行修饰。
代码
\documentclass[tikz,border=5pt]{standalone}
\usetikzlibrary{decorations.text}
\begin{document}
\usetikzlibrary{decorations.text}
\begin{tikzpicture}
% Here the text decoration replaces the path completely so it will not be drawn even if you add draw
% The | makes it possible to pass formatting commands to the decoration
\path [->, decorate, decoration={raise=-5pt, text along path, text={|\ttfamily|foo ||}, text align=center, text color=red}] (0,0) -- (5,5);
% postaction=decorate tells TiKZ to decorate the path *after* drawing it
\path [->, draw, postaction=decorate, decoration={raise=-10pt, text along path, text={|\ttfamily|foo ||}, text align=center, text color=blue}] (0,-1) -- (5,4);
% Here the text is stretched out along the path
\path [decoration={text along path, text={|\ttfamily|foo ||}, text align=fit to path}, postaction=decorate, draw, ->] (0,-2) -- (5,3);
% Here is a fancier one which scales the characters so that the text 'grows' along the path
\path [decoration={text effects along path, text={foo}, text effects/.cd, color=green!50!black, character count=\i, character total=\n, characters={text along path, scale=\i/\n, font=\ttfamily}}, postaction=decorate, draw, ->] (0,-3) -- (5,2);
\end{tikzpicture}
\end{document}
输出
答案2
字符在一个框中逐个排版;应在 之间添加格式说明|
,请参阅手册第 48.6 节第 603 页。
\documentclass{article}
\usepackage{tikz}
\begin{document}
\usetikzlibrary{decorations.text}
\begin{tikzpicture}
\path [->,decorate,decoration={raise=-5pt, text along path,
text={|\ttfamily|foo bar}, text align=center, text color=red}] (0,0) -- (2,2);
\end{tikzpicture}
\end{document}