subfigure | 使其与 tikzpicture-matrix 一起工作

subfigure | 使其与 tikzpicture-matrix 一起工作

使用 subfig 包。对于包含子图图形的简单文档,效果很好。例如,以下代码效果很好。

 \documentclass{article}

 \usepackage{graphicx}
 \usepackage[caption=false,position=top]{subfig}

 \begin{document}
   Subfigure demo -- works with included graphics

 \begin{figure}[h]
 \centering
 \subfloat[first sub-figure]{
    \includegraphics[width=0.8\columnwidth]{fig1.eps}
  }
 
 \subfloat[second sub-figure]{
 \includegraphics[width=0.6\columnwidth]{fig2.eps}
 }
 \end{figure}

但是,当我尝试将 tikzpicture 与 \matrix 一起使用(而不是包含的 .eps 文件)时,它就会崩溃。

简单的 tikzpictures 可以正常工作。例如,类似以下内容的工作符合预期:

 \documentclass{article}

  \usepackage{tikz}
     \usetikzlibrary{decorations,shapes,arrows,matrix,positioning}
  \usepackage{graphicx}
  \usepackage[caption=false,position=top]{subfig}

 \begin{document}

 \begin{figure}[h]
 \centering
 \subfloat[first diag]{
 \begin{tikzpicture}
   \draw (0,0) -- (4,0) -- (4,4) -- (0,4) -- (0,0);
 \end{tikzpicture}
 }

 \subfloat[second diag]{
 \begin{tikzpicture}
    \draw (2,2) circle (3cm);
 \end{tikzpicture}
 }
 \end{figure} 
 \end{document}

但是,当我尝试涉及 \matrix(我经常使用)的操作时,出现了以下错误。

 ! Undefined control sequence.
 <argument> \pgf@matrix@last@nextcell@options 
                                         
 l.29 }
 ? 
 Process has been terminated ...

这是使用 tizkpictures 绘制非循环图的示例的 MWE。tikzpicture 代码本身可以完美编译。当使用 subfig 包嵌入到子浮点中时,它只是拒绝编译。

欢迎指出我(肯定)犯的明显错误!

\documentclass{article}

 \usepackage{tikz}
   \usetikzlibrary{decorations,shapes,arrows,matrix,positioning}
  \usepackage{graphicx}
  \usepackage{float}

  \usepackage[caption=false,position=top]{subfig}

\begin{document}

Subfigure demo - refuses to work with tikzpicture-matrix

\begin{figure}[h]
\centering
\subfloat[first life-cycle]{
\begin{tikzpicture}[>=stealth,->,shorten >=1.5pt,transform canvas={scale=0.95},line width=0.75pt]
\matrix (a) [matrix of nodes,row sep=0.5em, column sep=3em, every node/.style={circle,draw,font=\small\sffamily,minimum width=1cm}] { 1 & 2 & 3 \\};
\path[>=stealth,font=\small] (a-1-1) edge node[below] {$S_0$} (a-1-2);
\path[>=stealth,font=\small] (a-1-2) edge node[below] {$S_1$} (a-1-3);
\path[>=stealth,->,font=\small] (a-1-3) edge [bend right=55] node [above,near start] {$S_2F_3$} (a-1-1);
\end{tikzpicture}
}

\subfloat[second life-cycle]{
\begin{tikzpicture}[>=stealth,->,shorten >=1.5pt,transform canvas={scale=0.95},line width=0.75pt]
\matrix (a) [matrix of nodes,row sep=0.5em, column sep=3em, every node/.style={circle,draw,font=\small\sffamily,minimum width=1cm}] { 1 & 2 & 3 \\};
\path[>=stealth,font=\small] (a-1-1) edge node[below] {$S_0$} (a-1-2);
\path[>=stealth,font=\small] (a-1-2) edge node[below] {$S_1$} (a-1-3);
\path[>=stealth,->,font=\small] (a-1-2) edge [bend right=50] node [above,near start] {$S_1F_2$} (a-1-1) ;
\path[>=stealth,->,font=\small] (a-1-3) edge [bend right=55] node [above,near start] {$S_2F_3$} (a-1-1) ;
\end{tikzpicture}
}
\end{figure} 

\end{document}

答案1

这不是答案,而是为什么会出错的线索\subfloat。两者之间的唯一区别是\savebox

\documentclass{article}

  \usepackage{tikz}
    \usetikzlibrary{decorations,shapes,arrows,matrix,positioning}
  %\usepackage{graphicx}
  %\usepackage{float}

  %\usepackage[caption=false,position=top]{subfig}
  
\newsavebox{\tempbox}

\begin{document}

\begin{figure}[h]
\centering
\setbox\tempbox=\hbox{%
\begin{tikzpicture}[>=stealth,->,shorten >=1.5pt,scale=0.95,line width=0.75pt]
\matrix (a) [matrix of nodes,row sep=0.5em, column sep=3em, every node/.style={circle,draw,font=\small\sffamily,minimum width=1cm}]
  { 1 & 2 & 3 \\};
\path[>=stealth,font=\small] (a-1-1) edge node[below] {$S_0$} (a-1-2);
\path[>=stealth,font=\small] (a-1-2) edge node[below] {$S_1$} (a-1-3);
\path[>=stealth,->,font=\small] (a-1-3) edge [bend right=55] node [above,near start] {$S_2F_3$} (a-1-1);
\draw[red] (current bounding box.south west) rectangle (current bounding box.north east);
\end{tikzpicture}%
}\usebox\tempbox

