我有以下代码,
\documentclass{article}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\begin{document}
\pagestyle{empty}
\definecolor{cloral}{RGB}{230, 110, 108}
\definecolor{conv}{RGB}{148, 183, 247}
\definecolor{layers}{RGB}{108, 230, 149}
% Define block styles
\tikzstyle{block} = [rectangle, draw, fill=blue!20,
text width=8em, text centered, rounded corners, minimum height=4em, minimum width=10em]
\tikzstyle{line} = [draw, -latex']
\begin{tikzpicture}[node distance = 4cm, auto]
% Place nodes
\node [block] (init)[fill=layers, xshift=-10, minimum width=12em] {\hspace{-1em}Filter concatenation};
\node [block, below of=init, fill=conv] (oneconv) {1X1 convolutions};
\node [block, right of=oneconv, fill=conv] (threeconv) {3X3 convolutions};
\node [block, right of=threeconv, fill=conv] (fiveconv) {5X5 convolutions};
\node [block, right of=fiveconv, fill=cloral] (maxpooling) {3X3 max pooling};
\node [block, below of=threeconv, fill=layers] (prev) {Previous Layer};
% Draw edges
\path [line] (oneconv) -- (init);
\path [line] (threeconv) -- (init);
\path [line] (fiveconv) -- (init);
\path [line] (maxpooling) -- (init);
\path [line] (prev) -- (oneconv);
\path [line] (prev) -- (threeconv);
\path [line] (prev) -- (fiveconv);
\path [line] (prev) -- (maxpooling);
\end{tikzpicture}
\end{document}
基本上,我只想移动第一个节点,使其位于流程图的中间。我试过使用,xshift
但似乎没有任何区别。
答案1
我没有将 4 个中间节点相对于顶部节点进行定位,而是将顶部节点和底部节点相对于这 4 个节点的中心进行定位。
为此,我创建了一个位于两个节点中间的辅助节点 (aux)(threeconv)
并(fiveconv)
使用该calc
库。
\node (aux) at ($(threeconv)!.5!(fiveconv)$){};
\node [block,above of=aux] (init)[fill=layers, minimum width=12em] {\hspace{-1em}Filter concatenation};
\node [block, below of=aux, fill=layers] (prev) {Previous Layer};
\documentclass{article}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,calc}
\begin{document}
\pagestyle{empty}
\definecolor{cloral}{RGB}{230, 110, 108}
\definecolor{conv}{RGB}{148, 183, 247}
\definecolor{layers}{RGB}{108, 230, 149}
% Define block styles
\tikzstyle{block} = [rectangle, draw, fill=blue!20,
text width=8em, text centered, rounded corners, minimum height=4em, minimum width=10em]
\tikzstyle{line} = [draw, -latex']
\begin{tikzpicture}[node distance = 4cm, auto]
% Place nodes
\node [block,fill=conv] (oneconv) {1X1 convolutions};
\node [block, right of=oneconv, fill=conv] (threeconv) {3X3 convolutions};
\node [block, right of=threeconv, fill=conv] (fiveconv) {5X5 convolutions};
\node [block, right of=fiveconv, fill=cloral] (maxpooling) {3X3 max pooling};
\node (aux) at ($(threeconv)!.5!(fiveconv)$){};
\node [block,above of=aux] (init)[fill=layers, minimum width=12em] {\hspace{-1em}Filter concatenation};
\node [block, below of=aux, fill=layers] (prev) {Previous Layer};
% Draw edges
\path [line] (oneconv) -- (init);
\path [line] (threeconv) -- (init);
\path [line] (fiveconv) -- (init);
\path [line] (maxpooling) -- (init);
\path [line] (prev) -- (oneconv);
\path [line] (prev) -- (threeconv);
\path [line] (prev) -- (fiveconv);
\path [line] (prev) -- (maxpooling);
\end{tikzpicture}
\end{document}
答案2
您无需加载任何额外的库,也无需进行复杂的计算。您需要做的就是将四个节点放在矩阵的中间。(顺便说一句,\tikzstyle
已弃用,您可以在循环中绘制箭头。)
\documentclass[tikz,border=3mm]{standalone}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\begin{document}
\pagestyle{empty}
\definecolor{cloral}{RGB}{230, 110, 108}
\definecolor{conv}{RGB}{148, 183, 247}
\definecolor{layers}{RGB}{108, 230, 149}
% Define block styles
\tikzset{block/.style={rectangle, draw, fill=blue!20,
text width=8em, text centered, rounded corners, minimum height=4em, minimum
width=10em},
line/.style={draw, -latex'}}
\begin{tikzpicture}[node distance = 4cm, auto]
% Place nodes
\node [block] (init)[fill=layers, xshift=-10, minimum width=12em] {\hspace{-1em}Filter concatenation};
\matrix[below of=init,column sep=1em] (mat){
\node [block, fill=conv] (oneconv) {1X1 convolutions}; &
\node [block, fill=conv] (threeconv) {3X3 convolutions}; &
\node [block, fill=conv] (fiveconv) {5X5 convolutions}; &
\node [block, fill=cloral] (maxpooling) {3X3 max pooling};\\
};
\node [block, below of=mat, fill=layers] (prev) {Previous Layer};
% Draw edges
\path [line] (oneconv) -- (init);
\path [line] (threeconv) -- (init);
\path [line] (fiveconv) -- (init);
\path [line] (maxpooling) -- (init);
\path [line] (prev) -- (oneconv);
\path [line] (prev) -- (threeconv);
\path [line] (prev) -- (fiveconv);
\path [line] (prev) -- (maxpooling);
\end{tikzpicture}
\end{document}
答案3
通过使用 TikZ 库calc
并chains
在循环中绘制连接线,您的代码会变得更短更清晰:
\documentclass[tikz, border=3mm]{standalone}
\usetikzlibrary{arrows.meta,
calc, chains,
positioning}
\definecolor{cloral}{RGB}{230, 110, 108}
\definecolor{conv}{RGB}{148, 183, 247}
\definecolor{layers}{RGB}{108, 230, 149}
\begin{document}
\begin{tikzpicture}[
node distance = 17mm and 2mm,
start chain = A going right,
box/.style = {rectangle, rounded corners, draw, fill=#1,
minimum width=9em, minimum height=6ex, align=center,
on chain=A},
box/.default = conv,
arr/.style = {-Latex}
]
% Place nodes
% midle row
\node [box] {$1 \times 1$ convolutions};
\node [box] {$3 \times 3$ convolutions};
\node [box] {$5 \times 5$ convolutions};
\node [box=cloral]{$3 \times 3$ max pooling};
% top
\node [box= green!30,
above=of $(A-1.north)!0.5!(A-4.north)$] {Filter concatenation};
% bottom
\node [box= green!30,
below=of $(A-1.south)!0.5!(A-4.south)$] {Previous Layer};
% Draw edges
\foreach \i in {1,...,4}
{
\draw[arr] (A-\i) -- (A-5);
\draw[arr] (A-6) -- (A-\i);
}
\end{tikzpicture}
\end{document}
绘制方式:
- 首先绘制具有名称的节点链
A-1
,...A-4
使用chains
库 - 节点链中间的上方是定位顶部节点,使用
calc
和positioning
库顶部节点 - 类似地,顶部节点添加到底部节点
- 连接线在循环中绘制。其中利用了节点的命名
A-1
、A-2
等。 - 在节点和箭头样式的定义中,样式在选项中移动
ikzpicture
,并使用最近的样式语法(˙\tikzstyle` 已过时)
答案4
所有其他节点都相对于第一个节点放置init
。因此更改它不会产生任何影响。xshift
最左边的节点oneconv
将获得中心对齐的第一个节点。
\documentclass[border=3mm]{standalone}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\begin{document}
\pagestyle{empty}
\definecolor{cloral}{RGB}{230, 110, 108}
\definecolor{conv}{RGB}{148, 183, 247}
\definecolor{layers}{RGB}{108, 230, 149}
% Define block styles
\tikzstyle{block} = [rectangle, draw, fill=blue!20,
text width=8em, text centered, rounded corners, minimum height=4em, minimum width=10em]
\tikzstyle{line} = [draw, -latex']
\begin{tikzpicture}[node distance = 4cm, auto]
% Place nodes
\node [block] (init)[fill=layers, minimum width=12em] {\hspace{-1em}Filter concatenation};
\node [block, below of=init, fill=conv,xshift=-6cm] (oneconv) {1X1 convolutions};
\node [block, right of=oneconv, fill=conv] (threeconv) {3X3 convolutions};
\node [block, right of=threeconv, fill=conv] (fiveconv) {5X5 convolutions};
\node [block, right of=fiveconv, fill=cloral] (maxpooling) {3X3 max pooling};
\node [block, below of=threeconv, fill=layers,xshift=2cm] (prev) {Previous Layer};
% Draw edges
\path [line] (oneconv) -- (init);
\path [line] (threeconv) -- (init);
\path [line] (fiveconv) -- (init);
\path [line] (maxpooling) -- (init);
\path [line] (prev) -- (oneconv);
\path [line] (prev) -- (threeconv);
\path [line] (prev) -- (fiveconv);
\path [line] (prev) -- (maxpooling);
\end{tikzpicture}
\end{document}