我正在尝试为我的任务图(嵌入式编程)创建一个缓冲区符号,但我在符号顶部看到一个箭头,它应该是一条线,就像框中文本下方的线一样。本质上我只想要一个垂直线不可见的矩形。我的代码如下:
\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[danish]{babel}
\usepackage{tikz}
\usetikzlibrary{automata,arrows}
\usetikzlibrary{shapes,snakes}
\tikzset{
data/.style={
draw,
rectangle split,
rectangle split parts=4,
text centered,
},
data+/.style={
data,
rectangle split every empty part={},% resets empty-part macro (explanation below)
rectangle split empty part width=\widthof{#1},
rectangle split empty part height=\heightof{#1},
rectangle split empty part depth=\depthof{#1},
},
}
\makeatletter
\pgfdeclareshape{datastore}{
\inheritsavedanchors[from=rectangle]
\inheritanchorborder[from=rectangle]
\inheritanchor[from=rectangle]{center}
\inheritanchor[from=rectangle]{base}
\inheritanchor[from=rectangle]{north}
\inheritanchor[from=rectangle]{north east}
\inheritanchor[from=rectangle]{east}
\inheritanchor[from=rectangle]{south east}
\inheritanchor[from=rectangle]{south}
\inheritanchor[from=rectangle]{south west}
\inheritanchor[from=rectangle]{west}
\inheritanchor[from=rectangle]{north west}
\backgroundpath{
% store lower right in xa/ya and upper right in xb/yb
\southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
\northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y
\pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@ya}}
\pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@ya}}
\pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@yb}}
\pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@yb}}
}
}
\makeatother
\begin{document}
\begin{tikzpicture}[
scale=0.8,
->,>=stealth',
shorten >=1pt,
auto,
node distance=3.5cm,
transform shape,
every node/.style={font=\sffamily\scriptsize},
buffer/.style={rectangle,draw,minimum width=1.5cm,minimum height=1.1cm,align=center},
interrupt/.style={diamond,draw,minimum width=1.5cm,minimum height=1.5cm,align=center},
datastore/.style={draw,shape=datastore,inner sep=.3cm}
]
\node[state,minimum width=1.5cm] (1) {Task};
\node[interrupt] (2) [right of=1 ]{Interrupt};
\node[buffer] (3) [right of=2] {Buffer};
\node[datastore] (4) [right of=3] {Buffer};
\end{tikzpicture}
\end{document}
输出结果如下:
正如你所看到的,最后一个数字并不漂亮:) 如有任何帮助我们将不胜感激,提前谢谢!
答案1
\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[danish]{babel}
\usepackage{tikz}
\usetikzlibrary{automata,arrows}
\usetikzlibrary{shapes,snakes}
\tikzset{
data/.style={
draw,
rectangle split,
rectangle split parts=4,
text centered,
},
data+/.style={
data,
rectangle split every empty part={},% resets empty-part macro (explanation below)
rectangle split empty part width=\widthof{#1},
rectangle split empty part height=\heightof{#1},
rectangle split empty part depth=\depthof{#1},
},
}
\makeatletter
\pgfdeclareshape{datastore}{
\inheritsavedanchors[from=rectangle]
\inheritanchorborder[from=rectangle]
\inheritanchor[from=rectangle]{center}
\inheritanchor[from=rectangle]{base}
\inheritanchor[from=rectangle]{north}
\inheritanchor[from=rectangle]{north east}
\inheritanchor[from=rectangle]{east}
\inheritanchor[from=rectangle]{south east}
\inheritanchor[from=rectangle]{south}
\inheritanchor[from=rectangle]{south west}
\inheritanchor[from=rectangle]{west}
\inheritanchor[from=rectangle]{north west}
\backgroundpath{
% store lower right in xa/ya and upper right in xb/yb
\southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
\northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y
\pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@ya}}
\pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@ya}}
\pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@yb}}
\pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@yb}}
}
}
\makeatother
\begin{document}
\begin{tikzpicture}[
scale=0.8,
->,>=stealth',
shorten >=1pt,
auto,
node distance=3.5cm,
transform shape,
every node/.style={font=\sffamily\scriptsize},-,% <- protect node from arrow setttings
buffer/.style={rectangle,draw,minimum width=1.5cm,minimum height=1.1cm,align=center},
interrupt/.style={diamond,draw,minimum width=1.5cm,minimum height=1.5cm,align=center},
datastore/.style={draw,shape=datastore,inner sep=.3cm}
]
\node[state,minimum width=1.5cm] (1) {Task};
\node[interrupt] (2) [right of=1 ]{Interrupt};
\node[buffer] (3) [right of=2] {Buffer};
\node[datastore] (4) [right of=3] {Buffer};
\end{tikzpicture}
\end{document}
答案2
问题是,当您定义\backgroundpath
新的节点形状时,您只声明了路径,但没有声明如何绘制它(线宽、线条颜色、箭头提示等),这些都是从范围继承的。
由于您的图片使用了选项->
,因此默认情况下所有路径的末尾都会有一个箭头(除非使用[-]
特定路径的选项覆盖此选项)。
如果您想创建一个样式不依赖于环境选项的形状(例如,边框必须始终为红色,或始终为粗体等),您可以在 而不是 中绘制它\behindbackgroundpath
。使用这种方法,您的代码不仅可以包含等,还\backgroundpath
可以包含,例如,以及在描边之前设置图形选项的代码。\pgfmoveto{}
\pgfusepath{stroke}
例如:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,arrows}
\makeatletter
\pgfdeclareshape{datastore}{
\inheritsavedanchors[from=rectangle]
\inheritanchorborder[from=rectangle]
\inheritanchor[from=rectangle]{center}
\inheritanchor[from=rectangle]{base}
\inheritanchor[from=rectangle]{north}
\inheritanchor[from=rectangle]{north east}
\inheritanchor[from=rectangle]{east}
\inheritanchor[from=rectangle]{south east}
\inheritanchor[from=rectangle]{south}
\inheritanchor[from=rectangle]{south west}
\inheritanchor[from=rectangle]{west}
\inheritanchor[from=rectangle]{north west}
\behindbackgroundpath{
% store lower right in xa/ya and upper right in xb/yb
\southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
\northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y
\pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@ya}}
\pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@ya}}
\pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@yb}}
\pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@yb}}
% Draw it, always blue, arrowless and .8mm width
\pgfsetlinewidth{.8mm}\pgfsetarrows{-}\pgfsetstrokecolor{blue}\pgfusepath{stroke}
}
}
\makeatother
\begin{document}
\begin{tikzpicture}[->, >=stealth']
\node[datastore, red] (1) {Buffer};
\node[datastore, black, right=of 1] (2) {Buffer};
\node[datastore, black!40!green, right=of 2] (3) {Buffer};
\draw (1) edge (2) (2) edge (3);
\end{tikzpicture}
\end{document}
请注意,传递的颜色选项node
不会影响背景路径(但会影响文本)。
答案3
正如@Tarass所建议的,代码中的第三行应该省略,我应该使用另一种符号来绘制箭头。这是工作代码:
\begin{tikzpicture}[
scale=0.5,
%->,>=stealth',
shorten >=1pt,
auto,
node distance=3.5cm,
transform shape,
every node/.style={font=\sffamily\scriptsize},
buffer/.style={rectangle,draw,minimum width=1.5cm,minimum height=1.1cm,align=center},
interrupt/.style={diamond,draw,minimum width=1.5cm,minimum height=1.5cm,align=center,text width = 1.5cm, text centered},
datastore/.style={draw,shape=datastore,inner sep=.2cm,text width = 1.5cm, text centered}
]
\node[state,minimum width=1.5cm] (1) {Main Task};
% LCD
\node[buffer] (2) at(4,2) {LCD Buffer};
\node[state,minimum width=1.5cm] (3) at(7.5,2) {LCD task};
% UART
\node[datastore] (4) at(4,0) {Transmit Queue};
\node[datastore] (5) at(4,-1.5) {Receive Queue};
\node[state] (6) at(7.5,-0.75) {Uart task};
% Fan
\node[datastore] (7) at(4,-3.5) {Pump Queue};
\node[datastore] (8) at(4,-5) {Expected Liter Fuel};
\node[state] (9) at(7.5,-4.25) {Pump task};
% Fan
\node[datastore] (10) at(4,-7) {Liter Fuel};
\node[state] (11) at(7.5,-7) {Encoder task};
\node[interrupt] (12) at(11,-7) {Encoder interrupt};
\draw[->,>=stealth'] (1) -- (2) node[above] { };
\draw[->,>=stealth'] (1) -- (4) node[above] { };
\draw[->,>=stealth'] (1) -- (5) node[above] { };
\draw[->,>=stealth'] (1) -- (7) node[above] { };
\draw[->,>=stealth'] (1) -- (8) node[above] { };
\draw[->,>=stealth'] (1) -- (10) node[above] { };
\end{tikzpicture}
输出如下: