Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

up vote 5 down vote accepted

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

\expandafter\includegraphics\expandafter[\grsmall]{example}
share|improve this answer
add comment

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}  
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.