1

The following code is producing an error:

\begin{figure}[htbp!]
\subfloat{
    \begin{minipage}[c][Structured Mesh]{0.5\textwidth}
        \centering
        \includegraphics[scale=0.4]{ch-SmallStrain/Graphics/Structured}
    \end{minipage}}
\subfloat{
    \begin{minipage}[c][Unstructured Mesh]{0.5\textwidth}
        \centering
        \includegraphics[scale=0.4]{ch-SmallStrain/Graphics/Unstructured}
    \end{minipage}}
\\
\subfloat{
    \begin{minipage}[c][Structured Mesh, zoomed to red box shown above]{0.5\textwidth}
        \centering
        \includegraphics[scale=0.2]{ch-SmallStrain/Graphics/StructuredCloseup}
    \end{minipage}}
\subfloat{
    \begin{minipage}[c][Unstructured Mesh, zoomed to red box shown above]{0.5\textwidth}
        \centering
        \includegraphics[scale=0.2]{ch-SmallStrain/Graphics/UnstructuredCloseup}
    \end{minipage}}
    \caption{Meshes for the Shearing example\label{fig:Meshes-for-the}}
\end{figure}

The error is:

! Missing number, treated as zero. S l.634 \end{minipage}}

which occurs at each \end{minipage}. This seems to me like the width arguments {0.5\textwidth} are not being recognized, and indeed the error goes away if I remove the optional arguments in the []. Is there a way to fix this error while keeping the optional arguments?

Edit:

I forgot to mention that I am using the subfig package and the second optional argument is supposed to be the caption of the subfigure.

  • 3
    what do you want the [Unstructured Mesh] argument to do? (minipage doesn't have an argument that takes text in that way). Without a hint on the intended result hard to suggest how to correct the code. – David Carlisle Nov 20 '13 at 19:47
  • 1
    I think you should read a bit about minipage and its parameters. – Thorsten Donig Nov 20 '13 at 19:55
  • Please edit your example to be a complete small document that shows the problem (a complete small document showing all packages used is always preferable to a fragment) – David Carlisle Nov 20 '13 at 20:30
5

I assume you intend the second argument in brackets following \begin{minipage} to act as the subfloat's caption. If that's your objective, you should place the argument immediately after \subfloat rather than after \begin{minipage}.

Note that the minipage environment can take up to three optional arguments -- an "outer" vertical alignment indicator, which can be t for top, c for center, and b for bottom; a height variable (a TeX "length" entity); and an "inner" position indicator -- and two mandatory arguments: a length variable to set the intended width of the environment and the actual contents of the minipage, both to be placed in curly braces.

Another observation: Unless there's a very good reason for making the subfloat graphs have different widths, you may just want to give all four the same width. This may be achieved by setting the option width=\linewidth when executing \includegraphics.

enter image description here

\documentclass{article}
\usepackage{subfig}
\usepackage[demo]{graphicx} %leave off the 'demo' option in your real document
\begin{document}
\begin{figure}
\subfloat[Structured Mesh]{%
  \begin{minipage}{0.48\textwidth}
    \includegraphics[width=\linewidth]{ch-SmallStrain/Graphics/Structured}
  \end{minipage}}
\hspace{\fill} % induce horizontal separation
\subfloat[Unstructured Mesh]{%
  \begin{minipage}[c]{0.48\textwidth}
    \includegraphics[width=\linewidth]{ch-SmallStrain/Graphics/Unstructured}
  \end{minipage}}

\subfloat[Structured Mesh, zoomed to red box shown above]{%
  \begin{minipage}{0.48\textwidth}  
    \includegraphics[width=\linewidth]{ch-SmallStrain/Graphics/StructuredCloseup}
  \end{minipage}}
\hspace{\fill} % induce horizontal separation
\subfloat[Unstructured Mesh, zoomed to red box shown above]{%
  \begin{minipage}{0.48\textwidth}
    \includegraphics[width=\linewidth]{ch-SmallStrain/Graphics/UnstructuredCloseup}
  \end{minipage}}
\caption{Meshes for the Shearing example\label{fig:Meshes-for-the}}
\end{figure}
\end{document}
| improve this answer | |
  • good answer except minipage does take optional argument in that position but it is the height which is why you get a missing number primitive TeX error if you put text there as the argument has to be a length. – David Carlisle Nov 20 '13 at 20:46
  • @DavidCarlisle - Thanks, I'll fix it right away. – Mico Nov 20 '13 at 20:56
  • "one optional argument" - it takes three: [outer pos][height][inner pos] the OP was putting the caption in the [height] argument. – David Carlisle Nov 20 '13 at 21:03
  • @DavidCarlisle - Arrgh, thanks! (I must be particularly distracted tonight!!) Let me fix this. – Mico Nov 20 '13 at 21:10
  • 1
    @Nicolas - I guess we don't know what the vertical dimensions of the OP's real image files are. If their heights aren't the same, then using minipage environments provides extra options for achieving a satisfactory positioning of the various subfloat elements. – Mico Nov 20 '13 at 22:51
1

As previously suggested, if you intend to use the minipage environment to set the captions of the sufigures, the subfloat itself can do it and you may simply remove all the minipages in the figure.

\documentclass{article}
\usepackage{subfig}
\usepackage[demo]{graphicx} %leave off the 'demo' option in your real document

\begin{document}
    \begin{figure}
    \subfloat[Structured Mesh]{%
        \includegraphics[width=0.48\linewidth]{ch-SmallStrain/Graphics/Structured}
    }
    \hfill % induce horizontal separation
    \subfloat[Unstructured Mesh]{%
        \includegraphics[width=0.48\linewidth]{ch-SmallStrain/Graphics/Unstructured}
    }

    \subfloat[Structured Mesh, zoomed to red box shown above]{%  
        \includegraphics[width=0.48\linewidth]{ch-SmallStrain/Graphics/StructuredCloseup}
    }
    \hfill % induce horizontal separation
    \subfloat[Unstructured Mesh, zoomed to red box shown above]{%
        \includegraphics[width=0.48\linewidth]{ch-SmallStrain/Graphics/UnstructuredCloseup}
    }
    \caption{Meshes for the Shearing example\label{fig:Meshes-for-the}}
    \end{figure}
\end{document}

You can adjust the width of each figure within the includegraphics command.

output

| improve this answer | |

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.