3

I am fairly inexperienced in Overleaf and I want to place two figures side by side, I tried the subcaption package. Here is the part of the latex code I used for this

\usepackage{caption} 
\usepackage{subcaption}

\begin{figure}  
    \begin{subfigure}[b]{0.4\textwidth}
        \includegraphics[width=0.4\textwidth]{image1.eps}
        \caption{Picture 1}
        \label{fig:1}  
    \end{subfigure}   

   \begin{subfigure}[b]{0.4\textwidth}
        \includegraphics[width=0.4\textwidth]{image2.eps}
        \caption{Picture 2}
        \label{fig:2}   
   \end{subfigure} 
\end{figure}

But I get the output in which the images are placed one below the other. AlsoI get some stray text [b]0.4 from the code and I would like them to be sub labelled 3.6 a) & 3.6 b) instead of Figure 3.6, Figure 3.7 and have a main caption.

Thanks for the help!

Output

Edit: I have now added the MWE.

\RequirePackage[l2tabu]{nag}
\documentclass{memoir}
\newsubfloat{figure}
\newsubfloat{table}
\usepackage{subfigure}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{showframe}
\usepackage{graphicx} 
\begin{document}
\begin{figure}
  \begin{subfigure}[b]{0.45\textwidth}
    \includegraphics[width=\textwidth]{example-image-a}
    \subcaption{Picture 1}
    \label{fig:1}
  \end{subfigure}
  \hfill
  \begin{subfigure}[b]{0.45\textwidth}
    \includegraphics[width=\textwidth]{example-image-b}
    \subcaption{Picture 2}
    \label{fig:2}
  \end{subfigure}
  \caption{Main Figure}
  \label{Main figure}
\end{figure}
\end{document}
1
  • Welcome to TeX.SE. To stop getting the line break between the subfigure environments, don't leave blanks lines between them. In addition, you should change both instances of [width=0.4\textwidth] to [width=1\textwidth], and you should place an \hfill directive immediately after the end of the first subfigure environment.
    – Mico
    Jul 3, 2019 at 1:43

2 Answers 2

6

The cause that you obtain one image below other is empty line between sub figures as already mentioned @Mensch in his answer, however you need to decide first, which package you will use for subfigures. memoir has own mechanism, subfigure is obsolete and not compatible with subcaption. I suggest you to stick with latter one:

%\RequirePackage[l2tabu]{nag}
\documentclass{memoir}
%\newsubfloat{figure}
%\newsubfloat{table}
%\usepackage{subfigure}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{graphicx}

%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

\begin{document}
\begin{figure}
  \begin{subfigure}[b]{0.45\textwidth}
    \includegraphics[width=\textwidth]{example-image-a}
    \caption{Picture 1}
    \label{fig:1}
  \end{subfigure}
  \hfill
  \begin{subfigure}[b]{0.45\textwidth}
    \includegraphics[width=\textwidth]{example-image-b}
    \caption{Picture 2}
    \label{fig:2}
  \end{subfigure}
  \caption{Main Figure}
  \label{Main figure}
\end{figure}
\end{document}

which gives:

enter image description here

0
7

Because you gave us no minimal working example (MWE) (please add it in you next question!) I have a little bit to guess.

Please see that I used package showframe to visualize typing area and margins. Please see I used image example-image-a and -b from package mwe (must be installed but not called) ...

Now have a look to a part of the corrected code:

  \begin{subfigure}[b]{0.45\textwidth} % <==============================
    \includegraphics[width=\textwidth]{example-image-a} % <=============
    \caption{Picture 1}
    \label{fig:1}  
  \end{subfigure}

I used 0.45\textwidth to get a smaller white space between the two images. This definition defined a new textwidth for the following

\includegraphics[width=\textwidth]{example-image-a}

To make sure the image uses the new complete available textwith use option [width=\textwidth] as shown above.

Command \hfill adds a white space between the two images as available to fill the line. Note that you have to delete all blank lines between the two subfigure environments! (Blank line = new paragraph in LaTeX)

Please see the following complete mwe:

\documentclass{article}

\usepackage{showframe} % <==============================================
\usepackage{graphicx} % <===============================================
\usepackage{caption} 
\usepackage{subcaption}


\begin{document}

\begin{figure}  
  \begin{subfigure}[b]{0.45\textwidth} % <==============================
    \includegraphics[width=\textwidth]{example-image-a} % <=============
    \caption{Picture 1}
    \label{fig:1}  
  \end{subfigure}
  \hfill % <============================================================
  \begin{subfigure}[b]{0.45\textwidth} % <==============================
    \includegraphics[width=\textwidth]{example-image-b} % <=============
    \caption{Picture 2}
    \label{fig:2}   
   \end{subfigure} 
\end{figure}
\end{document}

and the result:

resulting pdf

3
  • Thanks for the answers, I tried the options suggested in the answers but the problem still seems to persist. I have now added an MWE. Jul 3, 2019 at 2:47
  • @ChandramouliSanthanam Well, the culprit is \newsubfloat{figure}, please comment it out.
    – Mensch
    Jul 3, 2019 at 3:07
  • Commented out \newsubfloat{figure} and \usepackage{subfigure} and it works now :) Jul 3, 2019 at 4:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .