如何确保节点或边缘处的文本位于其应在的位置而不是横跨图形?如果我可以框住节点,我会很高兴。
以下是我想做的事情:
\documentclass[border=1pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{figure}
\begin{tikzpicture}
\draw [black, thick] (0,0) node { Ethiopia} -- (4,0) node { Egypt} -- (8,4) node { South Sudan} -- (4,4) node { Sudan} -- (0,0) -- (8,4);
\draw [black, thick] (4, 0) -- (4, 4);
\draw[-] (0,0) -- node[below, red, thick] {hostile} (4,0);
\draw[-] (4,0) -- node[below, blue, thick] {friendly} (8,4);
\draw[-] (8,4) -- node[above, red, thick] {hostile} (4,4);
\draw[-] (4,4) -- node[above, blue, thick] {friendly} (0,0);
\draw[-] (0,0) -- node[left, blue, thick] {friendly} (8,4);
\draw[-] (4,0) -- node[right, blue, thick] {friendly} (4,4);
\end{tikzpicture}
\caption{Relations in January 2020} \label{fig: Relations in 2020}
\end{figure}
\begin{figure}
\begin{tikzpicture}
\draw [black, thick] (0,0) node { Ethiopia} -- (4,0) node { Egypt} -- (8,4) node { South Sudan} -- (4,4) node { Sudan} -- (0,0) -- (8,4);
\draw [black, thick] (4, 0) -- (4, 4);
\draw[-] (0,0) -- node[below, red, thick] {hostile} (4,0);
\draw[-] (4,0) -- node[below, blue, thick] {friendly} (8,4);
\draw[-] (8,4) -- node[above, blue, thick] {friendly} (4,4);
\draw[-] (4,4) -- node[above, blue, thick] {hostile} (0,0);
\draw[-] (0,0) -- node[left, blue, thick] {unknown} (8,4);
\draw[-] (4,0) -- node[right, blue, thick] {friendly} (4,4);
\end{tikzpicture}
\caption{Relations in January 2021} \label{fig: Relations in 2020}
\end{figure}
\end{document}
答案1
这就是你想要的东西吗?
我建议放置nodes
(positioning
带选项的库on grid
)并使用它们作为参考,以便轻松绘制边缘,而不是绝对坐标。使用节点,边缘在其边界处停止绘制。
\documentclass[border=1pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\begin{scope}[on grid]
\node[draw] (Ethiopia) {Ethiopia};
\node[draw, right=4cm of Ethiopia] (Egypt) {Egypt};
\node[draw, above=4cm of Egypt] (Sudan) {Sudan};
\node[draw, right=4cm of Sudan] (SSudan) {South Sudan};
\end{scope}
\draw[-] (Ethiopia) edge node[below, red, thick] {hostile} (Egypt)
edge node[sloped, above, blue, thick] {friendly} (Sudan)
edge node[sloped, above, pos=.25, blue, thick] {friendly} (SSudan);
\draw[-] (Egypt) edge node[above, sloped, near start, blue, thick] {friendly} (Sudan)
edge node[below, sloped, blue, thick] {friendly} (SSudan);
\draw[-] (Sudan) edge node[above, red, thick] {hostile} (SSudan);
\end{tikzpicture}
\end{document}