将文本精确地置于路径上指定点的中心

将文本精确地置于路径上指定点的中心

我想将文本精确地放置在高度复杂路径上的指定点上,如下图所示:

在此处输入图片描述

我已经通过指定距离路径开头 0.1 处的点并向其添加标记来开发我的示例:

\documentclass[border=1cm]{standalone}

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

\begin{document}
\begin{tikzpicture}

\coordinate (A) at (0,0);
\coordinate (B) at (1, 3);
\coordinate (C) at (2, -1);
\coordinate (D) at (3, 0.5);

%placement of mark                                                                                                                                                                                          
\draw plot [smooth] coordinates { (A) (B) (C) (D) };
\path [
   postaction={decorate},
     decoration={
       markings,
       mark = at position 0.1 with {\fill[red] circle[radius=0.2cm];}
  }
] plot [smooth] coordinates { (A) (B) (C) (D) };

%placement of text                                                                                                                                                                                          
\path [
   postaction={decorate},
     decoration={
       text along path,
       raise=0.4cm,
       text align/left indent = {0.1\dimexpr\pgfdecoratedpathlength\relax},
       text={here!!}
  }
] plot [smooth] coordinates { (A) (B) (C) (D) };

\end{tikzpicture}
\end{document}

输出为

在此处输入图片描述

要将文本置于标记的中心,我只需将其替换为left。然后一切都会很完美!不幸的是,经过这一更改后,示例不再能编译。centertext align/left indent = {0.1\dimexpr\pgfdecoratedpathlength\relax}

如何解决这个问题?

答案1

只需将文本添加到标记即可。(您需要transform shape旋转它才能沿该点的切线排版。)

\documentclass[border=1cm]{standalone}

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

\begin{document}
\begin{tikzpicture}

\coordinate (A) at (0,0);
\coordinate (B) at (1, 3);
\coordinate (C) at (2, -1);
\coordinate (D) at (3, 0.5);

%placement of mark                                                                                                                                                                                          
\draw plot [smooth] coordinates { (A) (B) (C) (D) };
\path [
   postaction={decorate},
     decoration={
       markings,
       mark = at position 0.1 with {\fill[red] circle[radius=0.2cm];
       \node[transform shape] at (0,0.5) {here!};}
  }
] plot [smooth] coordinates { (A) (B) (C) (D) };

%placement of text                                                                                                                                                                                          
\end{tikzpicture}
\end{document}

在此处输入图片描述

或者,您也可以从偏移中减去 50% 的宽度。(请注意,我在此代码中使用,\pgfmathsetmacro{\mywidth}{5*width("here!!")}因为5*0.1=1/2这样0.1\dimexpr\pgfdecoratedpathlength-\mywidth pt\relax我们就可以减去文本宽度的 50%。)

\documentclass[border=1cm]{standalone}

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

\begin{document}
\begin{tikzpicture}

\coordinate (A) at (0,0);
\coordinate (B) at (1, 3);
\coordinate (C) at (2, -1);
\coordinate (D) at (3, 0.5);

%placement of mark                                                                                                                                                                                          
\draw plot [smooth] coordinates { (A) (B) (C) (D) };
\path [
   postaction={decorate},
     decoration={
       markings,
       mark = at position 0.1 with {\fill[red] circle[radius=0.2cm];}
  }
] plot [smooth] coordinates { (A) (B) (C) (D) };

\pgfmathsetmacro{\mywidth}{5*width("here!!")}
%placement of text                                                                                                                                                                                          
\path [
   postaction={decorate},
     decoration={
       text along path,
       raise=0.4cm,
       text align/left indent ={0.1\dimexpr\pgfdecoratedpathlength-\mywidth pt\relax},
       text={here!!}
  }
] plot [smooth] coordinates { (A) (B) (C) (D) };

\end{tikzpicture}
\end{document}

在此处输入图片描述

一些风格。(请注意,我使用路径hobby只是因为否则可能会出现dimension too large问题。这些问题与样式的细节无关,这是装饰路径的一般问题。)

\documentclass[tikz,border=1cm]{standalone}
\usetikzlibrary{decorations.text, decorations.markings,hobby}
\tikzset{move text/.style args={#1 to pos #2}{%
postaction={decorate,
/utils/exec=\pgfmathsetmacro{\mywidth}{(0.5/#2)*width("#1")},
     decoration={
       text along path,
       raise=0.4cm,
       text align/left indent={#2\dimexpr\pgfdecoratedpathlength-\mywidth pt\relax},
       text={here!!}
  }}
}}
\begin{document}
\foreach \X in {0.1,0.2,...,0.9}
{\begin{tikzpicture}[]
\path[use as bounding box] (-2,-1) rectangle (4,4);
\coordinate (A) at (0,0);
\coordinate (B) at (1, 3);
\coordinate (C) at (2, -1);
\coordinate (D) at (3, 0.5);

\draw [
   postaction={decorate,
     decoration={
       markings,
       mark = at position \X\space with {\fill[red] circle[radius=0.2cm];}
  }},move text={here!!} to pos \X,
] plot [smooth,hobby] coordinates { (A) (B) (C) (D) };

\end{tikzpicture}}
\end{document}

在此处输入图片描述

相关内容