\documentclass{article}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\begin{document}
\pagestyle{empty}
\tikzstyle{block} = [rectangle, draw, fill=blue!20,
text width=5em, text centered, rounded corners, minimum height=4em]
\tikzstyle{cloud} = [draw, ellipse,fill=red!20, node distance=3cm,
minimum height=2em]
\begin{figure}[ht] % 'ht' tells LaTeX to place the figure 'here' or at the top of the page
\centering % centers the figure
\scalebox{1}{%
\begin{tikzpicture}
\node[block] (data) {Data/Reality};
\node[block, right of=data] (physical) {Physical Model};
\node[block, right of=physical] (emulator) {Computer Models (emulators, etc.)};
\node[cloud, below of=data] (SU) {Structural Uncertainty};
\node[cloud, below of=physical] (AU) {Algorithmic Uncertainty};
\draw
(SU) edge[above] node{} (physical)
;
\draw[dashed]
(emulator) edge[above] node{} (AU);
\end{tikzpicture}
}
\caption{Diagram of Computer Modelling}
\label{Computer Modelling}
\end{figure}
\end{document}
此代码的问题在于节点相互影响……有没有办法自动解决这个问题,而不是仅在每个节点上使用 xshift?
答案1
正确使用node distance
节点形状不应重叠。此外,通过对节点样式进行少量重新定义,您可以轻松地将图像放入文本区域,而无需缩放图像(应避免缩放!)。
考虑到上述情况,您图像的 MWE 可以是:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,
shapes}
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\begin{document}
\begin{figure}[ht]
\centering
\begin{tikzpicture}[
node distance = 12mm and 4mm, % <---
block/.style = {draw, rounded corners, fill=blue!20,
text width=#1, align=center, minimum height=4em}, % <---
block/.default = 6em, % <---
cloud/.style = {ellipse, draw, fill=red!20,
text width=7em, align=center, inner xsep=0pt}, % <---
]
\node (data) [block] {Data/ Reality};
\node (physical) [block,right=of data] {Physical Model};
\node (emulator) [block=8em, % <---
right=of physical] {Computer Models\\ (emulators, etc.)};
\node (SU) [cloud,below=of data] {Structural\\ Uncertainty};
\node (AU) [cloud,right=of SU] {Algorithmic\\ Uncertainty};
%
\draw (SU) -- (physical);
\draw[dashed] (emulator) -- (AU);
\end{tikzpicture}
\caption{Diagram of Computer Modelling}
\label{Computer Modelling}
\end{figure}
\end{document}
笔记:
- 与 MWE 相比,代码中的变化用
% <---
(红线表示文本边框)表示 - 已弃用
\tikzstyle
。相反,它使用tikzset
或将样式写为选项tikzpicture
(如上面的 MWE 中所做的那样) - 使用
\scalebox{1}{...}
是迫不得已的行动。始终尝试设计图像,使其无需缩放即可适合文本(或某些环境)。例如,将节点中的长文本分成更多行(就像我在cloud
节点中所做的那样,为此我更改了它的样式)
答案2
我会单独定位节点。但一个自动修复方法是在图片开始后立即指定节点距离:
\begin{tikzpicture}[node distance=6cm]
或者您想要的任何距离。
答案3
更简单的方法是使用positioning library
和right=of...
而不是right of=...
。定位库在定位节点时会考虑节点大小。
\documentclass{article}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,positioning}
\begin{document}
\pagestyle{empty}
\tikzstyle{block} = [rectangle, draw, fill=blue!20,
text width=5em, text centered, rounded corners, minimum height=4em, anchor=west]
\tikzstyle{cloud} = [draw, ellipse,fill=red!20, node distance=3cm,
minimum height=2em]
\begin{figure}[ht] % 'ht' tells LaTeX to place the figure 'here' or at the top of the page
\centering % centers the figure
\scalebox{1}{%
\begin{tikzpicture}
\node[block] (data) {Data/Reality};
\node[block,right=of data] (physical) {Physical Model};
\node[block,right=of physical] (emulator) {Computer Models (emulators, etc.)};
\node[cloud,below=of data] (SU) {Structural Uncertainty};
\node[cloud,right=of SU] (AU) {Algorithmic Uncertainty};
\draw (SU) edge[above] node{} (physical);
\draw[dashed](emulator) edge[above] node{} (AU);
\end{tikzpicture}
}
\caption{Diagram of Computer Modelling}
\label{Computer Modelling}
\end{figure}
\end{document}
请注意,我必须将最后一个节点(AU)的位置设置为相对于其左节点的位置以避免重叠,因为定位库仅采用给定的节点作为参数。