使用 tikz 绘制语法图

使用 tikz 绘制语法图

我正在尝试创建这些:

在此处输入图片描述

我曾尝试使用此代码制作第一个图形

\documentclass[border=10pt]{standalone}

\usepackage{tikz}

\usetikzlibrary{arrows,automata,positioning}

\begin{document}

\begin{tikzpicture}
\node (first) [circle,draw,inner sep=1pt] {};
\node (second) [right=of first] {};
\node (third) [right=of second] {term};
\node (fourth) [right=of third] {};
\node (fifth) [circle,draw,inner sep=1pt,right=of fourth] {};

\node (sixth) [below=of fourth] {};
\node (seventh) [below=of third] {$+$};
\node (eigth) [below=of second] {};

\draw[->] (first) -- (second);
\draw[->] (second) -- (third);
\draw[->] (third) -- (fourth);
\draw[->] (fourth) -- (fifth);

\draw[->] (fourth) -- (sixth);
\draw[->] (sixth) -- (seventh);
\draw[->] (seventh) -- (eigth);
\draw[->] (eigth) -- (second);
\end{tikzpicture}

\end{document}

得出

在此处输入图片描述

我该怎么做才能使它更漂亮,没有标记节点之间的间距(我试图让它们成为坐标而不是空节点,但它不能正常工作\coordinate (...) [right=of ...];)。

我也尝试过弯曲箭头,但没有成功。应该是这样吗\draw (A) -- edge[bend right] (B);

答案1

我认为图库是一个很好的起点,里面有很多例子手动的

在此处输入图片描述

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{graphs,positioning,arrows}


\begin{document}
\begin{tikzpicture}[%
node distance=5mm,
>=stealth',
black!50,
text=black,
graphs/every graph/.style={edges=rounded corners},
skip loop/.style={to path={-- ++(0,#1) -| (\tikztotarget)}},
hv path/.style={to path={-| (\tikztotarget)}},
vh path/.style={to path={|- (\tikztotarget)}},
box/.style={%
    rectangle,
    minimum size=6mm,
    draw=black,
    top color=white, 
    bottom color=red!50!black!20, 
},
rounded/.style={%
    rectangle,minimum size=6mm,rounded corners=3mm,
    draw=black!50,
    top color=white,bottom color=black!20,
},
start/.style={%
    circle,inner sep=2pt,minimum size=2pt,fill=white,draw=black!50,
},
end/.style={%
    start,
},
]

\node[start] (start) {};
\node[right=of start] (p1) {};
\node[box,right=of p1] (term) {term};
\node[rounded,below=of term] (plus)  {+};
\node[right=of term] (p2) {};
\node[end,right=of p2] (end) {};

\graph [use existing nodes] {
    start -> term -> end;
    (plus) ->[hv path] (p1);
    (p2) ->[vh path] (plus);
};

\end{tikzpicture}
\end{document}

编辑:这与您的示例更相似:

在此处输入图片描述

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{graphs,positioning,arrows,calc}


\begin{document}
\begin{tikzpicture}[%
node distance=5mm,
>=stealth',
black!50,
text=black,
graphs/every graph/.style={edges=rounded corners},
vloop/.style={to path={-- ++(#1,0) |- (\tikztotarget)}},
hloop/.style={to path={-- ++($(0,0)-(#1,0)$) |- (\tikztotarget)}},
hv path/.style={to path={-| (\tikztotarget)}},
vh path/.style={to path={|- (\tikztotarget)}},
box/.style={%
    rectangle,
    minimum size=6mm,
    draw=black,
    top color=white, % a shading that is white at the top...
    bottom color=red!50!black!20, % and something else at the bottom
    % Font
    %font=\itshape
},
rounded/.style={%
    rectangle,minimum size=6mm,rounded corners=3mm,
    draw=black!50,
    top color=white,bottom color=black!20,
},
start/.style={%
    circle,inner sep=1pt,minimum size=1pt,fill=white,draw=black!50,
},
end/.style={%
    start,
},
junction/.style={circle,inner sep=0pt,minimum size=0pt},
]

\node[start] (start) {};
\node[junction,right=of start] (p1) {};
\node[box,right=of p1] (term) {term};
\node[rounded,below=of term] (plus)  {+};
\node[junction,right=of term] (p2) {};
\node[end,right=of p2] (end) {};

\graph [use existing nodes] {
start -> p1 -> term -> p2 -> end;
(plus) ->[hloop] term;
(term) ->[vloop] plus;
};
\end{tikzpicture}
\end{document}

相关内容