大家好,我在 PezzeYoung 的书上看到了这个图表。我不明白他们如何使用 LateX 创建这样的图表。我在网上搜索并尝试使用 TikZ 来创建这个想法,但我没有找到任何方法来制作这样的图表。
你能帮忙吗?
编辑:
TIKZ 设置代码
\tikzstyle{terminator} = [rectangle, draw, text centered, rounded corners, minimum height=2em]
\tikzstyle{process} = [rectangle, draw, text centered, minimum height=2em]
\tikzstyle{decision} = [diamond, draw, text centered, minimum height=2em]
\tikzstyle{data}=[trapezium, draw, text centered, trapezium left angle=60, trapezium right angle=120, minimum height=2em]
\tikzstyle{connector} = [draw, -latex']
\tikzstyle{arrow} = [thick]
我的图表代码
\begin{tikzpicture}[node distance = 2cm]
\node [process, fill=lightgray!20] (start) {\textbf{void exec(Process proc)}};
\node [terminator, below of=start] (b1) {
\begin{lstlisting}[language=Java, gobble=8, tabsize=4, showstringspaces=false]
Lock l = new Lock();
int counter;
\end{lstlisting}};
\node [terminator, below of=b1] (b2) {
\begin{lstlisting}[language=Java, gobble=8, tabsize=4, showstringspaces=false]
l.isLocked()
\end{lstlisting}};
\node [terminator, left of=b2, below of=b2] (b3) {
\begin{lstlisting}[language=Java, gobble=8]
throw new RuntimeException("Locking error 1");
\end{lstlisting}};
\draw[arrow] (start) -- (b1);
\draw[arrow] (b1) -- (b2);
\draw[arrow] (b2) -| node[anchor=east] {True} (b3);
\draw[arrow] (b2) |- node[anchor=west] {False} (b4);
\end{tikzpicture}
答案1
\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{arrows.meta}
\tikzset{
box/.style={
draw,
rectangle, rounded corners=0.4cm,
minimum height=0.8cm,
align=left,
inner xsep=10pt,
execute at end node={\hspace{10pt}},
label={[circle, draw, minimum size=0.8cm, inner sep=0pt, anchor=center, shift={(-0.4cm-0.5\pgflinewidth,-0.4cm-0.5\pgflinewidth)}]north east:#1},
execute at end node={\hspace{20pt}},
}}
\begin{document}
\begin{tikzpicture}
\node[box=b2] (A) at (0,0) {\{\\\quad char last = argStr.charAt(0);\\\quad StringBuffer argBuf = new StringBuffer\(\);\\\quad for(intcldx = 0;};
\node[box=b3] (B) at (0,-2) {cldx \textless argStr.length();\hspace{20pt}};
\draw[-{Triangle[scale=1.5]}] (A) -- (B);
\end{tikzpicture}
\end{document}