TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

I have setup a function called quickfigure which has the purpose of inserting a centered image with a one-line command:

\newcommand{\quickfigure}[2] {

This function is used as follows;

\quickfigure{img/testImage.png}{Test image}

I would like this function to accept optional arguments to define width and height of the image, as well as a label. The syntax is desired to be something like;

\quickfigure[width=0.6, label=img:test]{img/testImage.png}{Test image}

How would I go about setting this up? I need a push in the right direction. Thanks!

Edit: Thanks everyone for the ideas, this is my solution;



\newcommand{\quickfigure}[3][] {
share|improve this question

marked as duplicate by cfr, Peter Jansson, ChrisS, Jubobs, Martin Schröder Mar 3 '14 at 23:31

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Related/duplicate: How to create a command with key values? – Werner Mar 3 '14 at 21:43
@Werner This is a case where the optional argument seems to be passed directly to \includegraphics – egreg Mar 3 '14 at 22:25
I'm not sure you gain anything by hiding the document's structure into a macro. – egreg Mar 3 '14 at 22:25
@egreg: Just partially, since label doesn't exist for \includegraphics. It would have to be managed differently. And true, it was a mild approach at pushing towards a solution. – Werner Mar 3 '14 at 22:53
@Wilco: You could combine your \setkeys as \setkeys{quickfigure}{width=.6\linewidth,label=\@empty,#1}. Note that you are required to use a \makeatletter...\makeatother pair with your current definition. – Werner Mar 3 '14 at 23:07

Or you could always do something like this:



\NewDocumentCommand\quickfigure{O{width=0.6\linewidth} m o m}{%


\quickfigure{example-image-a}{Test image}

\quickfigure[width=20pt, height=100pt]{example-image-a}[img:test]{Test image}


Two images

share|improve this answer
You can also simply say \IfValueT{#3}{\label{#3}} or, equivalently, \IfNoValueF{#3}{\label{#3}} – egreg Mar 3 '14 at 22:27
@egreg Interesting. Thank you. Is that documented elsewhere? The xparse documentation I have only mentions \IfValueTF{}{}{} and \IfNoValueTF{}{}{}. – cfr Mar 3 '14 at 22:29
It is documented, albeit in a perhaps cryptic form; if you look closely, you'll see that TF in \IfNoValueTF (in the margin) are in italics and underlined in red. This is a shorthand for saying that either letter (and the corresponding argument) can be removed. The convention is described in the LaTeX3 interface documentation; it should probably be added also in the documentation of xparse. – egreg Mar 3 '14 at 22:37
@cfr Thank you for this suggestion! This is a very nice solution. Is it possible to have the optional label argument in the first parameter and extract it before passing it to \includegraphics? – Wilco Mar 3 '14 at 23:06
@Wilco Possible, yes. But I would use the link Werner provided in that case since there is no point in reinventing the wheel. That is, if you want to set it up to parse key-value pairs, you are best off using a tool designed for that. You could, of course, combine one of those solutions with the solution here. Just drop the second optional argument and run the first one through the tool of your choice. – cfr Mar 3 '14 at 23:32

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