\documentclass[a4paper,10pt,twoside]{book}
\usepackage[demo]{graphicx}
\begin{document}
\newcommand{\scalefactor}{scale = 0.25}
\begin{figure}[!ht]
\centering
%Next line does not work
\includegraphics[\scalefactor]{image}
% The following line works
%\includegraphics[scale = 0.25]{image}
\end{figure}
\end{document}
输出:
! Package keyval Error: scale = 0.25 undefined.
我需要按相同的比例缩放多个图像,因此需要该命令。
- 如何解决这个问题,更重要的是
- 为什么这只针对
\includegraphics
答案1
尝试一下这个方法:
\newcommand{\scalefactor}{.25}
\includegraphics[scale=\scalefactor]{image}
答案2
如果你使用
\setkeys{Gin}{scale=0.25}
该键值将默认有效,直到您更改它,因此这里不需要使用 LaTeX 命令宏语法。
如果你的问题是你想在其他地方设置值,你可以去
\newcommand{\scalefactor}{0.25}
...
\includegraphics[scale = \scalefactor]{image}
答案3
定义私有\includegraphics
允许您将宏作为可选参数调用:
\protected\def\newincludegraphics{\@testopt\new@includegraphics{}}
\def\new@includegraphics[#1]{%
\begingroup
\@protected@edef\x{\endgroup
\noexpand\includegraphics
\if\relax\detokenize{#1}\relax\else[#1]\fi
}\x
}
\newincludegraphics[\scalefactor]{image}