Minipage not finding width argument

4,654

Solution 1

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}

Solution 2

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

Share:
4,654

Related videos on Youtube

Colin
Author by

Colin

Updated on August 01, 2022

Comments

  • Colin
    Colin over 1 year

    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.

    • David Carlisle
      David Carlisle almost 10 years
      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.
    • Thorsten Donig
      Thorsten Donig almost 10 years
      I think you should read a bit about minipage and its parameters.
    • David Carlisle
      David Carlisle almost 10 years
      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
    David Carlisle almost 10 years
    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.
  • Mico
    Mico almost 10 years
    @DavidCarlisle - Thanks, I'll fix it right away.
  • David Carlisle
    David Carlisle almost 10 years
    "one optional argument" - it takes three: [outer pos][height][inner pos] the OP was putting the caption in the [height] argument.
  • Mico
    Mico almost 10 years
    @DavidCarlisle - Arrgh, thanks! (I must be particularly distracted tonight!!) Let me fix this.
  • Nicolas
    Nicolas almost 10 years
    @Mico - why is the minipage environment required ? You can make the same figure only using the subfloat only...
  • Mico
    Mico almost 10 years
    @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.