我想创建一个带有注释的流程图:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{tikz}
\usetikzlibrary{shapes,backgrounds,calc}
\begin{document}
\begin{tikzpicture}[node distance = 1.7cm, auto]
\tikzset{
line/.style = {draw},
comment/.style = {rectangle, draw, text centered, rounded corners, minimum height=2em,fill=white},
terminator/.style = {shape=rounded rectangle, draw, inner sep=2mm},
}
\node[terminator] (node-1) {begin};
\node [comment, below of=node-1] (node-2) {comment comment comment};
\begin{scope}[on background layer]
\draw[fill=gray!40] ($(node-2.south west)+(-2mm,-2mm)$) rectangle ($(node-2.north east)+(2mm, 2mm)$);
\end{scope}
\path [line] (node-1) -- ($(node-2.north)+(0mm,+2mm)$);
\node[terminator, below of=node-2, node distance=1.5cm] (node-3) {end};
\path [line] ($(node-2.south)+(0mm,-2mm)$) -- (node-3);
\end{tikzpicture}
\end{document}
它给出了这个:
但评论不是块,而是两个元素:背景层和圆角矩形。如何使用锚点从中创建新形状?
答案1
这很棘手,但可以奏效。您可以绘制并使用选项在其上background node
放置一个。label node
center
问题:您必须调整主节点大小,重复相同的文本并进行修复inner sep
。优点:您可以使用main node
锚点来绘制边缘。
\documentclass[tikz,border=2mm]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usetikzlibrary{shapes}
\begin{document}
\begin{tikzpicture}[node distance = 1.7cm, auto]
\tikzset{
line/.style = {draw},
comment/.style = {rectangle, draw, text centered,
rounded corners, minimum height=2em,fill=white},
comment border/.style = {comment, rounded corners=0pt,
fill=gray!40, inner sep=4mm, minimum height=2em+4mm},
terminator/.style = {shape=rounded rectangle, draw, inner sep=2mm},
}
\node[terminator] (node-1) {begin};
\node [comment border, below of=node-1,
label={[comment]center:comment comment comment}]
(node-2) {comment comment comment};
\node[terminator, below of=node-2, node distance=1.5cm] (node-3) {end};
\draw (node-1)--(node-2);
\draw (node-2)--(node-3);
\end{tikzpicture}
\end{document}
如果你不想使用这个技巧,可以尝试makeshape
今天在 CTAN 上宣布了这一消息。
编辑:2on 选项
另一个不太棘手的选择是使用matrix of nodes
节点。Amatrix
是node
包含其他 的nodes
,因此可以对矩阵和每个节点应用不同的样式。
comment/.style = {matrix of nodes,
% Next lines apply to matrix and all inner nodes
draw, fill=gray!40, inner sep=5pt,
% Next lines define style for inner nodes.
% It's possible to change previous options
nodes={text centered, rounded corners, minimum height=2em,
fill=white, inner sep=3pt}
},
所有行内都必须以'\'结尾,所以我们在使用节点和包含在部分内matrix
时需要小心:comment
\\
text
\node [comment, below of=node-1]
(node-2) {comment comment comment\\};
结果是一样的,但是代码变成了:
\documentclass[tikz,border=2mm]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usetikzlibrary{shapes,matrix} %<- Don't forget matrix library
\begin{document}
\begin{tikzpicture}[node distance = 1.7cm, auto]
\tikzset{
line/.style = {draw},
comment/.style = {matrix of nodes,
% Next lines apply to matrix and all inner nodes
draw, fill=gray!40, inner sep=5pt,
% Next lines define style for inner nodes.
% It's possible to change previous options
nodes={text centered, rounded corners, minimum height=2em,
fill=white, inner sep=3pt}
},
terminator/.style = {shape=rounded rectangle, draw, inner sep=2mm},
}
\node[terminator] (node-1) {begin};
\node [comment, below of=node-1]
(node-2) {comment comment comment\\};
\node[terminator, below of=node-2, node distance=1.5cm] (node-3) {end};
\draw (node-1)--(node-2);
\draw (node-2)--(node-3);
\end{tikzpicture}
答案2
解决方案如下append path picture
:
\documentclass[tikz]{standalone}
\usetikzlibrary{positioning}
\tikzset{
line/.style = {draw},
comment/.style = {
draw, text centered,
minimum height=2em+4mm, inner xsep=.3333em+4mm,
append after command={\pgfextra{
\pgfinterruptpath
% fill white
\fill[white] (\tikzlastnode.south west)
rectangle (\tikzlastnode.north east);
% fill gray region
\fill[fill=gray,even odd rule,draw]
(\tikzlastnode.south west) rectangle (\tikzlastnode.north east)
[rounded corners]
([shift={(2mm,2mm)}]\tikzlastnode.south west)
rectangle ([shift={(-2mm,-2mm)}]\tikzlastnode.north east);
\endpgfinterruptpath
}
},
},
terminator/.style = {rounded corners, draw, inner sep=2mm},
}
\begin{document}
\begin{tikzpicture}[on grid,node distance=1.7cm]
\node[terminator] (node-1) {begin};
\node [comment, below=1.7cm of node-1] (node-2) {comment comment comment};
\path [line] (node-1) -- (node-2);
\node[terminator, below=1.5cm of node-2] (node-3) {end};
\path [line] (node-2) -- (node-3);
\end{tikzpicture}
\end{document}