[Tex/LaTex] centering figure, containing a \subfigure with customized \includegraphics

floatsgraphicshorizontal alignmentsubfloats

My Problem:
I would like to have all images in my Latex document beeing well sized in height and width. Precisly I want, that protrait and landscape images get a predefined size in the document. I got this by this style:

\RequirePackage{ifthen,graphicx}
\newlength{\widthW}
\newlength{\heightH}
\newcommand\includegraphicsJustified[1]{
    \settowidth{\widthW}{\includegraphics{#1}}
    \settoheight{\heightH}{\includegraphics{#1}}
    \ifthenelse{\lengthtest{\widthW > \heightH}}{
        \includegraphics[width=0.40\textwidth]{#1}\\
    }{
        \includegraphics[height=0.35\textheight]{#1}\\
    }
}

The second thing is, I want to use this in a \subfigure because I have a lot of images which have to be side by side.

The problem is, everything works great, but as soon as I try to centering this images I got:

! LaTeX Error: Something's wrong–perhaps a
missing \item.

Thats my code:

\begin{figure}
\centering
\subfigure[comment]{\includegraphicsJustified{images/img1.png}  }
\subfigure[comment]{\includegraphicsJustified{images/img2.png}  }
\caption{whole comment}
\end{figure}

Note again: without the \centering command it works.

I tried to add this to my style, to automaticly center all figure elements:

\makeatletter
\let\oldfigure\figure
\def\figure{\@ifnextchar[\figure@i \figure@ii}
\def\figure@i[#1]{\oldfigure[#1]\centering}
\def\figure@ii{\oldfigure\centering}
\makeatother

This works great, but not when I use the \subfigure:

! LaTeX Error: Something's wrong–perhaps a
missing \item.

Without the \subfigure I can use my \includegraphicsJustified{} command and it get centered.
Note also: When I use the subfigure with a normal \includegraphics{} it works!

Is there anybody out there who could explain this or much better can distribute a solution?

Best Answer

Try putting the subfigure inside a minipage, see below,

\begin{figure}
\centering
\begin{minipage}{0.5\textwidth}
\subfigure[comment]{\includegraphicsJustified{images/img1.png} }
\subfigure[comment]{\includegraphicsJustified{images/img2.png} }
\end{minipage}
\caption{whole comment}
\end{figure}

I firmly believe that you can carry out the rest of the fine tuning.

For further details, please see http://www.tex.ac.uk/cgi-bin/texfaq2html?label=errmissitem.

Hope this helps.

Related Question