如何在单个 \newcommand 图形中包含两个 \includegraphics?

如何在单个 \newcommand 图形中包含两个 \includegraphics?

我有一个简短的命令,可以在我的文档中包含带有标题的图像,如下所示:

\newcommand{\image}[4][1.0]{\begin{figure}[tp]%\image[scale]{path}{label}{caption}
\centering
\includegraphics[width=#1\textwidth]{Figures/#2}
\decoRule
\caption{#4}
\label{fig:#3}
\end{figure}}

这很好用。但是,如果我尝试添加第二个新命令以将两个图像并排放置,则文件无法编译。我尝试添加的命令是:

\newcommand{\images2}[4]{\begin{figure}[tp]%\image[scale]{path1}{path2}{label}{caption}
\centering
\includegraphics[width=0.49\textwidth]{Figures/#1}
\includegraphics[width=0.49\textwidth]{Figures/#2}
\decoRule
\caption{#4}
\label{fig:#3}
\end{figure}}

日志显示

! LaTeX Error: Missing \begin{document}.
! LaTeX Error: Missing \begin{document}.
! Undefined control sequence.
! Illegal parameter number in definition of \@tempb.
! Illegal parameter number in definition of \reserved@a.
! Illegal parameter number in definition of \reserved@a.
! Illegal parameter number in definition of \filename@base.
! Illegal parameter number in definition of  .
! LaTeX Error: File `Figures/##1' not found.
! Illegal parameter number in definition of \@tempb.
! Illegal parameter number in definition of \reserved@a.
! Illegal parameter number in definition of \reserved@a.
! Illegal parameter number in definition of \filename@base.
! Illegal parameter number in definition of  .
! LaTeX Error: File `Figures/##2' not found.
! Illegal parameter number in definition of \reserved@a.
! Undefined control sequence.
! Undefined control sequence.
! Illegal parameter number in definition of \caption@tempa.
! Illegal parameter number in definition of \caption@tempa.
! Illegal parameter number in definition of \caption@tempa.
! You can't use `macro parameter character #' in restricted horizontal mode.
! Undefined control sequence.
! Illegal parameter number in definition of \caption@tempa.
! Illegal parameter number in definition of \caption@tempa.
! Illegal parameter number in definition of \caption@tempa.
! You can't use `macro parameter character #' in horizontal mode.
! Illegal parameter number in definition of \reserved@a.

添加新命令时,即使未使用该命令,构建也会立即失败。手动将新命令的内容放入文本中时,构建不会失败。


最后,我认为我不能用方程式来定义图像的宽度。有没有办法让两幅图像的大小动态变化,就像

\newcommand{\images2}[5][0.5]{\begin{figure}[tp]%\image[scale]{path1}{path2}{label}{caption}
\centering
\includegraphics[width=#1\textwidth]{Figures/#2}
\includegraphics[width=(1-#1)\textwidth]{Figures/#3}
...

答案1

你不能在命令名称中使用数字,\imagetwo或者\imageb其他不是\image2

另外,图像之间不能有单词间距,或者你需要缩小图像以留出空间

\newcommand{\twoimages}[5][0.5]{\begin{figure}[tp]%\image[scale]{path1}{path2}{label}{caption}
\centering
\includegraphics[width=#1\textwidth]{Figures/#2}%
\includegraphics[width=\dimexpr \textwidth-#1\textwidth\relax]{Figures/#3}
...

相关内容