# Multiple subfigures using the subfigure environment

When I compile the attached code,I find that: (a) The figure is not centred (b) The subfigures overlap (c) The captions are duplicated. I wasn't able to include the .tikz file as I am a new user. However,I believe any other figure saved in the fig.tikz format and included in the same directory as the attached code snippet along with the matlab2tikz mfile should give a good demonstration of the problem.

\documentclass{report}
\usepackage{subcaption}
\usepackage{tikz,pgfplots}
\usepackage{epsfig}
\begin{document}
\newlength\figureheight
\newlength\figurewidth
\begin{figure}
\centering
\begin{subfigure}{.2\linewidth}
\centering
\setlength\figureheight{4cm}
\setlength\figurewidth{4cm}
\input{fig.tikz} \caption{(a) Fig 1}
\end{subfigure}
\hspace{1cm}
\begin{subfigure}{.2\linewidth}
\centering
\setlength\figureheight{4cm}
\setlength\figurewidth{4cm}
\input{fig.tikz} \caption{(b) Fig 2}
\end{subfigure}\\
\vspace{1cm}
\begin{subfigure}{.2\linewidth}
\centering
\setlength\figureheight{4cm}
\setlength\figurewidth{4cm}
\input{fig.tikz} \caption{(c) Fig 3}
\end{subfigure}
\begin{subfigure}{.2\linewidth}
\centering
\setlength\figureheight{4cm}
\setlength\figurewidth{4cm}
\input{fig.tikz} \caption{(d) Fig 4}
\end{subfigure}
\end{figure}
\end{document}

-
.tikz files from matlab2tikz are just plain text files, no different from .tex files, simply open one in your texteditor and edit your question to include the code. –  Torbjørn T. Sep 16 '12 at 9:30
By "captions are duplicated", do you mean that the caption reads e.g. "(a) (a) Fig 1"? That is of course because you've written the subnumbering explicitly in the captions, but subcaption handles this for you, so there's no need to do that. (Also, there's no need to repeat the \setlengths for each figure, once right after the \newlengths will suffice. Unless you want to change the lengths, of course.) –  Torbjørn T. Sep 16 '12 at 9:41
You're reserving .2\linewidth for the width of the subfigures (about 69pt), but the subfigures are 4cm wide (about 114pt). The doubling of the labels is due to the fact that subcaption automatically provides them. –  egreg Sep 16 '12 at 9:42

You're doing contradictory requests: you're allowing .2\linewidth (about 69pt) for the subfigures, while you want them 4cm wide (about 114pt).

Look at the following example (I've used \test instead of your \input just for showing the results with the demo option to graphicx)

\documentclass{report}
\usepackage{subcaption}
\usepackage[demo]{graphicx}
\usepackage{tikz,pgfplots}

\newcommand{\test}{\includegraphics[width=\figurewidth,height=\figureheight]{x}}

\newlength\figureheight
\newlength\figurewidth

\begin{document}

\begin{figure}
\centering
\begin{subfigure}{.3\linewidth}
\centering
\setlength\figureheight{\linewidth}
\setlength\figurewidth{\linewidth}
\test \caption{Fig 1}
\end{subfigure}%
\hspace{1cm}%
\begin{subfigure}{.3\linewidth}
\centering
\setlength\figureheight{\linewidth}
\setlength\figurewidth{\linewidth}
\test \caption{Fig 2}
\end{subfigure}\\
\vspace{1cm}
\begin{subfigure}{4cm}
\centering
\setlength\figureheight{4cm}
\setlength\figurewidth{4cm}
\test \caption{Fig 3}
\end{subfigure}
\begin{subfigure}{4cm}
\centering
\setlength\figureheight{4cm}
\setlength\figurewidth{4cm}
\test \caption{Fig 4}
\end{subfigure}
\end{figure}
\end{document}


In the first row the squares fill all the space available (\linewidth refers to the current linewidth, inside a subcaption environment it's what you reserved to it) and they are separated by a 1 cm space (notice the % comments to mask off the end of lines). In the second row the squares are 4cm wide, separated by a normal interword space.

You don't need to specify (a) and so on in the captions, as they are provided automatically.

-