2

I'm making a command called \seal that uses the \includegraphics function. The command \seal has some key=value options of its own (color, beamer, trimz) and it must accept all options of \includegraphics so that it directly passes them to it. We may use \seal for a picture called theimage as follows:

\seal[width=2cm, color=red]{theimage}

which calls:

\includegraphics[width=2cm]{theimage}

Although I want to use all options in \includegraphics, the following code does it well for the height and width options.

% Declaring options for "seal"
\pgfkeys{
/seal/.is family, /seal,
% "seal" options
color/.estore in = \sealColor,
beamer/.estore in = \sealBeamer,
trimz/.estore in = \sealTrimz,
% "\includegraphics" options
height/.estore in = \sealHeight,
width/.estore in = \sealWidth,
}

% Declaring variables for the left side of "key=value"
\newcommand{\sealHeightL}{height}
\newcommand{\sealWidthL}{width}

% Declaring a command for storing the image
\newcommand{\theimage}{}

% Defining the \seal command
\newcommand{\seal}[2][]{%
    \pgfkeys{/seal, #1}%
    %
    % Makes the left side of "key=value" empty
    % if there's no value
    \ifthenelse{\equal{\sealHeight}{}}
        {\renewcommand{\sealHeightL}{}}
        {\renewcommand{\sealHeightL}{height}}%
    \ifthenelse{\equal{\sealWidth}{}}
        {\renewcommand{\sealWidthL}{}}
        {\renewcommand{\sealWidthL}{width}}%
    %
    % The \includegraphics command
    \renewcommand{\theimage}{\includegraphics[%
        \sealHeightL=\sealHeight,%
        \sealWidthL=\sealWidth,%
    ]{#2}%
    %
    % The other command (pay no attention to this)
    \othercommand{\sealColor}{\sealBeamer}{\sealTrimz}{\theimage}
}

The basic idea of this code is to make \seal[width=2cm, color=red]{theimage} call \includegraphics[width=2cm, =]{theimage}, and \seal[color=red]{theimage} call \includegraphics[=, =]{theimage}. This works because neither \includegraphics[width=2cm, =]{theimage} nor \includegraphics[=, =]{theimage} produce error messages. The last one is equivalent to calling \includegraphics[]{theimage}.

Now, although this code does the work very well, it returns two error messages when calling \seal[height=2cm]{theimage}:

Undefined control sequence. \seal[height=2cm]{theimage}
Package keyval Error: undefined. \seal[height=2cm]{theimage}

When calling \seal{theimage}, it returns the following messages repeated twice.

Undefined control sequence. \seal{theimage}
Package keyval Error: undefined. \seal{theimage}

I suppose this happens for the same reason that \includegraphics[\empty = \empty,]{theimage}, where \newcommand{\empty}{}, returns the following error message:

Package keyval Error: undefined. \includegraphics[\empty = \empty]{theimage}

Using an \ifthenelse command inside the (square) brackets of \includegraphics doesn't seem to work. It also seems to be mandatory that = be directly written inside the brackets, and not inside a command that is inside the brackets. I can think of the following strategies for addressing this problem:

  1. Devising a completely different approach for passing options from \seal to \includegraphics. Currently, I don't have a clue as to what that approach might be.
  2. Letting LaTeX know that this is not an error. Since I am new in LaTeX, I don't know how to do this. I also feel this cannot be a proper solution: if there is an error message something must be not in place. It could have actual consequences for the other options. However, I am open to be persuaded otherwise on this.
  3. Defining \seal so that it uses two brackets for key=value options: one for its own options and the other for the \includegraphics options. In this way, it will pass the whole string of one of those brackets to \includegraphics. In this case, we would call \seal[width=2cm][color=red]{theimage} instead of \seal[width=2cm, color=red]{theimage}. I think this might be the most straightforward solution. The first version of \seal was a command of the form \seal[includegraphicsOptions]{colorValue}{beamerValue}{trimzValue}{theimage}, which passed the whole includegraphicsOptions string to \includegraphics returning no error whatsoever. Nevertheless, I don't know how to define \seal so that it has two brackets and, although I more than welcome a solution along these lines, I consider it a bit sub-optimal to my taste. (I'm more likely to be persuaded otherwise on this one.)

But ultimately, I am here because I do not know how to properly address this. I hope someone can suggest a solution.

Note: The only packages I am using/requiring for this command are graphicx, pgfkeys and ifthenelse. (Also tikz, which is not important for this question, and optionally the class beamer.)

PS: I'm sorry I couldn't make this question shorter.

4

You can use the .unknown feature:

\documentclass{article}
\usepackage{pgfkeys}

% Declaring options for "seal"
\pgfkeys{
  /seal/.is family, /seal,
  % "seal" options
  color/.estore in = \sealColor,
  beamer/.estore in = \sealBeamer,
  trimz/.estore in = \sealTrimz,
  .unknown/.code={%
    \edef\includegraphicssealoptions{\includegraphicssealoptions,\pgfkeyscurrentname=#1}%
  },
}

% Defining the \seal command
\newcommand{\seal}[2][]{%
  \def\includegraphicssealoptions{}%
  \pgfkeys{/seal,color=black,beamer=,trimz=,#1}%
  \edef\thesealimage{\noexpand\includegraphics[\includegraphicssealoptions]{#2}}%
  % The other command (pay no attention to this)
  \othercommand{\sealColor}{\sealBeamer}{\sealTrimz}{\thesealimage}
}

\newcommand{\othercommand}[4]{%
  \typeout{%
    sealColor=#1^^J%
    sealBeamer=#2^^J%
    sealTrimz=#3^^J%
    imagecommand=\unexpanded\expandafter{#4}%
  }%
}

\begin{document}

\seal[width=2cm, color=red]{theimage}

\seal[trimz=b,width=2cm,height=1cm,beamer=x,color=red]{theimage}

\end{document}

For inserting the image you issue \thesealimage, I guess in \othercommand.

With the definition in the example, you get

sealColor=red
sealBeamer=
sealTrimz=
imagecommand=\includegraphics [,width=2cm]{theimage}

and

sealColor=red
sealBeamer=x
sealTrimz=b
imagecommand=\includegraphics [,width=2cm,height=1cm]{theimage}
| improve this answer | |
  • Thanks, I'm studying your answer. – lfba Aug 26 '19 at 10:48
  • I tested your answer and it seems to be the right answer. Really thanks. I think I'll become a fan of the .unknown feature. – lfba Aug 26 '19 at 10:59

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.