我有以下代码:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{chains,shapes.multipart}
\usetikzlibrary{shapes,calc}
\usetikzlibrary{automata,arrows,shapes,snakes,automata,backgrounds,petri,positioning}
\tikzset{
myshape/.style={
rectangle split,
minimum height=1.5cm,
rectangle split horizontal,
rectangle split parts=2,
draw,
text width=2cm,
anchor=center,
}
}
\begin{document}
\begin{tikzpicture}[scale = 0.2,bend angle = 35, inner sep=2mm,
queue/.style={rectangle split, rectangle split horizontal,rectangle split draw splits = false,rectangle split part fill = {white!100,####1} , rectangle split parts=2, draw, anchor=center, minimum height = 1cm},
dispatcher/.style={rectangle,draw=blue!50!black,fill=blue!20!white,thick, minimum height = 1cm, minimum width = 1.5cm}]
\node[dispatcher] (disp) {};
\node[queue = gray!50] (queue2) [above right= 0.1cm and 3cm of disp] {$\qquad$ \nodepart{two}$\qquad \qquad \qquad \qquad$};
\node[queue = red!50] (queue1) [above=0.3cm of queue2] {$\qquad\qquad \qquad$ \nodepart{two}$\qquad \qquad$}
edge [pre, bend right,line width = 0.4mm] (disp.east)
;
\node[queue = green!50] (queue3) [below right=0.1cm and 3cm of disp] {$\qquad\qquad \qquad \qquad$ \nodepart{two}$\qquad$}
edge [pre, bend left,line width = 0.4mm] (disp.east);
\node[queue = red!50] (queue4) [below=0.3cm of queue3] {$\qquad$ \nodepart{two}$\qquad \qquad \qquad \qquad$}
edge [pre, bend left, line width = 0.4mm] (disp.east);
\draw[loosely dotted, line width = 0.4mm] (queue2) -- (queue3);
\end{tikzpicture}
\end{document}
生成以下图片:
我想知道我应该做哪些改变来做到以下几点:
- 在箭头上方放置一些文字。
- 让箭头终止于队列的西侧。
答案1
您应该明确指定箭头的开始和结束。连接这些箭头的一种简单方法是使用,edge
并使用添加文本node[above,pos=.9]{text}
。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{chains,shapes.multipart}
\usetikzlibrary{shapes,calc}
\usetikzlibrary{automata,arrows,shapes,snakes,automata,backgrounds,petri,positioning}
\tikzset{
myshape/.style={
rectangle split,
minimum height=1.5cm,
rectangle split horizontal,
rectangle split parts=2,
draw,
text width=2cm,
anchor=center,
}
}
\begin{document}
\begin{tikzpicture}[scale = 0.2,bend angle = 35, inner sep=2mm,
queue/.style={rectangle split, rectangle split horizontal,rectangle split draw splits = false,rectangle split part fill = {white!100,#1} , rectangle split parts=2, draw, anchor=center, minimum height = 1cm},
dispatcher/.style={rectangle,draw=blue!50!black,fill=blue!20!white,thick, minimum height = 1cm, minimum width = 1.5cm}]
\node[dispatcher] (disp) {};
\node[queue = gray!50] (queue2) [above right= 0.1cm and 3cm of disp] {$\qquad$ \nodepart{two}$\qquad \qquad \qquad \qquad$};
\node[queue = red!50] (queue1) [above=0.3cm of queue2] {$\qquad\qquad\qquad$ \nodepart{two}$\qquad \qquad$};
\node[queue = green!50] (queue3) [below right=0.1cm and 3cm of disp] {$\qquad\qquad\qquad\qquad$\nodepart{two}$\qquad$};
\node[queue = red!50] (queue4) [below=0.3cm of queue3] {$\qquad$\nodepart{two}$\qquad\qquad\qquad\qquad$};
\draw[loosely dotted, line width = 0.4mm] (queue2) -- (queue3);
\path (disp.east) edge [->, bend right,line width = 0.4mm]node[above,pos=.85]{text3} (queue3.west)
edge [->, bend right,line width = 0.4mm]node[above,pos=.9]{text4} (queue4.west)
edge [->, bend left,line width = 0.4mm]node[above,pos=.9]{text1} (queue1.west);
\end{tikzpicture}
\end{document}
答案2
####1
直到我在第 19 行替换后,我才能编译你的代码。#1
如果您有更复杂的注释,您可能会发现用明确的命令替换循环更方便。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{chains,shapes.multipart}
\usetikzlibrary{shapes,calc}
\usetikzlibrary{automata,arrows,shapes,snakes,automata,backgrounds,petri,positioning}
\tikzset{
myshape/.style={
rectangle split,
minimum height=1.5cm,
rectangle split horizontal,
rectangle split parts=2,
draw,
text width=2cm,
anchor=center,
}
}
\begin{document}
\begin{tikzpicture}[scale = 0.2,bend angle = 35, inner sep=2mm,
queue/.style={rectangle split, rectangle split horizontal,rectangle split draw splits = false,rectangle split part fill = {white!100,#1} , rectangle split parts=2, draw, anchor=center, minimum height = 1cm},
dispatcher/.style={rectangle,draw=blue!50!black,fill=blue!20!white,thick, minimum height = 1cm, minimum width = 1.5cm}]
\node[dispatcher] (disp) {};
\node[queue = gray!50] (queue2) [above right= 0.1cm and 3cm of disp] {$\qquad$ \nodepart{two}$\qquad \qquad \qquad \qquad$};
\node[queue = red!50] (queue1) [above=0.3cm of queue2] {$\qquad\qquad \qquad$ \nodepart{two}$\qquad \qquad$}(disp.east);
\node[queue = green!50] (queue3) [below right=0.1cm and 3cm of disp] {$\qquad\qquad \qquad \qquad$ \nodepart{two}$\qquad$} (disp.east);
\node[queue = red!50] (queue4) [below=0.3cm of queue3] {$\qquad$ \nodepart{two}$\qquad \qquad \qquad \qquad$} (disp.east);
\draw[loosely dotted, line width = 0.4mm] (queue2) -- (queue3);
\foreach \a in {1,...,4}
{\draw[ultra thick, in=180,out=50-\a*20,thick,->] (disp.east) to
node[midway,above] {\a}
(queue\a.west);
};
\end{tikzpicture}
\end{document}
答案3
稍微修改一下就好了土拨鼠答案。采用 beamer 文档类,简化箭头、使用chains
和quotes
库。所有未使用的tikz
库和样式定义都被删除以获得 mwe(最小工作示例):
\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, chains, quotes, positioning, shapes.multipart}
\tikzset{scheduler/.style = {
node distance = 3mm and 33mm,
start chain = A going below,
queue/.style = {rectangle split, rectangle split horizontal, rectangle split parts=2,
rectangle split draw splits=false,
rectangle split part fill={white!100,##1},
draw, minimum height = 1cm,
on chain=A},
dispatcher/.style = {rectangle, draw=blue!50!black, fill=blue!20!white, thick,
minimum height=1cm, minimum width = 1.5cm},
every edge quotes/.append style = {auto, anchor=south east, sloped, pos=0.9}
}}
\begin{document}
\begin{frame}
\frametitle{Scheduler}
\begin{tikzpicture}[scheduler]
\node[queue=gray] {$\qquad$
\nodepart{two}$\qquad \qquad \qquad \qquad$};
\node[queue=red] {$\qquad\qquad \qquad$
\nodepart{two}$\qquad \qquad$};
\node[queue=green,below=16mm of A-2]
{$\qquad\qquad \qquad \qquad$
\nodepart{two}$\qquad$};
\node[queue=red!50] {$\qquad$
\nodepart{two}$\qquad \qquad \qquad \qquad$};
%
\node[dispatcher,below left=of A-2] (disp) {};
%
\draw[loosely dotted, line width=0.4mm,
shorten >=1mm, shorten <=1mm] (A-2) -- (A-3);
\path[thick,-Stealth]
(disp.east) edge ["text 1"] (A-1.west)
(disp.east) edge ["text 2"] (A-2.west)
(disp.east) edge ["text 3"] (A-3.west)
(disp.east) edge ["text 4"] (A-4.west);
\end{tikzpicture}
\end{frame}
\end{document}
答案4
我想这是首先定义节点的最简单的方法:
\documentclass{article}
...
\node[dispatcher] (disp) {};
\node[queue = gray!50] (queue2) [above right= 0.1cm and 3cm of disp] {$\qquad$ \nodepart{two}$\qquad \qquad \qquad \qquad$};
\node[queue = red!50] (queue1) [above=0.3cm of queue2] {$\qquad\qquad \qquad$ \nodepart{two}$\qquad \qquad$};
\node[queue = green!50] (queue3) [below right=0.1cm and 3cm of disp] {$\qquad\qquad \qquad \qquad$ \nodepart{two}$\qquad$};
\node[queue = red!50] (queue4) [below=0.3cm of queue3] {$\qquad$ \nodepart{two}$\qquad \qquad \qquad \qquad$};
其次画出箭头并标注:
\draw[<-, bend right, line width = 0.4mm] (queue1) -- (disp) node [auto] {your text};
\draw[<-, bend left, line width = 0.4mm] (queue3) -- (disp) node [auto] {your text};
\draw[<-, bend left, line width = 0.4mm] (queue4) -- (disp) node [auto] {your text};
\draw[loosely dotted, line width = 0.4mm] (queue2) -- (queue3);
\end{tikzpicture}
\end{document}
这个帖子可能会对您的问题有所帮助。
我不知道你是否知道TikZ & PGF 手册?本手册一开始可能有点让人不知所措,但我认为它非常有用。看看第 3.10 章和第 3.12 章,它们应该也能帮助你解答问题。:-)