tikzpicture 无法编译

tikzpicture 无法编译
\documentclass{article}
\usepackage{graphicx}
\begin{document}
\usetikzlibrary{shapes.geometric, arrows}

\tikzstyle{rect} = [draw, rectangle, fill=white!20,text width=6em, text centered, minimum height=2em]
\tikzstyle{elli} = [draw, ellipse, fill=white!20, minimum height=2em]
\tikzstyle{circ} = [draw, circle, fill=white!20,minimum width=8pt, inner sep=10pt]
\tikzstyle{diam} = [draw, diamond, fill=white!20,text width=6em, text badly centered, inner sep=0pt]
\tikzstyle{rect} = [draw, rectangle, fill=white!20,text width=6em, text centered, minimum height=2em]
\tikzstyle{line} = [draw, -latex']

\begin{figure}[h]
\begin{center}
\begin{tikzpicture}[node distance = 1.5cm,auto]
\node[rect,rounded corners] (stepl) {start};
\node[rect, below of=step1] (step2) {car};
\node[diam, below of=step2, node distance=2.5cm] (step3) {obd-ii, gps, tpms};
\node[rect, left of=step3, node distance=3.5cm] (step4) {no};
\node[rect, rounded corners, below of=step3, node distance=3cm](step5){end};
\path[line] (step1) -- (step2);
\path[line] (step2) -- (step3);
\path[line] (step3) -- node [above,near start] {no} (step4);
\path[line] (step3) -- node [left,near start] {yes} (step5);
\path[line,rounded corners] (step4) -(step1);
\end{tikzpicture}
\caption{chart.}
\label{f1}
\end{center}
\end{figure}
\end{document}

答案1

您的代码中存在相当多的错误(或至少是建议更改)。

  • 加载包tikz,而不是graphicx
  • 使用库shapesarrows.meta
  • \tikzset如下所示使用,而不是\tikzstyle
  • latex代替latex'
  • (step4) -(step1)应该(step4) |- (step1)
  • (stepl)应该(step1)
  • 放置\usetikzlibrary于之前\begin{document}

完整代码如下。

在此处输入图片描述

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes, arrows.meta}

\tikzset{
    rect/.style={draw, rectangle, fill=white!20,text width=6em, text centered, minimum height=2em},
    elli/.style={draw, ellipse, fill=white!20, minimum height=2em},
    circ/.style={draw, circle, fill=white!20,minimum width=8pt, inner sep=10pt},
    diam/.style={draw, diamond, fill=white!20,text width=6em, text badly centered, inner sep=0pt},
    rect/.style={draw, rectangle, fill=white!20,text width=6em, text centered, minimum height=2em},
    line/.style={draw, -latex}
}

\begin{document}
\begin{figure}[h]
\begin{center}
\begin{tikzpicture}[node distance = 1.5cm,auto]
\node[rect,rounded corners] (step1) {start};
\node[rect, below of=step1] (step2) {car};
\node[diam, below of=step2, node distance=2.5cm] (step3) {obd-ii, gps, tpms};
\node[rect, left of=step3, node distance=3.5cm] (step4) {no};
\node[rect, rounded corners, below of=step3, node distance=3cm](step5){end};
\path[line] (step1) -- (step2);
\path[line] (step2) -- (step3);
\path[line] (step3) -- node [above,near start] {no} (step4);
\path[line] (step3) -- node [left,near start] {yes} (step5);
\path[line,rounded corners] (step4) |- (step1);
\end{tikzpicture}
\caption{chart.}
\label{f1}
\end{center}
\end{figure}
\end{document}

答案2

  • 您的 MWE 中包含错误并且很容易丢失...
  • 需要更正的地方已在 @Sandy G 的回答中明确列出 (+1)
  • 这里有一些 pff-topic 建议,考虑这些建议后,可以使您的流程图代码更加简洁和清晰:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                chains,      % new
                positioning,
                quotes,      % new
                shapes.geometric}
                
\begin{document}
    \begin{figure}[h]
\centering
    \begin{tikzpicture}
\makeatletter
\tikzset{
  node distance = 6mm and 12mm,
    start chain = A going below,
     arr/.style = {-{Stealth[scale=0.8]}, semithick,
                   rounded corners},
     box/.style = {draw,
                   minimum width=6em, minimum height=8mm,
                   align=center, on chain, join=by arr},
decision/.style = {box, diamond, aspect=1.2, inner xsep=0pt},
every edge quotes/.style = {auto=right, font=\small},
%
suspend join/.code={\def\tikz@after@path{}}
        }
\makeatother
%
\node [box,
       rounded corners] (s1) {start};
\node [box]             (s2) {car};
\node [decision]        (s3) {obd-ii, gps,\\ tpms};
\node [box,
       rounded corners] (s5)    {end};
%
\node [box, suspend join,
       left=of s3 ] (s4) {no};
%
\draw[arr]  (s3) edge ["No"]    (s4)
            (s4) |- (s1);
\path       (s3)  to  ["Yes"]   (s5);
    \end{tikzpicture}
\caption{Flowchart, renewed :-)}
\label{fig:f1}
    \end{figure}
\end{document}

在上述 MWE 中,节点样式的名称以及它们的名称均已更改,这些名称不会干扰您的名称。建议解决方案的编译结果是:

在此处输入图片描述

相关内容