TikZ:弯曲文本,使其沿着一条线

TikZ:弯曲文本,使其沿着一条线

我有两个箭头,从左侧节点指向右侧节点。两个箭头都略微弯曲。我想用与箭头一样弯曲的文本标记箭头。这是我目前得到的结果:

\begin{tikzpicture}
\node (One) at (-3,0) [shape=circle,draw] {One}; 
\node (Two) at (3,0) [shape=circle,draw] {Two};
\draw [->, thick] (One) to [bend right=45]  (Two);
\draw [->, thick] (One) to  [bend left=45] (Two);
\node (mental) at (0,1.75) {Some bent text};
\node (non-mental) at (0,-1.75) {Some more bent text};
\end{tikzpicture}

答案1

这可以通过decorations.text图书馆给你一个text along path。请参阅 PGF/TikZ 手册 (v2.10) 第 337 页,第 30.6 节文本修饰了解更多详情。您不能直接同时绘制和装饰路径,但可以postaction={..}在绘制路径后使用它来装饰路径(感谢 Alan Munn 提供的此提示)。

要格式化文本,请将样式宏放在参数之间|text非英语字符需要包含在括号中,例如text={|\itshape|Some Text {ö} more text}。一个问题是提高字体(它实际上只是放在路径上!)。我查看了源代码,发现字体宏放在每个单个处理的字符之前,但中间有一个\relax,即<your style>\relax<character>。您可以定义一个接受两个参数的宏,第一个是,\relax第二个是字符,然后使用它\raisebox{<length>}{<content>}来提高或降低字符。

\documentclass{article}

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

\begin{document}

\begin{tikzpicture}
\node (One) at (-3,0) [shape=circle,draw] {$One$}; 
\node (Two) at (3,0) [shape=circle,draw] {$Two$};
\def\myshift#1{\raisebox{-2.5ex}}
\draw [->,thick,postaction={decorate,decoration={text along path,text align=center,text={|\sffamily\myshift|Some more bent text}}}] (One) to [bend right=45]  (Two);
\def\myshift#1{\raisebox{1ex}}
\draw [->,thick,postaction={decorate,decoration={text along path,text align=center,text={|\sffamily\myshift|Some bent text}}}]      (One) to [bend left=45] (Two);
\end{tikzpicture}

\end{document}

图像

答案2

您可以使用raise放置文本

\documentclass{article}

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

\begin{document}
\begin{tikzpicture}
\node (One) at (-3,0) [shape=circle,draw] {$One$}; 
\node (Two) at (3,0) [shape=circle,draw] {$Two$};
\draw [->,thick,postaction={decorate,decoration={raise=-2.5ex,text along path,text align=center,text={|\sffamily|Some more bent text}}}] (One) to [bend right=45]  (Two);
\draw [->,thick,postaction={decorate,decoration={raise=1ex,text along path,text align=center,text={|\sffamily|Some bent text}}}]      (One) to [bend left=45] (Two);
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案3

tikz 库decorations.text允许你沿路径放置文本。如果你试图将文本放在一行上,文本的默认放置位置就不是很好(这似乎是因为默认假设文本行)。您可以使用\pgftransform...命令来移动文本。这些命令在pgf手册的第 79 节中进行了描述。这有点麻烦,因为您不能再使用节点标签(它们保留其坐标)。这是您的示例,其中文本已移动,正如我在给 Martin 的评论中所建议的那样:

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

\begin{tikzpicture}
\node (One) at (-3,0) [shape=circle,draw] {$One$}; 
\node (Two) at (3,0) [shape=circle,draw] {$Two$};
\draw [->, thick] (One) to [bend right=45]  (Two);
\pgftransformyshift{-.65cm}
\draw [decoration={text along path,
       text={Some more bent text},text align={center}},decorate]  (-3,0) to [bend right=45]  (3,0);
\draw [->, thick] (One) to  [bend left=45] (Two);
\pgftransformreset
\pgftransformyshift{.5cm}
\draw[decoration={text along path,
      text={Some bent text},text align={center}},decorate] (-3,0) to [bend left=45]  (3,0);
\node (mental) at (0,1.75) {};
\node (non-mental) at (0,-1.75) {};
\end{tikzpicture}


\end{document}

代码输出

相关内容