當邊緣標籤傾斜時,如何說服邊緣標籤位於邊緣上方/下方?

當邊緣標籤傾斜時,如何說服邊緣標籤位於邊緣上方/下方?

\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}

相關內容