当边缘标签倾斜时,如何确保边缘标签位于边缘上方/下方?

当边缘标签倾斜时,如何确保边缘标签位于边缘上方/下方?

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{quotes}

\begin{document}
    \begin{tikzpicture}[auto]
\draw (0,0) to ["AA" '] + (2,0);
\draw (0,0) to ["BB"]   + (0,2);
%
\draw (3,0) to ["AA" ']      +(2,0);
\draw (3,0) to ["BB",sloped] +(0,2);
    \end{tikzpicture}
\end{document}

我得到

在此处输入图片描述

如果我预定义边缘引号例如

every edge quotes/.append style = {auto=left, font=\footnotesize\sffamily,
                                   anchor=south, sloped}

或选项对标签定位没有任何影响(由于锚点是固定的,所以这是预料之中的)swap'

有没有办法将第二幅图像中的标签放置在一条线的上方/左侧或下方/右侧,同时对选项swap(或')敏感?

答案1

不确定这是否是您所期望的。

\documentclass[tikz]{standalone}

\begin{document}

\makeatletter
\def\tikz@install@auto@anchor@sloped{\let\tikz@do@auto@anchor=\tikz@auto@anchor@on\def\tikz@auto@anchor@direction{sloped}}
\def\tikz@install@auto@anchor@sroped{\let\tikz@do@auto@anchor=\tikz@auto@anchor@on\def\tikz@auto@anchor@direction{sroped}}
\def\tikz@auto@anchor@sloped{\pgfslopedattimetrue\tikz@auto@pre\tikz@auto@anchor@sloped@\tikz@auto@post}
\def\tikz@auto@anchor@sroped{\pgfslopedattimetrue\tikz@auto@pre\tikz@auto@anchor@sroped@\tikz@auto@post}
\def\tikz@auto@anchor@sloped@{%
    \ifdim\pgf@x<0pt%
        \def\tikz@anchor{north}%
    \else%
        \def\tikz@anchor{south}%
    \fi%
}
\def\tikz@auto@anchor@sroped@{%
    \ifdim\pgf@x<0pt%
        \def\tikz@anchor{south}%
    \else%
        \def\tikz@anchor{north}%
    \fi%
}
\tikzoption{swap}[]{%
    \def\tikz@swap@left@to{right}%
    \def\tikz@swap@right@to{left}%
    \def\tikz@swap@sloped@to{sroped}%
    \def\tikz@swap@sroped@to{sloped}%
    \edef\tikz@auto@anchor@direction{\csname tikz@swap@\tikz@auto@anchor@direction @to\endcsname}
}

\tikz\draw foreach\i in{3,6,...,36}{(0,0)--node[auto=sloped]{ABC}+(\i0:3)};

\tikz\draw foreach\i in{3,6,...,36}{(0,0)--node[auto=sloped,']{ABC}+(\i0:3)};

\tikz\draw foreach\i in{3,6,...,36}{(0,0)--node[auto=sroped]{ABC}+(\i0:3)}; % sroped = sloped'

\end{document}

相关内容