10

I'd like to define some figure/graphic sizes in the preamble and then reference those in the \includegraphics options throughout the document (this way, I can change the size once and it will change throughout the entire document). Here's an example of what I'm trying to do (it gives me a "package keyval error"):

%%
\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}  
%%

Is \newcommand the right command for this task or should I be using something else?

cmhughes
  • 100,947

2 Answers2

8

It works if you use \expandafter for expanding your macro before \includegraphics is called:

\expandafter\includegraphics\expandafter[\grsmall]{example}
Stefan Kottwitz
  • 231,401
7

Another option is to define extra keys for the \includegraphics command, as demonstrated here:

includegraphics[page=\pageref... possible? how to set [page=] to a variable?

\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}  
cmhughes
  • 100,947