[Tex/LaTex] Errors and alignment of subfigures

errorssubfloats

I want to put two pictures side by side in a two-column conference paper. The LaTeX code is as follows:

\documentclass[conference]{IEEEtran}
% Add the compsoc option for Computer Society conferences.
% *** GRAPHICS RELATED PACKAGES ***
%
\ifCLASSINFOpdf
   \usepackage[pdftex]{graphicx}
  % declare the path(s) where your graphic files are
  % \graphicspath{{../pdf/}{../jpeg/}}
  % and their extensions so you won't have to specify these with
  % every instance of \includegraphics
   \DeclareGraphicsExtensions{.pdf,.jpeg,.png,.eps}
\else
  % or other class option (dvipsone, dvipdf, if not using dvips). graphicx
  % will default to the driver specified in the system graphics.cfg if no
  % driver is specified.
   \usepackage[dvips]{graphicx}
  % declare the path(s) where your graphic files are
  % \graphicspath{{../eps/}}
  % and their extensions so you won't have to specify these with
  % every instance of \includegraphics
   \DeclareGraphicsExtensions{.eps}
\fi

% correct bad hyphenation here
\hyphenation{op-tical net-works semi-conduc-tor}
\usepackage{lscape}
\usepackage{subfigure}

\begin{document}
%
% paper title
% can use linebreaks \\ within to get better formatting as desired
\title{0000}

\author{\IEEEauthorblockN{00000}}

% make the title area
\maketitle


\begin{abstract}
\end{abstract}

\IEEEpeerreviewmaketitle

\section{0000}

\begin{figure}
\centering
\begin{subfig}{.5\textwidth}
  \centering
  \includegraphics[width=.4\linewidth]{pic/interest.png}
  \label{packet:interest}
\end{subfig}
\begin{subfig}{.5\textwidth}
  \centering
  \includegraphics[width=.4\linewidth]{pic/data.png}
  \label{packet:data}
\end{subfig}
\caption{CPI packet structure}
\label{packet}
\end{figure}

\begin{thebibliography}{1}

\end{thebibliography}

\end{document}

I used a .clsfile IEEEtran.cls‎:

www.ctan.org/tex-archive/macros/latex/contrib/IEEEtran/IEEEtran.cls‎

I got the following errors:

There's no line here to end.
Missing number, treated as zero

and the result is like this:

enter image description here

there are two unexpected .5 besides the subfigures and these 2 subfigures are aligned on the bottom, but I hope to align them
on the top.

How to modify the code?

Best Answer

You're mixing incompatible syntaxes. With the subfigure package you have a \subfig command and not a subfig environment.

However, calling \begin{subfig} executes \subfig which gets confused because it doesn't expect a length as argument.

The environment form with a stated length is available with the subcaption package, but the environment is called subfigure.

Here is a compiling code. Note that I loaded the package graphicx with the option demo just to show black blotches instead of the (unavailable) pictures. In the options for the pictures I added a height= statement in order to have them of different height; you don't need this statement, of course.

Recall also that the paper is typeset in two column format, so you have two choices:

  1. Use the figure environment and express the width as a fraction of \columnwidth

  2. Use the figure* environment and express the width as a fraction of \textwidth.

I used the first form; the lipsum package is just to fill the pages with nonsense text.

\documentclass[conference]{IEEEtran}
% Add the compsoc option for Computer Society conferences.
% *** GRAPHICS RELATED PACKAGES ***
%
\usepackage[demo]{graphicx} % demo is just for the example
\usepackage{subcaption}

% correct bad hyphenation here
\hyphenation{op-tical net-works semi-conduc-tor}
\usepackage{lipsum}

\begin{document}
%
% paper title
% can use linebreaks \\ within to get better formatting as desired
\title{0000}

\author{\IEEEauthorblockN{00000}}



% make the title area
\maketitle


\begin{abstract}
\lipsum[1]
\end{abstract}

\IEEEpeerreviewmaketitle

\section{0000}

\lipsum[1-3]

\begin{figure}
\centering
\begin{subfigure}[t]{.5\columnwidth}
  \centering
  \vspace{0pt}
  \includegraphics[width=.4\linewidth,height=3cm]{pic/interest.png}
\end{subfigure}% <--- This is important!!!!
\begin{subfigure}[t]{.5\columnwidth}
  \centering
  \vspace{0pt}
  \includegraphics[width=.4\linewidth,height=4cm]{pic/data.png}
\end{subfigure}
\caption{CPI packet structure}
\label{packet}
\end{figure}

\lipsum

\begin{thebibliography}{1}
\bibitem{a} x
\end{thebibliography}

\end{document}

enter image description here