使用 \includegraphics 选项中的 \newcommand 来替换图形尺寸

使用 \includegraphics 选项中的 \newcommand 来替换图形尺寸

我想在序言中定义一些图形/图片大小,然后在\includegraphics整个文档的选项中引用这些大小(这样,我可以更改一次大小,它将在整个文档中更改)。下面是我正在尝试执行的操作的示例(它给了我一个“包键值错误”):

%%
\documentclass[11pt]{article}
\usepackage{geometry}
\usepackage[demo]{graphicx}                

%%GRAPHIC/FIGURE SIZES%%
        %FULL PAGE GRAPHIC
        \newcommand{\grfull}{width=6.25in,height=9.25in}
        %LARGE GRAPHIC
        \newcommand{\grlarge}{width=5.25in,height=6.5in}
        %NORMAL GRAPHIC
        \newcommand{\grnormal}{width=4.25in,height=5.25in}
        %SMALL GRAPHIC (FOR WRAPFIG)
        \newcommand{\grsmall}{width=3.5in,height=4.25in}
        %TINY GRAPHIC (FOR SUBFIG)
        \newcommand{\grtiny}{width=3.2in,height=3.8in}


\begin{document}

Here's the working figure:

\begin{figure}[htbp]
   \centering
   \includegraphics[width=3in,height=2in]{example} 
   \caption{example caption 1}
   \label{fig:example1}
\end{figure}


The text substitution works here for \grtiny and for \grsmall .

Here's the non-working figure:

\begin{figure}[htbp]
   \centering
   \includegraphics[\grsmall]{example} 
   \caption{example caption 1}
   \label{fig:example1}
\end{figure}

\end{document}  
%%

这是\newcommand完成这项任务的正确命令吗或者我应该使用其他命令?

答案1

如果您在调用\expandafter之前使用扩展宏,它会起作用\includegraphics

\expandafter\includegraphics\expandafter[\grsmall]{example}

答案2

另一个选择是为命令定义额外的键\includegraphics,如下所示:

includegraphics[page=\pageref... 可能吗?如何将 [page=] 设置为变量?

\documentclass[11pt]{article}
\usepackage{geometry}
\usepackage[demo]{graphicx}

%%GRAPHIC/FIGURE SIZES%%
\makeatletter
\define@key{Gin}{grlarge}[true]{%
    \edef\@tempa{{Gin}{width=4.25in,height=6.5in}}%
    \expandafter\setkeys\@tempa
}
\define@key{Gin}{grtiny}[true]{%
    \edef\@tempa{{Gin}{width=3.2in,height=3.8in}}%
    \expandafter\setkeys\@tempa
}
\makeatother

\begin{document}

Here's the working figure:

\begin{figure}[htbp]
   \centering
   \includegraphics[grtiny]{example} 
   \caption{example caption 1}
   \label{fig:example1}
\end{figure}

\begin{figure}[htbp]
   \centering
   \includegraphics[grlarge]{example} 
   \caption{example caption 1}
   \label{fig:example2}
\end{figure}

\end{document}  

相关内容