![tikz 画像でラベルの重複を防ぐにはどうすればよいでしょうか?](https://rvso.com/image/461907/tikz%20%E7%94%BB%E5%83%8F%E3%81%A7%E3%83%A9%E3%83%99%E3%83%AB%E3%81%AE%E9%87%8D%E8%A4%87%E3%82%92%E9%98%B2%E3%81%90%E3%81%AB%E3%81%AF%E3%81%A9%E3%81%86%E3%81%99%E3%82%8C%E3%81%B0%E3%82%88%E3%81%84%E3%81%A7%E3%81%97%E3%82%87%E3%81%86%E3%81%8B%3F.png)
以下の Latex コードは、私の C# プログラムによって生成されました。それぞれにラベルが付いた矢印が 3 つあります。2 番目のラベルが最初のラベルと重なっています。これを解決するには、ラベルを矢印の下に配置するか (3 番目の矢印/ラベルのように)、矢印/ラベルを完全に省略します。
重複を検出するにはどうすればいいですか?
または、現在のように矢印を基準にするのではなく、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}
もう 1 つのオプションは、矢印間の距離を増やすことです。