使用 TikZ 绘制矩形图

使用 TikZ 绘制矩形图

我在用 LaTeX 绘制矩形图时遇到了问题。例如,下图显示的是梯形图。我需要做哪些更改才能得到我想要的结果?

\begin{center}
\begin{tikzpicture}
  % Tell it where the nodes are
  \node (A) {$short expression$};
  \node (B) [below=of A] {$long expression$};
  \node (C) [right=of A] {$short expression$};
  \node (D) [right=of B] {$long expression$};
  % Tell it what arrows to draw
  \draw[-stealth] (A)-- node[left] {} (B);
  \draw[stealth-] (B)-- node [below] {} (D);
  \draw[stealth-] (A)-- node [above] {} (C);
  \draw[-stealth] (C)-- node [right] {} (D);,
\end{tikzpicture}
\end{center}

答案1

您需要为每个节点指定最小宽度,以便它们都具有相同的大小。如果您经常这样做,最简单的方法是为此创建一种样式,正如我在下面的示例中所做的那样:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[fixed/.style={minimum width={1.25in}}]

  % Tell it where the nodes are
  \node[fixed] (A) {$short expression$};
  \node[fixed] (B) [below=of A] {$long expression$};
  \node[fixed] (C) [right=of A] {$short expression$};
  \node[fixed] (D) [right=of B] {$long expression$};
  % Tell it what arrows to draw
  \draw[-stealth] (A)-- node[left] {} (B);
  \draw[stealth-] (B)-- node [below] {} (D);
  \draw[stealth-] (A)-- node [above] {} (C);
  \draw[-stealth] (C)-- node [right] {} (D);,
\end{tikzpicture}

\end{document}

与使用相比,此解决方案具有一定的优势,every node/.style因为如果绘图中还有其他节点,您可能不希望它们全部具有相同的大小。

答案2

matrix库非常适合这种图表。minimum width仅当您希望有相同长度的边时才没有必要。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document} 

\begin{tikzpicture}
  \matrix [matrix of math nodes,row sep=2cm,column sep=2cm,minimum width=2cm]
  {
   |(A)| \displaystyle\frac{B}{A} &   |(B)| C                  \\
   |(C)| A\times B                &   |(D)|  A\times B\times C \\
};
\draw[->]  (A)--(B);
\draw[->]  (A)--(C);
\draw[<->] (C)--(D);
\draw[->]  (B)--(D); 
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案3

您可以使用具有相同尺寸的节点,例如,您可以在这里实现它,通过为所有节点设置最小宽度,高度类似:

\begin{tikzpicture}[every node/.style={minimum width = 20ex}]
  % Tell it where the nodes are
  \node (A) {$short expression$};
  \node (B) [below=of A] {$long expression$};
  \node (C) [right=of A] {$short expression$};
  \node (D) [right=of B] {$long expression$};
  % Tell it what arrows to draw
  \draw[-stealth] (A)-- node[left] {} (B);
  \draw[stealth-] (B)-- node [below] {} (D);
  \draw[stealth-] (A)-- node [above] {} (C);
  \draw[-stealth] (C)-- node [right] {} (D);,
\end{tikzpicture}

如果您不喜欢相等的宽度,而是希望有一个矩形,那么您可以通过这种方式获得具有不同尺寸的水平线和垂直线:

\draw[-stealth] (C) -- node {} (C|-D.north);

例如

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
  \node (A) {$A\times B$};
  \node (B) [below=of A] {$\displaystyle\frac{B}{A}$};
  \node (C) [right=of A] {$C$};
  \node (D) [right=of B] {$A\times B\times C$};
  \draw[-stealth] (A)-- node[left] {} (B);
  \draw[stealth-] (B)-- node [below] {} (D);
  \draw[stealth-] (A)-- node [above] {} (C);
  \draw[-stealth] (C)-- node {} (C|-D.north);
\end{tikzpicture}
\end{document}

图表

通常,首先尝试正确定位,正如 Tom 所写。在这里,对于 D,D 可以与 C 水平居中,但随后与 B 的垂直居中就会丢失。可以以任何方式进行纠正,但我认为最好知道您可以使用或关系表达式below=of C获得垂直或水平线。|--|

答案4

这很简单:只需将节点 D 置于 B 下方:

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}

\begin{center}
\begin{tikzpicture}
  % Tell it where the nodes are
  \node (A) {$short expression$};
  \node (B) [below=of A] {$long expression$};
  \node (C) [right=of A] {$short expression$};
  \node (D) [below=of C] {$long expression$};
  % Tell it what arrows to draw
  \draw[-stealth] (A)-- node[left] {} (B);
  \draw[stealth-] (B)-- node [below] {} (D);
  \draw[stealth-] (A)-- node [above] {} (C);
  \draw[-stealth] (C)-- node [right] {} (D);,
\end{tikzpicture}
\end{center}

\end{document}

在此处输入图片描述

相关内容