\savebox{\tempbox}{%
\begin{tikzpicture}[>=stealth,->,shorten >=1.5pt,scale=0.95,line width=0.75pt]
\matrix (a) [matrix of nodes,row sep=0.5em, column sep=3em, every node/.style={circle,draw,font=\small\sffamily,minimum width=1cm}]
  { 1 & 2 & 3 \\};
\path[>=stealth,font=\small] (a-1-1) edge node[below] {$S_0$} (a-1-2);
\path[>=stealth,font=\small] (a-1-2) edge node[below] {$S_1$} (a-1-3);
\path[>=stealth,->,font=\small] (a-1-3) edge [bend right=55] node [above,near start] {$S_2F_3$} (a-1-1);
\draw[red] (current bounding box.south west) rectangle (current bounding box.north east);
\end{tikzpicture}%
}\usebox\tempbox
\end{figure} 

\end{document}

答案2

subcaption对于最后一个例子,只要您不使用,包的行为就会更好transform canvas=

A

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{decorations,shapes,arrows,matrix,positioning}

\usepackage{graphicx}

\usepackage{subcaption}% added <<<<<<<<<<<<

\begin{document}
        
    Subfigure demo - refuses to work with tikzpicture-matrix
    
    \begin{figure}[htp!]
        \centering
    \begin{subfigure}[t]{0.45\textwidth}% changed <<<<<
    \subcaption{first life-cycle}
            \begin{tikzpicture}[>=stealth,->,shorten >=1.5pt,
                %transform canvas={scale=0.95},
                line width=0.75pt]          
                \matrix (a) [matrix of nodes,row sep=0.5em, column sep=3em, every node/.style={circle,draw,font=\small\sffamily,minimum width=1cm}] { 1 & 2 & 3 \\};
                \path[>=stealth,font=\small] (a-1-1) edge node[below] {$S_0$} (a-1-2);
                \path[>=stealth,font=\small] (a-1-2) edge node[below] {$S_1$} (a-1-3);
                \path[>=stealth,->,font=\small] (a-1-3) edge [bend right=55] node [above,near start] {$S_2F_3$} (a-1-1);
            \end{tikzpicture}
     \end{subfigure}
 
 \bigskip
        
    \begin{subfigure}[t]{0.45\textwidth}% changed <<<<<
    \subcaption{second sub-figure}
            \begin{tikzpicture}[>=stealth,->,shorten >=1.5pt,
                %transform canvas={scale=0.95},
                line width=0.75pt]
                \matrix (a) [matrix of nodes,row sep=0.5em, column sep=3em, every node/.style={circle,draw,font=\small\sffamily,minimum width=1cm}] { 1 & 2 & 3 \\};
                \path[>=stealth,font=\small] (a-1-1) edge node[below] {$S_0$} (a-1-2);
                \path[>=stealth,font=\small] (a-1-2) edge node[below] {$S_1$} (a-1-3);
                \path[>=stealth,->,font=\small] (a-1-2) edge [bend right=50] node [above,near start] {$S_1F_2$} (a-1-1) ;
                \path[>=stealth,->,font=\small] (a-1-3) edge [bend right=55] node [above,near start] {$S_2F_3$} (a-1-1) ;
            \end{tikzpicture}
     \end{subfigure}
    \end{figure} 
    
\end{document}

答案3

您将\matrix作为参数传递给另一个命令,因此您需要ampersand replacement

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{decorations,shapes,arrows,matrix,positioning}
\usepackage{graphicx}
\usepackage[caption=false,position=top]{subfig}

\begin{document}

\begin{figure}[htp]
\centering

\subfloat[first life-cycle]{%
  \makebox[0.4\columnwidth]{\begin{tikzpicture}[
    >=stealth,
    ->,
    shorten >=1.5pt,
    transform canvas={scale=0.95},
    line width=0.75pt
  ]
  \matrix (a) [
    matrix of nodes,
    row sep=0.5em,
    column sep=3em,
    every node/.style={circle,draw,font=\small\sffamily,minimum width=1cm},
    ampersand replacement=\&,
  ] { 1 \& 2 \& 3 \\};
  \path[>=stealth,font=\small] (a-1-1) edge node[below] {$S_0$} (a-1-2);
  \path[>=stealth,font=\small] (a-1-2) edge node[below] {$S_1$} (a-1-3);
  \path[>=stealth,->,font=\small] (a-1-3) edge [bend right=55] node [above,near start]
    {$S_2F_3$} (a-1-1);
  \end{tikzpicture}\rule[-5ex]{0pt}{15ex}}%
}\qquad
\subfloat[second life-cycle]{%
  \makebox[0.4\columnwidth]{\begin{tikzpicture}[
    >=stealth,
    ->,
    shorten >=1.5pt,
    transform canvas={scale=0.95},
    line width=0.75pt,
    ampersand replacement=\&,
  ]
  \matrix (a) [
    matrix of nodes,
    row sep=0.5em, 
    column sep=3em,
    every node/.style={circle,draw,font=\small\sffamily,minimum width=1cm}
  ] { 1 \& 2 \& 3 \\};
  \path[>=stealth,font=\small] (a-1-1) edge node[below] {$S_0$} (a-1-2);
  \path[>=stealth,font=\small] (a-1-2) edge node[below] {$S_1$} (a-1-3);
  \path[>=stealth,->,font=\small] (a-1-2) edge [bend right=50] node [above,near start]
    {$S_1F_2$} (a-1-1) ;
  \path[>=stealth,->,font=\small] (a-1-3) edge [bend right=55] node [above,near start]
    {$S_2F_3$} (a-1-1) ;
  \end{tikzpicture}\rule[-5ex]{0pt}{15ex}}%
}

\end{figure} 

\end{document}

我添加了一个不可见的规则和一个装箱级别,以使边界框可接受。可能还有更好的方法。

在此处输入图片描述

相关内容