tikz-将注释文本放在行的中间

tikz-将注释文本放在行的中间

我想将注释文本(x 和 y)放在正中间。当前示例由于线条倾斜而有很大偏移。

\documentclass[border=2pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing,calc}
\begin{document}
\begin{tikzpicture}
\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[>=latex,red,<->]  (AE2) -- (BE2) node[midway,sloped,auto,align=center] {#6};
    \draw[very thin,shorten >=1pt,shorten <=1pt] (#2) -- (AE1);
    \draw[very thin,shorten >=1pt,shorten <=1pt] (#3) -- (BE1);
}
\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} ;
}
\coordinate (A) at (0,0);
\coordinate (B) at (1,2);
\draw (A) -- (B);
\annC{A}{B}{1mm}{0.5mm}{x};
\annL{B}{A}{5mm}{4mm}{y};
\end{tikzpicture}
\end{document}

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

答案1

问题在于auto您请求的节点位置。只需明确选择above或,below就应该没问题。

\documentclass[border=2pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing,calc}
\begin{document}
\begin{tikzpicture}
\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[>=latex,red,<->]  (AE2) -- (BE2) node[midway,sloped,below,align=center] {#6};
    \draw[very thin,shorten >=1pt,shorten <=1pt] (#2) -- (AE1);
    \draw[very thin,shorten >=1pt,shorten <=1pt] (#3) -- (BE1);
}
\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,above,yshift=#5]  {#6} ;
}
\coordinate (A) at (0,0);
\coordinate (B) at (1,2);
\draw (A) -- (B);
\annC{A}{B}{1mm}{0.5mm}{x};
\annL{B}{A}{5mm}{4mm}{y};
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容