我想在矩形框内添加这些注释。非常感谢。到目前为止,我已经做到了这一点,但我不知道如何在框内输入更多单词。我怎样才能将这些“输入”云和“输出”云放在我想要的位置?
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
% Define block styles
\tikzstyle{block} = [rectangle, draw,
text width=15em, text centered, rounded corners, minimum height=16em]
\tikzstyle{line} = [draw, -latex']
\tikzstyle{cloud} = [draw, ellipse, node distance=6cm,
minimum height=8em]
\begin{document}
\begin{tikzpicture}[node distance = 10cm, auto]
% Place nodes
\node [block] (init) {\underline{Machine}};
\node [cloud, left of=init] (data) {\underline{Training data set}};
\node [cloud, above right of=init] (input) {\underline{Input}};
\node [cloud, below right of=init] (output) {\underline{Output}};
% Draw edges
\path [line] (data) -- (init);
\path [line] (input) -- (init);
\path [line] (init) -- (output);
\end{tikzpicture}
\end{document}
答案1
作为起点:
\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{arrows,
positioning, % <--- added
shapes}
\usepackage{xparse}% So that we can have two optional parameters
\NewDocumentCommand\DownArrow{O{2.0ex} O{black}}%
{%
\mathrel{\tikz[baseline] \draw [<-, line width=0.5pt, #2] (0,0) -- ++(0,#1);}
}
\begin{document}
\begin{tikzpicture}[
node distance = 3mm and 9mm,
block/.style = {rectangle, draw, rounded corners,
text width =15em, align=center},
cloud/.style = {draw, ellipse, aspect=1.2, align=center}
]
% Place nodes
\node [block] (init) {\underline{Machine}\\
$f(\vec{x}) = \theta_0 + \theta_1x_1 + \dotsm + \theta_n x_n$\\[1ex]
$\DownArrow[30pt]$\\[1ex]
$y=\phi\bigl(f(\vec{x})\bigr)$
};
\node [cloud, left=of init] (data)
{\underline{Training data set}\\
dog, cat, pig, etc};
\node [cloud, above right=of init.east] (input)
{\underline{Input}\\
features data $\vec{x}$};
\node [cloud, below right=of init.east] (output)
{\underline{Output}\\
class: $y$};
% Draw edges
\draw[-latex'] (data) -- (init);
\draw[-latex'] (input) -- (init.east |- input);
\draw[-latex'] (init.east |- output) -- (output);
\end{tikzpicture}
\end{document}
您走在正确的轨道上 :)。我对您的代码做了以下更改:
- 更正输入编码(应该只有一个,
article
默认使用最近的utf8
)。 - 为了定位,请使用库
positioning
及其语法(请注意above right=of ...
您的above of = ...
)。 - 不推荐使用
\tikzstyle
。相反,使用\tikzset
或将样式定义为选项tikzpicture
(就像我在上面的 MWE 中所做的那样)。 - 定义后
align=center
,可以在节点中写入多行(居中)文本
编辑: 下一次迭代(应该更接近你的草图):
\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{arrows,
positioning, % <--- added
shapes}
\usepackage{xparse}% So that we can have two optional parameters
\NewDocumentCommand\DownArrow{O{2.0ex} O{black}}%
{%
\mathrel{\tikz[baseline] \draw [<-, line width=0.5pt, #2] (0,0) -- ++(0,#1);}
}
\begin{document}
\begin{tikzpicture}[
node distance = 3mm and 9mm,
block/.style = {rectangle, draw, rounded corners,
text width =15em, align=center},
cloud/.style = {draw, ellipse, aspect=1.2, align=center},
]
% Place nodes
\node [block, label={[anchor=east, align=center, xshift=-1em,
font=\small\linespread{0.8}\selectfont,
text=purple]right:
$x_1,\dotsc,x_n$:\\
features\\
variables}
] (init) {\underline{Machine}\\
$f(\vec{x}) = \theta_0 + \theta_1 x_1 + \dotsm + \theta_n x_n$\\[1ex]
$\DownArrow[30pt]$\\[1ex]
$y=\phi\bigl(f(\vec{x})\bigr)$
};
\node [cloud, left=of init] (data)
{\underline{Training data set}\\
dog, cat, pig, etc};
\node [cloud, above right=of init.east] (input)
{\underline{Input}\\
features data $\vec{x}$};
\node [cloud, below right=of init.east] (output)
{\underline{Output}\\
class: $y$};
% Draw edges
\draw[-latex'] (data) -- (init);
\draw[red,-latex'] (input) -- (init.east |- input);
\draw[red,-latex'] (init.east |- output) -- (output);
\end{tikzpicture}
\end{document}
附录:
在过去三年中,包裹tikz
和文档类别article
经历了几次增加,上述解决方案提案现在可以/必须写成如下形式:
\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, % changed
positioning, % <--- added
shapes}
\NewDocumentCommand\DownArrow{O{2.0ex} O{black}}%
{%
\mathrel{\tikz[baseline] \draw [Straight Barb-, semithick, #2] (0,0) -- ++(0,#1);}
}
\begin{document}
\begin{tikzpicture}[
node distance = 3mm and 9mm,
block/.style = {rectangle, draw, rounded corners,
text width =15em, align=center},
cloud/.style = {draw, ellipse, aspect=1.2, align=center},
]
% Place nodes
\node [block, label={[anchor=east, align=center, xshift=-1em,
font=\small\linespread{0.8}\selectfont,
text=purple]right:
$x_1,\dotsc,x_n$:\\
features\\
variables}
] (init) {\underline{Machine}\\
$f(\vec{x}) = \theta_0 + \theta_1 x_1 + \dotsm + \theta_n x_n$\\[1ex]
$\DownArrow[30pt]$\\[1ex]
$y=\phi\bigl(f(\vec{x})\bigr)$
};
\node [cloud, left=of init] (data)
{\underline{Training data set}\\
dog, cat, pig, etc};
\node [cloud, above right=of init.east] (input)
{\underline{Input}\\
features data $\vec{x}$};
\node [cloud, below right=of init.east] (output)
{\underline{Output}\\
class: $y$};
% Draw edges
\draw[-Latex] (data) -- (init);
\draw[red,-Latex] (input) -- (init.east |- input);
\draw[red,-Latex] (init.east |- output) -- (output);
\end{tikzpicture}
\end{document}
结果几乎和以前一样: