\begin{figure}... \end{figure} 无法与 tikz 包配合使用

\begin{figure}... \end{figure} 无法与 tikz 包配合使用

我正在使用tikz绘制一个图形tikzpicture,它工作正常。但是,如果我尝试使用语法插入图形

\begin{figure} ...\end{figure}

我得到了错误

\begin{document} 以 \end{figure} 结束

如果我删除,\usepackage{tikz}那么我就可以插入图形,但会出现错误tikzpicture

    \documentclass[oneside,12pt,a4paper]{book}
    \usepackage{UTMThesis}
    \usepackage{tikz}
    \begin{document}
        \centering
                \begin{tikzpicture}

                %**************** Chapter 1(blocks\nodes***************%
                \draw node[draw, below of=title, align=center] (int)at (-1,-0.7) {Chapter 1 };
                \draw node[draw, below of=int,align=center] (c12) at (-7,-2.8) {A};
                \draw node[draw, below of=int, align=center] (c13) at (-5,-4.3) {B};
                \draw node[draw, below of=int, align=center] (c14) at (-3,-2.8) {C};
                \draw node[draw, below of=int, align=center] (c15) at (-1,-4.3) {D};
                \draw node[draw, below of=int, align=center] (c16) at (1,-2.8) {E};
                \draw node[draw, below of=int, align=center] (c17) at (4,-2.8) {F};
                %************for arrow/lines**************%
                \draw[->, to path={-| (\tikztotarget)}](int.south) -- ++(0,-0.5) edge (c12);
                \draw[->, to path={-| (\tikztotarget)}](int.south) -- ++(0,-0.5) edge (c13);
                \draw[->, to path={-| (\tikztotarget)}](int.south) -- ++(0,-0.5) edge (c14);
                \draw[->, to path={-| (\tikztotarget)}](int.south) -- ++(0,-0.5) edge (c15);
                \draw[->, to path={-| (\tikztotarget)}](int.south) -- ++(0,-0.5) edge (c16);
                \draw[->, to path={-| (\tikztotarget)}](int.south) -- ++(0,-0.5) edge (c17);

                \end{tikzpicture}
            \begin{figure}[tbh]
\centering
    \includegraphics[scale=0.4]{example-image}
\end{figure}

    \end{document}

有人能帮助我吗?提前致谢。

UTMthesis.sty在这儿

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% The package UTMthesis.sty will produce the output    %

% of a thesis according to UTM Thesis Manual,          %  

% School of Graduate Studies, 2007.                    %

% Yusof Yaacob, Department of Mathematical Sciences,   %

% Faculty of Science,  Universiti Teknologi Malaysia   %

% 81310 Johor Bahru, Malaysia                          %

% e-mail: [email protected]                               %

% 24 December 2009                                     %

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% LaTeX cod for chapter header

\makeatletter
\def\@makechapterhead#1{
  %\vspace*{20\p@}   %  \vspace*{15\p@}
  {\parindent \z@ 
      \centering  
    \normalfont
    \ifnum \c@secnumdepth >\m@ne
      \if@mainmatter

      \normalsize 
           \bfseries \@chapapp\space \thechapter 
       \par\nobreak
              \vskip 1.4\baselineskip 
      \fi
    \fi
    \interlinepenalty\@M
    \normalsize     
         \bfseries #1\par\nobreak 
       \vskip 1.4\baselineskip %
  }}
\def\@schapter#1{\if@twocolumn
                   \@topnewpage[\@makeschapterhead{#1}]%
                 \else
                   \@makeschapterhead{#1}
                   \@afterheading
                 \fi}
\def\@makeschapterhead#1{
  %\vspace*{20\p@} % \vspace*{20\p@} %
  {\parindent \z@  
    \centering  
    \normalfont 
    \interlinepenalty\@M
    \normalsize 
    \bfseries  #1\par\nobreak
         \vskip 1.4\baselineskip  
  }}
\makeatother
\renewcommand{\chaptername}{CHAPTER}
%------------------------------------------------------------
% Commands for layout
   %\setlength{\topmargin}{-1.2cm}
   %\setlength{\oddsidemargin}{1.7cm}   %\setlength{\oddsidemargin}{1.46cm} 
\setlength{\textwidth}{14.5cm}
   \setlength{\textheight}{24.5cm} %  \setlength{\textheight}{24.5cm}
   %\setlength{\headheight}{2mm}
    %\setlength{\headsep}{10.175mm}
%-------------------------------------------------------------
% Command for 1.5 spacing
   %\renewcommand{\baselinestretch}{1.5}
\usepackage{doublespace}
\setstretch{1.5}
%-------------------------------------------------------------
% Command for  indentation
   \setlength\parindent{.5in}
   \usepackage{indentfirst} %1st line indented after section...
%-------------------------------------------------------------
% Command for space between paragraph
  \makeatletter
    %\setlength{\parskip}{1.5\baselineskip \@plus 3ex
                %  \@minus 2.0ex}
    \setlength{\parskip}{2\baselineskip \@plus .01ex
                  \@minus .01ex}
   \makeatother
%-------------------------------------------------------------
% Commands for no hyphenation and right justified
    \hyphenpenalty=10000 \sloppy
    \raggedbottom
%-------------------------------------------------------------
% Command to get rid of default running head
   \pagestyle{myheadings}
%-------------------------------------------------------------
% Command for  page number 
%  \makeatletter
%    \renewcommand{\ps@plain}{%
%    \renewcommand{\@oddhead}{\hfill\textrm{\thepage}}
%    \renewcommand{\@evenhead}{\@oddhead}
%    \renewcommand{\@evenfoot}{}
%    \renewcommand{\@oddfoot}{}  }
%   \makeatother
%-----------------------------------------------------------------

 %For section font
 % \usepackage{sectsty} 
  %  \chapterfont{\centering\normalsize\uppercase}
  %  \sectionfont{\normalsize\uppercase}
   % \subsectionfont{\normalsize}
   % \subsubsectionfont{\itshape\normalsize}
%---------------------------------------------------------
%--------------------------------------------------------------
% Commands for REFERENCES name
\renewcommand{\bibname}{REFERENCES}
%--------------------------------------------------------------
% Commands to change vertical space in section and subsection
\makeatletter
\renewcommand{\section}{\@startsection
  {section}%      % the name
  {1}             % the level
  {0cm}           % the indent
  {1.0cm}         % the beforeskip
  {0.01cm}    %{0.5cm}    % the afterskip
  {\normalfont\normalsize\bfseries} } % the style
\renewcommand{\subsection}{\@startsection
  {subsection}%   % the name
  {2}             % the leve2
  {0cm}           % the indent
  {1.0cm}         % the beforeskip
  {0.01cm} %{0.5cm}        % the afterskip
  {\normalfont\normalsize\bfseries} } % the style
\makeatother
%--------------------------------------------------------------
% Commands to change horizontal space in section and subsection
% for times romans
   \renewcommand{\thesection} % space between sect. etc.
     {\thechapter.\arabic{section}\hspace{.12in}}
    \renewcommand{\thesubsection} 
       {\thesection\hspace{-.3cm}.\arabic{subsection} \hspace{-.135cm}} 
%--------------------------------------------------------------
% Commands for  TocLoFT
  \usepackage{tocloft}
    %\cftsetrmarg{2.75cm} %    
     \cftsetrmarg{1.5cm} %  
       \cftsetpnumwidth{1.25cm} 
       %\cftsetpnumwidth{1.25cm}
    \renewcommand{\cftdot}{} % get rid of dots
%Table of Contents
\renewcommand{\contentsname}{}        % off tocname
\setlength{\cftchapindent}{.8cm}      % chapter indent
\setlength{\cftsecindent}{2.3cm}      % section indent
\setlength{\cftsubsecindent}{3.25cm}  % subsection indent
\setlength{\cftchapnumwidth}{1.45cm}  % chp number width
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  \renewcommand{\cftchapafterpnum}{\rule{1.75ex}{0ex}}
%   \renewcommand{\cftsecafterpnum}{\rule{1.75ex}{0ex}}
%   \renewcommand{\cftsubsecafterpnum}{\rule{1.75ex}{0ex}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  \renewcommand{\cftchapafterpnum}{\rule{0.75ex}{0ex}}
   \renewcommand{\cftsecafterpnum}{\rule{0.75ex}{0ex}}
   \renewcommand{\cftsubsecafterpnum}{\rule{0.75ex}{0ex}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   \renewcommand{\cftchappagefont}{\normalfont}
%-------------------------------------------------------------  
%List of Table
\setlength{\cfttabnumwidth}{2.5cm}  %\setlength{\cfttabnumwidth}{2.5cm}  
\renewcommand{\listtablename}{} % off lotname
%\setlength{\cfttabindent}{-.015cm}      % table indent %%%%%%%%%%%%%%%%%%
\setlength{\cfttabindent}{-.1cm}      % table indent %%%%%%%%%%%%%%%%%%
             \renewcommand{\cfttabafterpnum}{\rule{2ex}{0ex}}
\cftsetrmarg{3cm}
%------------------------------------------------------------
 %List of Figure
\setlength{\cftfignumwidth}{2.5cm}
\renewcommand{\listfigurename}{} % off lofname
\setlength{\cftfigindent}{-.1cm}      % figure indent %%%%%%%%%%%%%%%%%%
   \renewcommand{\cftfigafterpnum}{\rule{2ex}{0ex}}
%-------------------------------------------------------------
%-------------------------------------------------------------
% Command for bold caption
\makeatletter
  \long\def\@makecaption#1#2{\vskip 10\p@   % caption parameter
       \setbox\@tempboxa\hbox{{#1\,\,\,\,\,} #2} %%%%%
       \ifdim \wd\@tempboxa >\hsize         % caption parameter
           {#1\,\,\,\,\,}  #2\par  % caption parameter
       \else                                   % caption parameter
           \hbox to\hsize{\hfil\box\@tempboxa\hfil}  % caption parameter
   \fi}                                            % caption parameter
\makeatother   
\renewcommand{\thefigure}{\arabic{chapter}.\arabic{figure}}
\renewcommand{\thetable}{\arabic{chapter}.\arabic{table}}
%--------------------------------------------------------------
\usepackage{paralist} %refine list item
%--------------------------------------------------------------
\usepackage{latexsym,amsmath,amssymb} % math symbols
%--------------------------------------------------------------
\usepackage{graphicx} % .eps graphic
%--------------------------------------------------------------
\usepackage{lscape} % for landscape
%-------------------------------------------------------------- 
\usepackage{times}
%--------------------------------------------------------------
% Theorem-Like-Environment
\usepackage{theorem}
{\theorembodyfont{\upshape}
\newtheorem{lemma}{Lemma}[chapter]
\newtheorem{theorem}{\rule{0ex}{5ex}Theorem}[chapter]
\newtheorem{corollary}{Corollary}[chapter]
\newtheorem{proposition}{Proposition}[chapter]
\newtheorem{definition}{\rule{0ex}{5ex}Definition}[chapter]
\newtheorem{example}{Example}[chapter] }
%--------------------------------------------------------
\newenvironment{proof}{\begin{paragraph}
          {Proof}\vspace{-1cm}}{\end{paragraph}}
\newenvironment{solution}{\begin{paragraph}
          {Solution}\vspace{-1cm}}{\end{paragraph}}
%-----------------------------------------------------------

亲切的问候,Bibigul

答案1

编辑:

  • 将您的包插入tikzpicure浮动figure环境不会导致任何错误,UTMThesis无论是否使用您的包
  • 您的图片代码不是导致您出现问题的原因
  • 在您的文档中,您没有figure用 终止浮动\end{figure}。从您的 MWE(最小工作示例)中无法看到它在您的文档中的位置。您的 MWE 不包含figure浮动。

题外话:你的图片代码很奇怪...使用的语法是错误的。例如:

\draw node[draw, below of=title, align=center] (int) at (-1,-0.7) {Chapter 1 };

由于text width=<width>未定义,因此align=center是多余的(节点大小由节点内容大小决定,因此没有什么可以居中),但是主要问题是节点的定位:

  • title没有定义,因此下面的=title 没有意义
  • 定位 at (-1,-0.7)仅适用于节点,不适用于命令\draw node ...

此行的正确代码是:

\draw (-1,-0.7) node[draw] (int) {Chapter 1}

或更短

\node[draw] (int) at (-1,-0.7) {Chapter 1}

此外,在节点之间画线也没有必要那么复杂。可以简化(以更清晰、更简短的方式):

\draw[->] (int.south) -- ++(0,-0.5) -| (c12);

考虑到上述情况,MWE 为:

\documentclass[oneside,12pt,a4paper]{book}
\usepackage{UTMThesis}
\usepackage{tikz}

\begin{document}
    \centering
    \begin{tikzpicture}[
every node/.style = {draw}
                        ]
%**************** Chapter 1(blocks\nodes***************%
\node   (int) at (-1,-0.7) {Chapter 1 };
\node   (c12) at (-7,-2.8) {A};
\node   (c13) at (-5,-4.3) {B};
\node   (c14) at (-3,-2.8) {C};
\node   (c15) at (-1,-4.3) {D};
\node   (c16) at (1,-2.8)  {E};
\node   (c17) at (4,-2.8)  {F};
%************for arrow/lines**************%
\draw[->] (int.south) -- ++(0,-0.5) -| (c12);
\draw[->] (int.south) -- ++(0,-0.5) -| (c13);
\draw[->] (int.south) -- ++(0,-0.5) -| (c14);
\draw[->] (int.south) -- ++(0,-0.5) -| (c15);
\draw[->] (int.south) -- ++(0,-0.5) -| (c16);
\draw[->] (int.south) -- ++(0,-0.5) -| (c17);
    \end{tikzpicture}
\end{document}

这使:

在此处输入图片描述

您的图像呈现了一棵有分叉边缘的树。使用tikz库可以使其变得更加简单trees

\documentclass[oneside,12pt,a4paper]{book}
\usepackage{UTMThesis}
\usepackage{tikz}
\usetikzlibrary{trees}

\begin{document}
    \begin{figure}
    \centering
\begin{tikzpicture}[
every node/.style = {draw},
   level distance = 22mm,
 sibling distance = 15mm
                    ]
\node {Chapter 1}
    [edge from parent fork down]
    child { node {A}}
    child { node {B}}
    child { node {C}}
    child { node {D}}
    child { node {E}}
    child { node {F}};
\end{tikzpicture}
\caption{This is for test}
\end{figure}

\end{document}

在此处输入图片描述

答案2

您的标签中没有错误,我插入它只是\begin{figure}...\caption{...}\end{figure}为了测试问题,没有错误。您能详细说明您的要求吗?请参阅下面的代码,这是我尝试的:

\documentclass[oneside,12pt,a4paper]{book}
%\usepackage{UTMThesis}%%Just comment it to check the output
\usepackage{tikz}
\begin{document}
\begin{figure}
    \centering
            \begin{tikzpicture}

            %**************** Chapter 1(blocks\nodes***************%
            \draw node[draw, below of=title, align=center] (int)at (-1,-0.7) {Chapter 1 };
            \draw node[draw, below of=int,align=center] (c12) at (-7,-2.8) {A};
            \draw node[draw, below of=int, align=center] (c13) at (-5,-4.3) {B};
            \draw node[draw, below of=int, align=center] (c14) at (-3,-2.8) {C};
            \draw node[draw, below of=int, align=center] (c15) at (-1,-4.3) {D};
            \draw node[draw, below of=int, align=center] (c16) at (1,-2.8) {E};
            \draw node[draw, below of=int, align=center] (c17) at (4,-2.8) {F};
            %************for arrow/lines**************%
            \draw[->, to path={-| (\tikztotarget)}](int.south) -- ++(0,-0.5) edge (c12);
            \draw[->, to path={-| (\tikztotarget)}](int.south) -- ++(0,-0.5) edge (c13);
            \draw[->, to path={-| (\tikztotarget)}](int.south) -- ++(0,-0.5) edge (c14);
            \draw[->, to path={-| (\tikztotarget)}](int.south) -- ++(0,-0.5) edge (c15);
            \draw[->, to path={-| (\tikztotarget)}](int.south) -- ++(0,-0.5) edge (c16);
            \draw[->, to path={-| (\tikztotarget)}](int.south) -- ++(0,-0.5) edge (c17);
            \end{tikzpicture}
\caption{This is for test}
\end{figure}

\end{document}

相关内容