你好,我正在尝试创建一个如图所示的 tikz 图形。这是我的代码
\documentclass[border=4mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,positioning}
\begin{document}
\begin{tikzpicture}[
\node[rectangle] (a1) {box 1};
\node[rectangle,below right=of a1] (a2) {box 2};
\node[rectangle,above right=of a1] (a3) {box 3};
\foreach \i/\j/\txt/\p in {% start node/end node/text/position
a1/a2/1-2/below,
a1/a3/1-3/above,
a2/a3/2-3/below} \draw [myarrow] (\i) -- node[sloped,font=\small,\p] {\txt} (\j);
\end{tikzpicture}
\end{document}
我不知道如何添加矩形;如何将箭头连接到侧边;以及如何在框长度不同的情况下使箭头变直——最好箭头接触边缘的中心。
答案1
使用Tikz
shapes
库并描述节点的侧面。
\documentclass[margin=4mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,positioning,shapes}
\begin{document}
\begin{tikzpicture}[>=latex]
\tikzstyle{rect}=[ thick, draw=blue, rectangle, minimum width=100pt,
minimum height = 50pt, align=center]
\node[rect] (a1) {box 1};
\node[rect,below right=of a1] (a3) {box 3};
\node[rect,above right=of a1] (a2) {box 2};
\draw[->] (a1.north)--(a2.west)node[midway,above,xshift=-2mm]{1-2};
\draw[->] (a1.south)--(a3.west)node[midway,below,xshift=-2mm]{1-3};
\draw[->] (a2.south)--(a3.north)node[midway,xshift=-3mm]{2-3};
\end{tikzpicture}
\end{document}