如何防止 tikz 图片中的标签重叠?

如何防止 tikz 图片中的标签重叠?

下面的 Latex 代码是由我的 C# 程序生成的。有三个箭头,每个箭头都有一个标签。第二个标签与第一个标签重叠。为了解决这个问题,可以将标签放置在箭头下方(如第三个箭头/标签),或者可以完全省略箭头/标签。

我如何检测重叠?

或者如何将标签相对于 x 轴定位,而不是相对于其箭头定位,就像现在这样(上方/下方/省略切换)?

或者标签位置和到箭头的距离(以上=4?)与 x 轴有何关系?

\documentclass[11pt,a4paper,dutch]{article}
\usepackage[per-mode=symbol,mode=text,per-mode=symbol,exponent-product=\cdot]{siunitx}
\usepackage{xcolor}
\usepackage{rotating}
\usepackage{pgfplots}
\usepackage{tikz}
\tikzset{every picture/.style={line width=0.75pt}}
\usetikzlibrary{arrows.meta,calc,math,quotes,angles}
\tikzset{>=latex}
\tikzstyle{vector}=[->,very thick,xcol]
\pgfplotsset{compat=1.18}
\begin{document}
\begin{figure}
\begin{tikzpicture}[scale=0.9, transform shape]
\fontsize{10pt}{12pt}
\begin{axis}[
width=6cm,
height=6cm,
xmin={-0.7},xmax={5*1.1},
ymin={-0.1*1.1},ymax={2.335*1.1},
y label style={at={(axis description cs:-0.04,.5)},rotate=90,anchor=south},
grid=both,
grid style={line width=.1pt, draw=gray!20},
major grid style={line width=.2pt,draw=gray!50},
axis lines=middle,
y label style={at={(axis description cs:-0.04,.5)},rotate=90,anchor=south},
xlabel near ticks,
minor tick num=5,
enlargelimits={abs=0.5},
ticklabel style={font=\tiny,fill=white},
axis x line = bottom,
axis y line = left,
axis line style={-stealth}
] 
\def\closeby{3.2}
\def\spaced{3.7}
\coordinate(Sag1) at (axis cs: 3.5,0);
\coordinate(Sag2) at (axis cs: 3.5,1.835);
\draw[stealth-stealth] [black](Sag1) -- (Sag2)  node [above,midway, rotate=90]{$\qty{2.33}{\meter}$};
\coordinate(Sag1) at (axis cs: \closeby, 0);
\coordinate(Sag2) at (axis cs: \closeby, 2.335);
\draw[stealth-stealth] [red](Sag1) -- (Sag2)  node [above,midway, rotate=90]{$\qty{2.353}{\meter}$};
\coordinate(Sag1) at (axis cs: \spaced, 0);
\coordinate(Sag2) at (axis cs: \spaced, 2.235);
\draw[stealth-stealth] [red](Sag1) -- (Sag2)  node [below,midway, rotate=90]{$\qty{2.353}{\meter}$};

\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}

在此处输入图片描述

答案1

一种方法是:

  • 为标签添加白色背景,
  • 用箭头写标签,
  • 减小标签字体大小
  • 减少inner ysep

\documentclass[11pt,a4paper,dutch]{article}
\usepackage{xcolor}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usetikzlibrary{arrows.meta,
                calc,
                math,
                quotes,
                angles}
\tikzset{
    >=latex,
vector/.style = {->,very thick},
every picture/.style = {line width=0.75pt},
lbl/.style = {fill=white, font=\scriptsize, inner ysep=0pt, sloped}
        }
\usepackage[per-mode=symbol,
            mode=text,
            exponent-product=\cdot]{siunitx}


\begin{document}
    \begin{figure}
\begin{tikzpicture}[scale=0.9, transform shape]
\fontsize{10pt}{12pt}
\begin{axis}[
width=6cm,
height=6cm,
xmin={-0.7},xmax={5*1.1},
ymin={-0.1*1.1},ymax={2.335*1.1},
y label style={at={(axis description cs:-0.04,.5)},rotate=90,anchor=south},
grid=both,
grid style={line width=.1pt, draw=gray!20},
major grid style={line width=.2pt,draw=gray!50},
axis lines=middle,
y label style={at={(axis description cs:-0.04,.5)},rotate=90,anchor=south},
xlabel near ticks,
minor tick num=5,
enlargelimits={abs=0.5},
ticklabel style={font=\tiny,fill=white},
axis x line = bottom,
axis y line = left,
axis line style={-stealth}
]
\def\closeby{3.2}
\def\spaced{3.7}
\coordinate(Sag1) at (axis cs: 3.5,0);
\coordinate(Sag2) at (axis cs: 3.5,1.835);
\draw[stealth-stealth]      (Sag1) -- node [pos=0.3, lbl] {\qty{2.33}{\meter}}    (Sag2);

\coordinate(Sag1) at (axis cs: \closeby, 0);
\coordinate(Sag2) at (axis cs: \closeby, 2.335);
\draw[stealth-stealth,red]  (Sag1) -- node [pos=0.7, lbl] {\qty{2.353}{\meter}}   (Sag2);

\coordinate(Sag1) at (axis cs: \spaced, 0);
\coordinate(Sag2) at (axis cs: \spaced, 2.235);
\draw[stealth-stealth,red]  (Sag1) -- node [pos=0.7, lbl] {\qty{2.353}{\meter}}   (Sag2);

\end{axis}
\end{tikzpicture}
    \end{figure}
\end{document}

在此处输入图片描述

另一个选择是增加箭头之间的距离。

相关内容