海报的 TikZ 流程图

海报的 TikZ 流程图

我正在制作一张海报,需要包含一个流程图。我从来没有做过这件事,所以我从手册中的示例开始。问题是,它不起作用。

! Package pgfbasematrix Error: Single ampersand used with wrong catcode.

我搜索了一下,但目前为止没有找到任何解决方案可以解决这个问题。由于我是 TikZ 的新手,矩阵对我来说似乎更简单,所以我真的很想使用它。

这是 ME(尽管我无法编译它,并且错误与我的完整文档不同):

\documentclass{article}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,matrix}
\begin{document}
\begin{tikzpicture}
    [auto,
        decision/.style={diamond, draw=blue, thick, fill=blue!20,text width=4.5em, text badly centered,inner sep=1pt},
        block/.style={rectangle, draw=blue, thick, fill=blue!20,text width=5em, text centered, rounded corners,minimum height=4em},
        line/.style={draw, thick, -latex’,shorten >=2pt},
        cloud/.style={draw=red, thick, ellipse,fill=red!20,minimum height=2em}]

    \matrix [column sep=5mm,row sep=7mm]
      {
      % row 1
        \node [cloud] (expert){expert}; &
        \node [block] (init){initialize model}; &
        \node [cloud] (system){system}; \\
      % row 2
        & \node [block] (identify) {identify candidate model}; & \\
      % row 3
          \node [block] (update){update model}; &
          \node [block] (evaluate) {evaluate candidate models}; & \\
      % row 4
        & \node [decision] (decide) {is best candidate}; & \\
      % row 5
        & \node [block] (stop){stop}; & \\
      };
\begin{scope}[every path/.style=line]
  \path   (init)-- (identify);
  \path   (identify) -- (evaluate);
  \path   (evaluate) -- (decide);
  \path   (update)|- (identify);
  \path   (decide)-| node [near start] {yes} (update);
  \path   (decide)-- node [midway] {no} (stop);
  \path [dashed] (expert)-- (init);
  \path [dashed] (system)-- (init);
  \path [dashed] (system)|- (evaluate);
\end{scope}
\end{tikzpicture}
\end{document}

我的问题是:出了什么问题?这是我第一次接触 TikZ,所以我完全不知所措。

答案1

您对样式的定义有错误line

line/.style={draw, thick, -latex’,shorten >=2pt}

那应该是-latex',不是-latex’

此外,您提到您正在制作海报:您是否碰巧正在使用该beamerposter软件包?如果是这样,您可以将fragile选项添加到以frame消除pgfbasematrix错误:

\begin{frame}[fragile]
...

答案2

您需要用其他符号替换您的 & 符号。请参阅 TikZ/PGF 手册第 16.5 节。我需要根据 ipavlic 和 LianTze Lim 的回答/评论对 MWE 进行几处更改,但考虑到问题中引用的原始错误,我认为问题出在 & 符号上。

编辑了定义中的拼写错误lines并添加了scopes库后,这对我而言是编译成功的:

\documentclass{article}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,matrix,scopes}
\begin{document}
\begin{tikzpicture}
    [auto,
        decision/.style={diamond, draw=blue, thick, fill=blue!20,text width=4.5em, text badly centered,inner sep=1pt},
        block/.style={rectangle, draw=blue, thick, fill=blue!20,text width=5em, text centered, rounded corners,minimum height=4em},
        line/.style={draw, thick, -latex', shorten >=2pt},
        cloud/.style={draw=red, thick, ellipse,fill=red!20,minimum height=2em}]

    \matrix [column sep=5mm,row sep=7mm,ampersand replacement=\&]
      {
      % row 1
        \node [cloud] (expert){expert}; \&
        \node [block] (init){initialize model}; \&
        \node [cloud] (system){system}; \\
      % row 2
        \& \node [block] (identify) {identify candidate model}; \& \\
      % row 3
          \node [block] (update){update model}; \&
          \node [block] (evaluate) {evaluate candidate models}; \& \\
      % row 4
        \& \node [decision] (decide) {is best candidate}; \& \\
      % row 5
        \& \node [block] (stop){stop}; \& \\
      };
\begin{scope}[every path/.style=line]
  \path   (init)-- (identify);
  \path   (identify) -- (evaluate);
  \path (evaluate) -- (decide);
  \path   (update)|- (identify);
  \path   (decide)-| node [near start] {yes} (update);
  \path   (decide)-- node [midway] {no} (stop);
  \path [dashed] (expert)-- (init);
  \path [dashed] (system)-- (init);
  \path [dashed] (system)|- (evaluate);
\end{scope}
\end{tikzpicture}
\end{document}

相关内容