tikz 그림에서 레이블이 겹치는 것을 방지하는 방법은 무엇입니까?

tikz 그림에서 레이블이 겹치는 것을 방지하는 방법은 무엇입니까?

아래 라텍스 코드는 내 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}

여기에 이미지 설명을 입력하세요

또 다른 옵션은 화살표 사이의 거리를 늘리는 것입니다.

관련 정보