10

I tried to create a figure which consists of two subfigures, each of which is a TikZ picture.

\documentclass[a4paper,10pt]{article}
\usepackage{amsmath,amssymb,amsfonts,amsthm,tikz,caption,subcaption}

\begin{document}

\begin{figure}
\begin{center}

\begin{subfigure}
\begin{tikzpicture}[scale=1]

  \path (0,0) coordinate (P0) node[right=0.1cm] {P0};
  \fill (P0) circle (2pt);

\end{tikzpicture}
\caption{Hello P1} \label{fig:M1}
\end{subfigure}

\begin{subfigure}
\begin{tikzpicture}[scale=1]

  \path (0,0) coordinate (P0) node[right=0.1cm] {P0};
  \fill (P0) circle (2pt);

\end{tikzpicture}
\caption{Hello P2} \label{fig:M2}
\end{subfigure}


\end{center}
\end{figure}

\end{document}

But compiling this file gives the error

! Missing \endcsname inserted.
<to be read again> 
                   \relax 
l.10 \begin
           {tikzpicture}[scale=1]

Why is that?

11

The subfigure environment has a mandatory argument specifying the width:

\documentclass[a4paper,10pt]{article}
\usepackage{amsmath,amssymb,amsfonts,amsthm,tikz,caption,subcaption}

\begin{document}

\begin{figure}
\begin{subfigure}{.5\linewidth}
\centering
\begin{tikzpicture}[scale=1]

  \path (0,0) coordinate (P0) node[right=0.1cm] {P0};
  \fill (P0) circle (2pt);

\end{tikzpicture}
\caption{Hello P1} \label{fig:M1}
\end{subfigure}%
\begin{subfigure}{.5\linewidth}
\centering
\begin{tikzpicture}[scale=1]

  \path (0,0) coordinate (P0) node[right=0.1cm] {P0};
  \fill (P0) circle (2pt);

\end{tikzpicture}
\caption{Hello P2} \label{fig:M2}
\end{subfigure}
\end{figure}

\end{document}

enter image description here

I used .5\linewidth for each subfigure so each one will occupy half the available horizontal space and the two will appear side-by-side; of course, you can adjust those settings according to your needs. I also used \centering to center the objects, since the center environment adds (usually unnecessary) vertical spacing inside a float.

  • Thank you, Gonzalo. Do you mean textwidth or linewidth? (In the example you have linewidth but in your description you have textwidth.) Or are they the same? – boaten Apr 16 '14 at 19:16
  • @boaten in my example document they are the same, but they can differ depending in the context (for example, if twocolumn is used). I've updated my answer for consistency's sake. – Gonzalo Medina Apr 16 '14 at 19:18
1

The issue is not with using TiKz for your figures, it's how you're doing the subfigures.

I write a lot of papers where I want a series of side-by-side (or more complicated) subfloats in my figures. In addition, I'd like the figures to be centered vertically. I finally gave up and switched from subfloat or subfig to using the floatrow package.

Here's a minimal example of using floatrow to place two subfigures side-by-side.

\documentclass[]{article}       
\usepackage{graphicx, subfig}   
\usepackage{floatrow}   
\floatsetup[figure]{floatrowsep=qquad,   valign=c}      
\floatsetup[subfigure]{subfloatrowsep=qquad, heightadjust=object, valign=c, framefit = yes}

\usepackage{tikz}

\begin{document}

\begin{figure}[htbp]
\begin{center}
\ffigbox{
\begin{subfloatrow}[2] %change to 3 to have 3 side-by-side subfigs
\ffigbox[.4\textwidth]{\caption{A caption!}}{
\begin{tikzpicture}
\draw (0,0) rectangle (1,4);
\end{tikzpicture}
}
\ffigbox[.4\textwidth]{\caption{Another nice rectangle}}{
\begin{tikzpicture}
\draw (0,0) rectangle (6,2);
\end{tikzpicture}}
\end{subfloatrow}
}{
\caption{Here is a figure with two subfigures.}
\label{F:FirstEx}
}
\end{center}
\end{figure}
\end{document}  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.