TikZ 路径装饰:路径下方的文本

TikZ 路径装饰:路径下方的文本

我想发表一篇文字以下TikZ 中的给定路径,请参见下面屏幕截图中的“标签 b”。

不幸的是,我未能below在文档中找到路径装饰的键。

因此,我尝试测量文本的高度,将其取反,然后将装饰高度提高相应的量。

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

\begin{document}
    \begin{tikzpicture}
    \newcommand{\radius}{2}
    \coordinate (cs-center);

    \draw (cs-center) circle (\radius);
    \draw (cs-center) -- ++(  0 + 180 : \radius);
    \draw (cs-center) -- ++(120 + 180 : \radius);
    \draw (cs-center) -- ++(240 + 180 : \radius);


    \newcommand{\labelraise}{1pt}
    \path[decorate, decoration={text along path, text=label a, text align=center, raise=\labelraise}] (cs-center) ++(180 : \radius) arc (180 : 300 : \radius);

    \newlength{\labelheight}
    \settoheight{\labelheight}{l}
    \path[decorate,decoration={text along path, text=label b, text align=center, raise=-\labelheight-\labelraise}] (cs-center) ++(180 : \radius) arc (180 : 60 : \radius);
    \end{tikzpicture}
\end{document}

\settoheight似乎在 tikzpicture 内部不起作用。\labelheight设置为零。(因此“标签 b”仅降低了 1pt \labelraise。)

在此处输入图片描述

如果我尝试通过(不令人满意的)硬编码来提高它,-1em标签的字符会一起移动(b甚至互相接触):

在此处输入图片描述

因此,我尝试改变路径本身,将半径减小 1em,但结果却是! Dimension too large.

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

\begin{document}
    \begin{tikzpicture}
    \newcommand{\radius}{2}
    \coordinate (cs-center);

    \draw (cs-center) circle (\radius);
    \draw (cs-center) -- ++(  0 + 180 : \radius);
    \draw (cs-center) -- ++(120 + 180 : \radius);
    \draw (cs-center) -- ++(240 + 180 : \radius);


    \newcommand{\labelraise}{1pt}
    \path[decorate, decoration={text along path, text=label a, text align=center, raise=\labelraise}] (cs-center) ++(180 : \radius) arc (180 : 300 : \radius);

    \newlength{\labelheight}
    %\settoheight{\labelheight}{l} % this does not work
    \setlength{\labelheight}{1em}
    \newcommand{\textradius}{\radius-\labelheight}
    \path[decorate,decoration={text along path, text=label b, text align=center, raise=-\labelraise}] (cs-center) ++(180 : \textradius) arc (180 : 60 : \textradius);
    \end{tikzpicture}
\end{document}

我已经看到了这个问题路径下方的文本修饰但唯一的答案只是建议使用raise具有上述问题的硬编码。

我怎样才能将文本放置在路径下方,以便如果我想更改字体大小和字符的自然距离,则不需要更改硬编码数字?

答案1

含钛Z 计算字符的高度非常简单,例如height("l")

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

\begin{document}
    \begin{tikzpicture}
    \newcommand{\radius}{2}
    \coordinate (cs-center);

    \draw (cs-center) circle (\radius);
    \draw (cs-center) -- ++(  0 + 180 : \radius);
    \draw (cs-center) -- ++(120 + 180 : \radius);
    \draw (cs-center) -- ++(240 + 180 : \radius);


    \newcommand{\labelraise}{1pt}
    \path[decorate, decoration={text along path, text=label a, text align=center, raise=\labelraise}] (cs-center) ++(180 : \radius) arc (180 : 300 : \radius);

    \pgfmathsetmacro{\labelheight}{height("l")}
    \path[decorate,decoration={text along path, text=label b, text align=center,
    raise=-\labelraise-\labelheight}] (cs-center) ++(180 : \radius) arc (180 : 60 : \radius);
    \end{tikzpicture}
\end{document}

在此处输入图片描述

调整字符间距的一种方法是

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

\begin{document}
    \begin{tikzpicture}
    \newcommand{\radius}{2}
    \coordinate (cs-center);

    \draw (cs-center) circle (\radius);
    \draw (cs-center) -- ++(  0 + 180 : \radius);
    \draw (cs-center) -- ++(120 + 180 : \radius);
    \draw (cs-center) -- ++(240 + 180 : \radius);


    \newcommand{\labelraise}{1pt}
    \path[decorate,decoration={text effects along path, text={label a}, 
    raise=\labelraise, text align=center,
    text effects/every character/.style={text along path,inner xsep=0.1pt}}] (cs-center) ++(180 : \radius) arc (180 : 300 : \radius);

    \pgfmathsetmacro{\labelheight}{height("l")}
    \path[decorate,decoration={text effects along path, text={label b}, 
    raise=-\labelraise-\labelheight, text align=center,
    text effects/every character/.style={text along path,inner xsep=0.3pt}}] (cs-center) ++(180 : \radius) arc (180 : 60 : \radius);
    \end{tikzpicture}
\end{document}

在此处输入图片描述

一个可以说更简洁的解决方案就是转移第二条路径:

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

\begin{document}
    \begin{tikzpicture}
    \newcommand{\radius}{2}
    \coordinate (cs-center);

    \draw (cs-center) circle (\radius);
    \draw (cs-center) -- ++(  0 + 180 : \radius);
    \draw (cs-center) -- ++(120 + 180 : \radius);
    \draw (cs-center) -- ++(240 + 180 : \radius);


    \newcommand{\labelraise}{1pt}
    \path[decorate, decoration={text along path, text=label a, text align=center, raise=\labelraise}] (cs-center) ++(180 : \radius) arc (180 : 300 : \radius);

    \pgfmathsetmacro{\newradius}{\radius*1cm-\labelraise-height("l")}
    \path[decorate,decoration={text along path, text=label b, text align=center}] 
    (cs-center) ++(180 : {\newradius*1pt}) arc (180 : 60 : {\newradius*1pt});
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容