6

I'm trying to make a class file to help me draft bylaws for clubs, but I'm running into a problem with \includegraphics. I want to be able to define a club logo using a \logo command (see class file), with the option of passing options to \includegraphics, but it doesn't want to work.

Class file:

\ProvidesClass{bylaws_min}[2012/10/27 version 0.01 "alpha" Bylaws]
\NeedsTeXFormat{LaTeX2e}

\RequirePackage[margin=1in]{geometry}% Sets page margins
\RequirePackage{graphicx}% Allows adding images to documents

\newcommand{\logo}[2][\@empty]{\def\@logoopts{#1}\def\@logoimage{#2}}

\renewcommand{\maketitle}{%
\begin{center}
\ifx\@empty\@logoopts\includegraphics{\@logoimage}\else\includegraphics[\@logoopts]{\@logoimage}\fi
\end{center}%
}

Document file:

\documentclass{bylaws_min}

\logo[scale=2]{imagefile.jpg}

\begin{document}

\maketitle

\end{document}

I've tried both \logo[scale=2]{imagefile.jpg} (or some other option other than scale=2), or just \logo{imagefile.jpg}, and I get variations on this error:

Package keyval Error: scale=2 undefined.
See the keyval package documentation for explanation.
Type H <return> for immediate help.
 ...

1.7 \maketitle

I've played around with this for a while, and it seems that there are two problems. First, \ifx never sees \@logoopts as \@empty (in other words, the comparison always fails, and the \else statement always happens). Second, if I explicitly create an image like \includegrahpics[scale=2]{imagefile.jpg} in \maketitle, everything works, but if I try to pass it the options with \@logoopts it fails. But passing it the image file name with \@logoimage always works.

1
  • Actually the error message is a bit misleading. As Heiko explains, the contents of the optional argument to \includegraphics is not expanded when the command is executed; however the error messages do expand things, so you find scale=2 undefined. If you mistype an option, for example \includegraphics[scaled=2]{file}, you'd get scaled undefined and you can see that the part from = onward would not appear. – egreg Oct 29 '12 at 13:42
3

There is no need for \@empty as default argument. It only would costs extra work.

\newcommand{\logo}[2][]{\def\@logoopts{#1}\def\@logoimage{#2}}

The option parser needs explicit key value settings:

\expandafter\includegraphics\expandafter[\@logoopts]{\@logoimage}%

If I remember correctly, then \@logoimage is expanded once by graphics. Otherwise it also would have to be expanded for parsing the file name.

\expandafter\expandafter\expandafter\includegraphics
\expandafter\expandafter\expandafter[\expandafter\@logoopts\expandafter]%
\expandafter{\@logoimage}%
2

Since the options and the image file must be expandable, we can avoid a chain of \expandafter.

\newcommand{\logo}[2][]{\def\@logoopts{#1}\def\@logoimage{#2}}
% Default setting, in case Ben User calls \maketitle without first calling \logo:
\logo{nofile}
\DeclareGraphicsExtensions{jpg}
\renewcommand{\maketitle}{%
  \begin{center}%
    \begingroup
    \edef\x{\endgroup\noexpand\includegraphics[\@logoopts]{\@logoimage}}\x
  \end{center}%
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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