我很难弄清楚如何使用 TikZ 设计因果图。该图应如下所示:
下面的代码是我所能得到的,但它没有一些重要的细节(见图)。首先,如何在路径顶部添加文本(红色)?我见过使用 \draw 的示例,其中 {text} 就足够了,但如果我将其添加到我的图表中,它就不再编译了。其次,如何添加粗体文本和换行符?第三,理想情况下,箭头应该在节点之前/之后开始和结束,就像在图中一样,而不是在节点的中心(X 到 Z 和 Z 到 Y)。
\documentclass[10pt]{article}
\usepackage{color}
\usepackage{tikz}
\usetikzlibrary{shapes,decorations,arrows,calc,arrows.meta,fit,positioning}
\tikzset{
-Latex,auto,node distance =1 cm and 1 cm,semithick,
state/.style ={ellipse, draw, minimum width = 0.7 cm},
point/.style = {circle, draw, inner sep=0.04cm,fill,node contents={}},
bidirected/.style={Latex-Latex,dashed},
el/.style = {inner sep=2pt, align=left, sloped}
}
\begin{document}
\begin{tikzpicture}
\begin{tikzpicture}
\usetikzlibrary{positioning}
\node (1) at (6,0) {Some \ Text \ for \ X};
\node (2) [right = 7cm of 1] {Some \ Text \ for \ Y};
\node (3) [above right = 3cm of 1] {Some \ Text \ for \ Z};
\node (t1) [above = 0.6cm of 3] {Text bold (with line break)};
\draw (1) -- (2);
\draw (3) -- (2) [anchor=west];
\draw (1) -- (3);
\node (4) at (6,6) {Some \ Text \ for \ X};
\node (5) [right = 7cm of 4] {Some \ Text \ for \ Y};
\node (6) [above right = 3cm of 4] {Some \ Text \ for \ Z};
\node (t2) [above = 0.6cm of 6] {Text bold (with line break)};
\draw (4) -- (5);
\draw (6) -- (5);
\draw (4) -- (6);
\node (7) at (6,12) {Some \ Text \ for \ X};
\node (8) [right = 7cm of 7] {Some \ Text \ for \ Y};
\node (9) [above right = 3cm of 7] {Some \ Text \ for \ Z};
\node (t2) [above = 0.6cm of 9] {Text bold (with line break)};
\draw (7) -- (8);
\draw (9) -- (8);
\draw (7) -- (9);
\end{tikzpicture}
\end{tikzpicture}
\end{document}
答案1
让我们来看看:
- 要有多行节点,你必须指定一个
align
键;要自动换行,你需要一个text width
- 要更改节点的字体,您可以使用键
font
- 要在路径周围添加文本,您只需将其放在
node
路径中间即可。默认情况下,它是水平的并且位于中间,但您可以使用sloped
和pos
键进行更改。
示例(这是最小的,并且没有嵌套tikzpicture
......)
\documentclass[10pt]{article}
\usepackage{color}
\usepackage{tikz}
\usetikzlibrary{shapes,decorations,arrows,calc,arrows.meta,fit,positioning}
\tikzset{
-Latex,auto,node distance =1 cm and 1 cm,semithick,
state/.style ={ellipse, draw, minimum width = 0.7 cm},
point/.style = {circle, draw, inner sep=0.04cm,fill,node contents={}},
bidirected/.style={Latex-Latex,dashed},
el/.style = {inner sep=2pt, align=left, sloped}
}
\begin{document}
\begin{tikzpicture}
\node (1) at (6,0) {Some \ Text \ for \ X};
\node (2) [right = 7cm of 1] {Some \ Text \ for \ Y};
\node (3) [above right = 3cm of 1] {Some \ Text \ for \ Z};
\node (t1) [above = 0.6cm of 3, text width=4cm, align=center, font=\bfseries] {Text bold (with line break)};
\draw (1) -- node[red, above ]{text in red} (2);
\draw (3) -- node [red, above , sloped] {text in red, sloped} (2) [anchor=west];
\draw (1) -- node [red, above left, pos=0.2, align=center] {text in red,\\ horizontal at 0.2} (3);
\end{tikzpicture}
\end{document}
然后有几个库可以简化这类图表的输入;一个很好的起点是Ti 中的第二个教程钾Z 手册。
答案2
作为交换图:
\documentclass[border?3.141592]{article}
\usepackage{amsmath}
\usepackage{makecell}
\usepackage{tikz-cd}
\tikzcdset{
arrow style = tikz,
arrows = {>=Straight Barb, semithick},
% nodes = {align=center}
}
\begin{document}
\begin{tikzcd}%[ampersand replacement=\&]
& \textbf{\makecell{Text bold\\ (with line break)}}
& \\
& \text{Some Text for Z} \ar[rd, "\beta"]
& \\
\text{Some Text for X} \ar[ur, "\alpha"] \ar[rr, "\gamma" ']
& & \text{Some Text for Y}
\end{tikzcd}
\end{document}