使用 Tikz 中的 decorations.text 库控制文本位置

使用 Tikz 中的 decorations.text 库控制文本位置

我有两个属性,我一直在努力在decorations.text库中实现以下图像。 我有两个问题是:

在此处输入图片描述

1) 如何控制沿线的文本位置?通常我会使用类似下面的方法 - 我可以使用来控制文本的位置,pos=0.2但它似乎在库中不起作用decorations.text

\draw [dashed] (\mylt,\mytop) .. controls (\mylt+4cm,\mytop-2cm) and (\mylt+8cm,\mytop-2cm) .. (\mylt+15cm,\mytop-2cm) node [pos=0.2, above, sloped] (TextNode) {Overall influence curve};

2) 我可以稍微将文本与线条偏移一下吗?它看起来有点被挤压了。

以下是最小工作示例:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.text}
\begin{document}
\begin{tikzpicture}
\newlength{\mylt}
\newlength{\mytop}
\setlength{\mylt}{1.5cm}
\setlength{\mytop}{-1cm}
\draw [dashed, postaction={decorate,decoration={text along path,text align=center,text={Overall influence curve}}}] (\mylt,\mytop) .. controls (\mylt+4cm,\mytop-2cm) and (\mylt+8cm,\mytop-2cm) .. (\mylt+15cm,\mytop-2cm);
\end{tikzpicture}
\end{document}

在此先感谢您的帮助!

大卫

答案1

您可以使用选项控制沿线的位置align。您可以定义一些left indentright indent值来指示文本的开始或结束位置。

我不知道如何设置垂直偏移,但也许你可以设置\draw一条垂直移动的线,然后将\path装饰好的文本放在同一个未移动的线上。

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.text}
\begin{document}
\begin{tikzpicture}
\newlength{\mylt}
\newlength{\mytop}
\setlength{\mylt}{1.5cm}
\setlength{\mytop}{-1cm}
\draw [dashed, postaction={decorate,decoration={text along path,text align=center,text={Overall influence curve}}}] (\mylt,\mytop) .. controls (\mylt+4cm,\mytop-2cm) and (\mylt+8cm,\mytop-2cm) .. (\mylt+15cm,\mytop-2cm);

\begin{scope}[yshift=1cm]
\draw [dashed, postaction={decorate,decoration={text along path,text align={left, left indent=1cm}, text={Overall influence curve}}}] (\mylt,\mytop) .. controls (\mylt+4cm,\mytop-2cm) and (\mylt+8cm,\mytop-2cm) .. (\mylt+15cm,\mytop-2cm);
\end{scope}


\begin{scope}[yshift=2cm]
\draw[yshift=-3mm] (\mylt,\mytop) .. controls (\mylt+4cm,\mytop-2cm) and (\mylt+8cm,\mytop-2cm) .. (\mylt+15cm,\mytop-2cm);
\path [postaction={decorate,decoration={text along path,text align={right,right indent=4cm}, text={Overall influence curve}}}] (\mylt,\mytop) .. controls (\mylt+4cm,\mytop-2cm) and (\mylt+8cm,\mytop-2cm) .. (\mylt+15cm,\mytop-2cm);
\end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

更新:垂直移位

正如 jlovegren 所建议的,可以使用raise选项 inside来引入路径和文本之间的所需分隔。之前使用不同选项和简化的第三个选项的代码如下所示:decorationraisescope

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.text}
\begin{document}
\begin{tikzpicture}
\newlength{\mylt}
\newlength{\mytop}
\setlength{\mylt}{1.5cm}
\setlength{\mytop}{-1cm}
\draw [dashed, postaction={decorate,decoration={text along path,text align=center,text={Overall influence curve},raise=1mm}}] (\mylt,\mytop) .. controls (\mylt+4cm,\mytop-2cm) and (\mylt+8cm,\mytop-2cm) .. (\mylt+15cm,\mytop-2cm);

\begin{scope}[yshift=1cm]
\draw [dashed, postaction={decorate,decoration={text along path,text align={left, left indent=1cm}, text={Overall influence curve}, raise=-5mm}}] (\mylt,\mytop) .. controls (\mylt+4cm,\mytop-2cm) and (\mylt+8cm,\mytop-2cm) .. (\mylt+15cm,\mytop-2cm);
\end{scope}

\begin{scope}[yshift=2cm]
\draw [postaction={decorate,decoration={text along path,text align={right,right indent=4cm}, text={Overall influence curve}, raise=3mm}}] (\mylt,\mytop) .. controls (\mylt+4cm,\mytop-2cm) and (\mylt+8cm,\mytop-2cm) .. (\mylt+15cm,\mytop-2cm);
\end{scope}
\end{tikzpicture}
\end{document}

结果是:

在此处输入图片描述

答案2

您也可以使用text effects along path,它允许您逐个字符地应用效果,例如将文本移动到曲线的上方或下方或向左或向右移动。例如:

\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{decorations.text}
\begin{document}
  \begin{tikzpicture}
    \newlength{\mylt}
    \newlength{\mytop}
    \setlength{\mylt}{1.5cm}
    \setlength{\mytop}{-1cm}
    \draw
      [dashed,
      postaction={
        decorate,
        decoration={
          text effects along path,
          text align=center,
          text={Overall influence curve},
          text effects/.cd,
          characters={text along path, yshift=5pt}
        }
      }
      ] (\mylt,\mytop) .. controls (\mylt+4cm,\mytop-2cm) and (\mylt+8cm,\mytop-2cm) .. (\mylt+15cm,\mytop-2cm);
    \scoped[yshift=-10mm]{
      \draw
        [dashed,
        postaction={
          decorate,
          decoration={
            text effects along path,
            text align=left,
            text={Overall influence curve},
            text effects/.cd,
            characters={text along path, yshift=10pt, xshift=20mm}
          }
        }
        ] (\mylt,\mytop) .. controls (\mylt+4cm,\mytop-2cm) and (\mylt+8cm,\mytop-2cm) .. (\mylt+15cm,\mytop-2cm);}
    \scoped[yshift=-20mm]{
      \draw
        [dashed,
        postaction={
          decorate,
          decoration={
            text effects along path,
            text align=right,
            text={Overall influence curve},
            text effects/.cd,
            characters={text along path, yshift=-15pt, xshift=-20mm}
          }
        }
        ] (\mylt,\mytop) .. controls (\mylt+4cm,\mytop-2cm) and (\mylt+8cm,\mytop-2cm) .. (\mylt+15cm,\mytop-2cm);}
  \end{tikzpicture}
\end{document}

带移位文本的曲线

相关内容