[Tex/LaTex] Horizontal alignment of 2 figures in Latex

floats

I would like to plot two figures next to each other (a) (b).
This code however gives me
(a)
(b) vertically.

\begin{figure}[htp] 
\centering

\subfloat[data a]{%
\includegraphics[width=0.4\textwidth]{fig_a}%
\label{fig:a}%
}

\subfloat[data b]{%
\includegraphics[width=0.4\textwidth]{fig_b}%
\label{fig:b}%
}

\caption{all the data}
\end{figure}

How can I change that?

Best Answer

Some strategic %s at the right places help you to get what you want.

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{subfig}

\begin{document}
\begin{figure}[htp] 
    \centering
    \subfloat[data a]{%
        \includegraphics[width=0.4\textwidth]{fig_a}%
        \label{fig:a}%
        }%
    \hfill%
    \subfloat[data b]{%
        \includegraphics[width=0.4\textwidth]{fig_b}%
        \label{fig:b}%
        }%
    \caption{all the data}
\end{figure}

\end{document}

enter image description here