例如,当一个节点位于另一个节点上方且较短时,如何在两个节点之间画直线。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\usetikzlibrary{positioning}
\usepackage{amsmath,bm,times}
\usepackage{verbatim}
\begin{document}
\pagestyle{empty}
% We need layers to draw the block diagram
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}
% Define a few styles and constants
\tikzstyle{sensor}=[draw, fill=blue!20, text width=5em,
text centered, minimum height=2.5em]
\tikzstyle{sensor}=[draw, fill=blue!20, text width=5em,
text centered, minimum height=2.5em]
\tikzstyle{ann} = [above, text width=5em]
\tikzstyle{naveqs} = [sensor, text width=6em, fill=red!20,
minimum height=12em, rounded corners]
\tikzstyle{mainblock} = [above,sensor, text width=6em, fill=yellow!20,
minimum height=12em, rounded corners, dashed]
\tikzstyle{main} = [rounded corners, fill=yellow!20,
minimum width=7cm,minimum height=5cm, text centered]
\tikzstyle{submain} = [node distance=0.4cm, rounded corners, fill=red!20,
minimum width=6.5cm,minimum height=0.7cm, text centered, draw, rectangle]
\tikzstyle{subsubmain} = [node distance=0.2cm, rounded corners, fill=green!20,
minimum width=6cm,minimum height=0.7cm, text centered, draw, rectangle]
\tikzstyle{header} = [submain, fill=blue!10]
\def\blockdist{2.3}
\def\edgedist{2.5}
\begin{tikzpicture}[scale=3.0]
\node (mobprint)[main,draw,rectangle] {};
\path (mobprint.north) node (exper) [header] {MobilePrint Application};
\node (gui) [submain,below=of exper] {GUI Interface};
\node (pssub) [submain, below=of gui, minimum height=3cm] {Subsystem};
\node (opmap) [subsubmain, above=0.1cm of pssub.south,
minimum width] {Option Mapping};
\node (storage) [subsubmain, above left=0.4cm and 0cm of opmap.north east, minimum width=3cm] {Compressed Storage};
\path [draw, ->] (storage) -- (opmap);
\draw (storage) edge (opmap);
\end{tikzpicture}
\end{document}
附言:我希望连接(贮存)“压缩存储”到(opmap)“选项映射”
答案1
这是一个尝试,尽管我必须承认我不完全理解你在这里想要实现的目标。你想画一个垂直的storage
从节点到节点的线opmap
?那么coordinate system perpendicular
就是你的朋友。请参阅第 130 页的pgfmanual
。这里我们使用了隐式语法(<p>|- <q>)
。我还重新定位了Subsystem
标签,因为它被storage
节点覆盖。
最后,代码。
%\documentclass{article}
\documentclass[tikz,border=5,convert]{standalone}
%\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\usetikzlibrary{positioning}
\usepackage{amsmath,bm,times}
\usepackage{verbatim}
\begin{document}
%\pagestyle{empty}
% We need layers to draw the block diagram
%\pgfdeclarelayer{background}
%\pgfdeclarelayer{foreground}
%\pgfsetlayers{background,main,foreground}
% Define a few styles and constants
\tikzstyle{sensor}=[draw, fill=blue!20, text width=5em,
text centered, minimum height=2.5em]
\tikzstyle{sensor}=[draw, fill=blue!20, text width=5em,
text centered, minimum height=2.5em]
\tikzstyle{ann} = [above, text width=5em]
\tikzstyle{naveqs} = [sensor, text width=6em, fill=red!20,
minimum height=12em, rounded corners]
\tikzstyle{mainblock} = [above,sensor, text width=6em, fill=yellow!20,
minimum height=12em, rounded corners, dashed]
\tikzstyle{main} = [rounded corners, fill=yellow!20,
minimum width=7cm,minimum height=5cm, text centered]
\tikzstyle{submain} = [node distance=0.4cm, rounded corners, fill=red!20,
minimum width=6.5cm,minimum height=0.7cm, text centered, draw, rectangle]
\tikzstyle{subsubmain} = [node distance=0.2cm, rounded corners, fill=green!20,
minimum width=6cm,minimum height=0.7cm, text centered, draw, rectangle]
\tikzstyle{header} = [submain, fill=blue!10]
%\def\blockdist{2.3}
%\def\edgedist{2.5}
\begin{tikzpicture}[scale=3.0]
\node (mobprint)[main,draw,rectangle] {};
\path (mobprint.north) node (exper) [header] {MobilePrint Application};
\node (gui) [submain,below=of exper] {GUI Interface};
\node (pssub) [submain, below=of gui, minimum height=3cm] {};
\node (opmap) [subsubmain, above=0.1cm of pssub.south,
minimum width] {Option Mapping};
\node (storage) [subsubmain, above left=0.4cm and 0cm of opmap.north east, minimum width=3cm] {Compressed Storage};
% Draw a line from storage perpendicular to opmap
\draw [->] (storage) -- (storage |- opmap.north);
% Position the label at the top portion of pssub node
\node at (pssub.north) [anchor=north] {Subsystem};
\end{tikzpicture}
\end{document}
顺便说一句,考虑用 替换你\tikzstyle
的\tikzset
。我还注释了一些未使用的代码行。您可以在实际用例中稍后取消注释它们。此外,考虑使用newtxtext
和newtxmath
代替times
包。