tikz:控制文本大小和位置

tikz:控制文本大小和位置

我正在尝试使用以下方法创建流程图tikz

\begin{document}

\tikzstyle{decision} = [diamond, draw, fill=blue!20, minimum size=2em, text badly centered,     node distance=2.5cm, inner sep=0pt]

\tikzstyle{block}    = [rectangle, draw, fill=blue!20, minimum size=2em, text centered, rounded corners, minimum height=4em]

\tikzstyle{line}     = [draw, very thick, color=black!50, -latex']

\tikzstyle{cloud}    = [draw, ellipse,fill=red!20, node distance=2.5cm, minimum height=2em]

\tikzstyle{circle}   = [draw, circle,fill=red!20, node distance=2.5cm, minimum height=2em]

 \tikzstyle{start}    = [circle,draw,minimum size=1.2cm ,draw=blue!80, fill=blue!20,text centered]

 \tikzstyle{decision answer}=[near start,color=black]

 \begin{tikzpicture}[scale=2, node distance = 2cm, auto]

 \tikzstyle{every node}=[midway,auto,font=\scriptsize, anchor=center]
 \matrix [column sep=5mm,row sep=7mm]
  {
\node [start] (start){start};\\
\node [block] (buff){Add new measurement, $y_k$ to buffer};\\
  };

 \end{tikzpicture}
 \end{document}

我想将文本Add new measurement, $y_k$ to buffer分成两行显示。添加时\\出现 LaTeX 错误。

使用text width=4.5em会导致线分割,但不是我想要的位置,然后文本不再居中。

答案1

如果您想要将一个节点置于另一个节点下方,请使用below of=<node name>或使用 加载positioning\usetikzlibrary{positioning}并使用below=<optional length> of <node name>

\\在节点内部使用text width对我来说是可行的。您的代码的一个大问题是您定义了一个circle使用circle内部的样式,因此递归调用自身。在这种情况下您需要使用shape=circle,但我建议使用不同的名称。也不\tikzstyle应该再使用。请参阅代码中的我的注释。

\documentclass{article}
\usepackage{tikz}
\begin{document}

% Use `\tikzset{name/.style={...}}` instead of `\tikzstyle{name} = [...]`
\tikzstyle{decision} = [diamond, draw, fill=blue!20, minimum size=2em, text badly centered,     node distance=2.5cm, inner sep=0pt]
\tikzstyle{block}    = [rectangle, draw, fill=blue!20, minimum size=2em, text centered, rounded corners, minimum height=4em]
\tikzstyle{line}     = [draw, very thick, color=black!50, -latex']
\tikzstyle{cloud}    = [draw, ellipse,fill=red!20, node distance=2.5cm, minimum height=2em]
% Recursive Reference! Use `shape=circle` instead of `circle`.
%\tikzstyle{circle}   = [draw, circle,fill=red!20, node distance=2.5cm, minimum height=2em]
\tikzstyle{start}    = [circle,draw,minimum size=1.2cm ,draw=blue!80, fill=blue!20,text centered]
\tikzstyle{decision answer}=[near start,color=black]

\begin{tikzpicture}[scale=2, node distance = 2cm, auto, every node/.style={midway,auto,font=\scriptsize, anchor=center}]
   \node [start] (start) {start};
   \node [below of=start,block,text width=4.5cm] (buff) {Add new measurement,\\ $y_k$ to buffer};
   \draw [->] (start) edge (buff);
\end{tikzpicture}
\end{document}

结果

答案2

只需使用align=center而不是text width=4.5em即可使用而\\无需设置固定宽度。

另请参阅 PGF 手册,“16.4.3 文本参数:多行文本的对齐方式和宽度”部分

相关内容