tikz 节点位置自动但不居中

tikz 节点位置自动但不居中

我希望使用自动,但希望文本位于注释点之间的中心。看来 align=center 不起作用。

\documentclass[tikz,border=2mm]{standalone} 
\usetikzlibrary{decorations.pathreplacing}
\usetikzlibrary{calc}

\begin{document}
\newcommand{\annL}[6] [] {
    \coordinate (AE1) at ($(#2)!#4!90:(#3)$);
    \coordinate (BE1) at ($(#3)!#4!-90:(#2)$);
    \coordinate (AE2) at ($(#2)!#5!90:(#3)$);
    \coordinate (BE2) at ($(#3)!#5!-90:(#2)$);
    \draw[red,<->]  (AE1) -- (BE1) node[midway,sloped,auto,align=center] {#6};
    \draw[very thin,shorten >=1pt,shorten <=1pt] (#2) -- (AE2);
    \draw[very thin,shorten >=1pt,shorten <=1pt] (#3) -- (BE2);
}
\newcommand{\annC}[6] [] {
   \coordinate (AE1) at ($(#2)!#4!90:(#3)$);
    \coordinate (BE1) at ($(#3)!#4!-90:(#2)$);
   \draw [decorate,decoration={brace,amplitude=#5},xshift=0pt,yshift=0pt]
          (AE1) -- (BE1) node [black,midway,sloped,auto,yshift=#5]  {#6} ;              
}
%
\tiny\begin{tikzpicture}
\tikzset{>=latex}
    \coordinate (A) at (0,0);
    \coordinate (B) at (3,2);
    \draw[black,thick] (A) -- (B);
   \annL{A}{B}{1mm}{2mm}{line arrow};
    \annC{B}{A}{1mm}{2mm}{curly bracket};
\end{tikzpicture}
\end{document}

输出: 在此处输入图片描述

我知道上面和下面都可以正常工作,但我希望使用自动,然后我们可以对 A 到 B 和 B 到 A 的情况使用相同的命令(上下注释)。

答案1

由于您的文本始终与括号在一起,因此这是一个定义结合两个预定义装饰的新装饰的好机会:bracetext along path

\documentclass[tikz,border=2mm]{standalone} 
\usetikzlibrary{decorations.pathreplacing,decorations.text}

\begin{document}

\pgfdeclaredecoration{brace with text}{brace}
{
  \state{brace}[width=0pt,next state=text]
  {
    \pgfpathmoveto{\pgfpointorigin}
    \pgfpathcurveto
    {\pgfqpoint{.15\pgfdecorationsegmentamplitude}{.3\pgfdecorationsegmentamplitude}}
    {\pgfqpoint{.5\pgfdecorationsegmentamplitude}{.5\pgfdecorationsegmentamplitude}}
    {\pgfqpoint{\pgfdecorationsegmentamplitude}{.5\pgfdecorationsegmentamplitude}}
    {
      \pgftransformxshift{+\pgfdecorationsegmentaspect\pgfdecoratedremainingdistance}
      \pgfpathlineto{\pgfqpoint{-\pgfdecorationsegmentamplitude}{.5\pgfdecorationsegmentamplitude}}
      \pgfpathcurveto
      {\pgfqpoint{-.5\pgfdecorationsegmentamplitude}{.5\pgfdecorationsegmentamplitude}}
      {\pgfqpoint{-.15\pgfdecorationsegmentamplitude}{.7\pgfdecorationsegmentamplitude}}
      {\pgfqpoint{0\pgfdecorationsegmentamplitude}{1\pgfdecorationsegmentamplitude}}
      \pgfpathcurveto
      {\pgfqpoint{.15\pgfdecorationsegmentamplitude}{.7\pgfdecorationsegmentamplitude}}
      {\pgfqpoint{.5\pgfdecorationsegmentamplitude}{.5\pgfdecorationsegmentamplitude}}
      {\pgfqpoint{\pgfdecorationsegmentamplitude}{.5\pgfdecorationsegmentamplitude}}
    }
    {
      \pgftransformxshift{+\pgfdecoratedremainingdistance}
      \pgfpathlineto{\pgfqpoint{-\pgfdecorationsegmentamplitude}{.5\pgfdecorationsegmentamplitude}}
      \pgfpathcurveto
      {\pgfqpoint{-.5\pgfdecorationsegmentamplitude}{.5\pgfdecorationsegmentamplitude}}
      {\pgfqpoint{-.15\pgfdecorationsegmentamplitude}{.3\pgfdecorationsegmentamplitude}}
      {\pgfqpoint{0pt}{0pt}}
    }
  }
  \state{text}[width=0pt,next state=final]
  {
    \pgfpathmoveto{\pgfpointorigin}
    \pgftransformxshift{+\pgfdecorationsegmentaspect\pgfdecoratedremainingdistance}
    \pgfusepath{draw}
    \ifdim\pgfdecoratedangle pt< 90pt
      \pgfnode{rectangle}{south}{\pgfdecorationtext}{}{\pgfusepath{}}
    \else\ifdim\pgfdecoratedangle pt>270pt
      \pgfnode{rectangle}{south}{\pgfdecorationtext}{}{\pgfusepath{}}
    \else
      \pgftransformrotate{180}
      \pgfnode{rectangle}{north}{\pgfdecorationtext}{}{\pgfusepath{}}
    \fi\fi
  }
  \state{final}
  {}
}

\begin{tikzpicture}
    \foreach\i in{1,...,10}{
        \draw[decorate,decoration={brace with text,text=my text}](\i*36:1)--(\i*36+60:3);
    }
\end{tikzpicture}
\end{document}

相关内容