1

I want to arrage two figures in "side-by-side" (left-right). But the out put I got is like this (top-bottom): enter image description here

My code is here.

\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}

\begin{document}

This is sample latex code.

\begin{figure}
\centering
\begin{subfigure}{0.4\textwidth}
  \includegraphics[width=0.4\linewidth]{linux.jpeg}
  \caption{A subfigure}\label{fig1}
\end{subfigure}

\begin{subfigure}{0.4\textwidth}
  \includegraphics[width=0.4\linewidth]{latex.jpeg}
  \caption{A subfigure}\label{fig2}
\end{subfigure}

\end{figure}

\end{document}

Obviously, there is a large room to arrange the two figures left and right positions respectively. But latex does this space wasting arrangement. How can I fix it?

Note that I'm using TexLive 2018 installed on Mac.

  • 2
    Welcome to TeX.SE! Does removing the empty line between the subfigures help? I mean something like ...\end{subfigure} \quad \begin{subfigure}{0.4\textwidth}.... An empty line is interpreted as the start as a new paragraph, and starts a new line. – user121799 Jan 4 at 1:36
  • @marmot Thanks!! I removed the empty line and the problem is solved. – ywat Jan 4 at 1:39
3

I'll be happy to remove this, which spells out my above comment in more detail.

\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}

\begin{document}

This is sample latex code.

\begin{figure}
\centering
\begin{subfigure}{0.4\textwidth}
  \includegraphics[width=0.4\linewidth]{example-image-a}
  \caption{A subfigure}\label{fig1}
\end{subfigure}
\quad
\begin{subfigure}{0.4\textwidth}
  \includegraphics[width=0.4\linewidth]{example-image-b}
  \caption{A subfigure}\label{fig2}
\end{subfigure}

\end{figure}

\end{document}

enter image description here

With inputs by Sigur (\centering) and Martin Scharrer (\hfill) and the information that 0.4\linewidth makes the figure 0.4^2=0.16 of the \textwidth wide, here is a slightly more advanced proposal:

\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}

\begin{document}

This is sample latex code.

\begin{figure}
\hfill
\begin{subfigure}{0.4\textwidth}
\centering
  \includegraphics[width=0.9\linewidth]{example-image-a}
  \caption{A subfigure}\label{fig1}
\end{subfigure}
\hfill
\begin{subfigure}{0.4\textwidth}
\centering
  \includegraphics[width=0.9\linewidth]{example-image-b}
  \caption{A subfigure}\label{fig2}
\end{subfigure}
\hfill
\end{figure}
\end{document}

enter image description here

  • Maybe \centering in the sub? – Sigur Jan 4 at 1:40
  • @Sigur I was about to delete this, must be a duplicate. In case you think it should stay, \centering is an excellent suggestion, thanks! – user121799 Jan 4 at 1:41
  • In addition to the \centerings inside the subfigure environments, changing \quad to \hfill might be a good idea. This will cause equal distances between the subfigures and the margins. – Martin Scharrer Jan 4 at 8:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.