在图中并排插入图像

在图中并排插入图像

我试图在 LaTex 中并排插入两幅图像,但它们并不是并排的,而是相互叠加的。

我尝试了很多方法,但似乎都不起作用。代码如下:

\documentclass{article}

\usepackage{graphicx}
\usepackage{subcaption}

\graphicspath{{./images/}}


\title{Inserting Images Latex}
\author{Lorem Ipsum}
\date{February 2020}



\begin{document}

\maketitle

A demonstration of how to have two figures side by side.

\bigskip


\begin{figure}
    \centering

    \begin{minipage}{0.45\textwidth}
        \centering
        \includegraphics[width=0.9\textwidth]{universe} % first figure itself
        \caption{Universe}
    \end{minipage}\hfill

    \begin{minipage}{0.45\textwidth}
        \centering
        \includegraphics[width=0.9\textwidth]{figure} % second figure itself
        \caption{Bar Plot}
    \end{minipage}

\end{figure}




\begin{figure}[h]
    \includegraphics[width=.24\textwidth]{universe}\hfill
    \caption{Universe}
    \includegraphics[width=.24\textwidth]{figure}\hfill
    \caption{Bar Plot}
   %  \includegraphics[width=.24\textwidth]{example-image}\hfill
   %  \includegraphics[width=.24\textwidth]{example-image}
   %  \\[\smallskipamount]

\end{figure}



\begin{figure}[h]
    \begin{subfigure}[b]{0.4\textwidth}
        \includegraphics[width=\textwidth]{universe}
         \caption{Universe}
         \label{fig:1}
    \end{subfigure}


     \begin{subfigure}[b]{0.4\textwidth}
        \includegraphics[width=\textwidth]{figure}
         \caption{Bar Plot}
         \label{fig:2}
    \end{subfigure}
\end{figure}




\begin{figure}[h]
  \begin{minipage}[b]{0.4\textwidth}
    \includegraphics[width=\textwidth]{universe}
    \caption{Universe}
    \label{fig:1}
  \end{minipage}

  \begin{minipage}[b]{0.4\textwidth}
   \includegraphics[width=\textwidth]{figure}
    \caption{Bar Plot}
    \label{fig:2}
  \end{minipage}
\end{figure}


This is how you have multiple images in one figure.

\end{document}

发生什么问题了?

谢谢

答案1

正如评论已经指出的那样,在您的第一个示例中,换行符是由多个新行引起的(在 LaTeX 中,多个换行符会创建一个新段落)。只需注释掉小页面之间的行或完全删除该行即可轻松修复此问题:

\begin{figure}
    \centering
    \begin{minipage}{0.45\textwidth}
        \centering
        \includegraphics[width=0.9\textwidth]{universe} % first figure itself
        \caption{Universe}
    \end{minipage}\hfill
    % Remove or comment out this line
    \begin{minipage}{0.45\textwidth}
        \centering
        \includegraphics[width=0.9\textwidth]{figure} % second figure itself
        \caption{Bar Plot}
    \end{minipage}
\end{figure}

结果: 在此处输入图片描述 除了不使用小页面和子图的示例外,其余示例也是如此。

相关内容