如何使用 TikZ 来放置括号而不是连接节点的“线”并在其上方添加一些文本?
答案1
自动解决方案:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,shapes.arrows,decorations.pathreplacing,shadows}
% list of keys
\pgfkeys{/tikz/.cd,
arrow color/.store in=\arrowcol,
arrow color=green!80!blue,
items distance/.store in=\itemdistance,
items distance=3cm,
border color/.store in=\bordercol,
border color=green!80!black,
fill color/.store in=\fillcol,
fill color=green!80!lime!20,
brace color/.store in=\bracecol,
brace color=green!80!blue,
brace distance/.store in=\bracedistance,
brace distance=5pt,
}
% list of styles
\tikzset{my arrow/.style={
single arrow, draw, minimum height=0.5cm,
minimum width=0.05cm,
single arrow head extend=0.1cm
},
module/.style={
rounded corners,
draw=\bordercol,
fill=\fillcol,
minimum height=1cm,
minimum width=1.5cm,
general shadow={shadow yshift=-.4ex,opacity=.4, fill=black!50, every shadow},
},
brace/.style={
decoration={brace,raise=\bracedistance,amplitude=0.75em},
decorate,
draw=\bracecol,
very thick,
}
}
% code able to reproduce arrows between two modules
\newcommand{\guparrow}{
node[my arrow,top color=\arrowcol!10,bottom color=\arrowcol,midway] {}
}
% braceddiagram:
% #1 options <optional argument>,
% #2 list of items <mandatory>,
% #3 brace comment <mandatory>
\newcommand{\braceddiagram}[3][]{
\begin{tikzpicture}[#1]% here we can pass the options for customization
% counting all the items to be displayed
\foreach \items [count=\xi] in {#2}{\global\let\maxnumitem\xi}
% foreach item in the list we are going to compute the distance it should
% be placed;
% then we put the item in a node with the module style: it also gets a
% name in the form <module><num-module>
\foreach \items [count=\xi] in {#2}{%
\path let
\n1={0+\xi*\itemdistance} in
node[module] (module\xi) at +(\n1,0) {\items};
}
% when all items are located we can put the arrows in between:
% for this we draw an imaginary path and we put the arrow between nodes;
% the path starts from the module on the left and ends in the
% subsequent module: we should compute analytically the start and end modules
\foreach \items [count=\xi] in {#2}{%
\pgfmathtruncatemacro{\xj}{mod(\xi, \maxnumitem) + 1)}
\ifnum\xi<\maxnumitem % to not have a path starting from the last module
% directed to the first one
\path (module\xj) -- (module\xi) \guparrow;
\fi
}
% at the end we draw the brace with the comment
\draw[brace] (module1.north west)--(module\maxnumitem.north east)
node[above=3*\bracedistance, midway]{#3};
\end{tikzpicture}
}
\begin{document}
\braceddiagram[brace distance=4pt]{Bla, Bla, Bla}{Some comment}
\vspace{1cm}
\tikzset{my style for the diagram/.style={
brace distance=6pt,
brace color=violet!60!magenta,
fill color=violet!60!magenta!30,
border color=violet!60!magenta,
arrow color=violet!60!magenta,
}
}
\braceddiagram[my style for the diagram]{Bla, Bla, Bla, Bla}{Some other comment}
\end{document}
结果:
代码的一些注释
要使用的基本命令是:
\braceddiagram[<options>]{<list of items>}{<comment above brace>}
在其中<list of items>
应插入一个逗号分隔的列表:每个列表将以水平方式放置,并且两个列表之间的距离由键items distance
(初始值为 3cm) 表示。注意:项目太多可能不适合页面,因此可以减少项目之间的距离或缩放图表。
缩放图表包括引入标准两个选项scale=<factor>
和transform shape
。例如:
\braceddiagram[scale=0.75,transform shape]{Bla, Bla, Bla, Bla}{Some other comment}
在<options>
可选参数中,可以引入常用的 TikZ 选项(如前所述)以及图表的特定键。当需要自定义多个键时,最好创建新样式:
\tikzset{my style for the diagram/.style={
brace distance=6pt,
brace color=violet!60!magenta,
fill color=violet!60!magenta!30,
border color=violet!60!magenta,
arrow color=violet!60!magenta,
}
}
传递给图表:
\braceddiagram[my style for the diagram,thick,scale=0.75,transform shape]{Bla, Bla, Bla, Bla}{Some other comment}