I was wondering if it was possible to make a big macro, for inserting figures? I wanted it to contain:

\begin{figure} [H]



\caption{#FIGURECAPTION \label{#LABELREF}}


Seeing it being so long, I wanted to make it easier to insert such a long "code". Should I make it as an environment in the preamble? Or as a new command?

I would be grateful for any kind of help I could get!

  • Welcome to TeX.SX!! You can highlight code in your post using back-ticks. For code-blocks indent them by four spaces or use the {} on the gui. It is better to post a full minimal working example that starts with a \documentclass command, has a minimal preamble and then \begin{document}...\end{document}. Unless the problem is a compilation error, the code should compile and be as small as possible to demonstrate your problem. This makes it much easier for people to help you --- and much ore likely that they will! – Andrew Feb 17 '16 at 20:39
  • You certainly can, but you lose in markup. – egreg Feb 17 '16 at 20:41

Have you tried:

  \begin{figure} [H]

You would use this as


Actually, even better would be:

  \begin{figure} [H]

The second variant defines an optional argument, which defaults to scale=0.8, for passing arguments to \includegraphics. For example, you could write things like \Figure[width=3cm]{filename}{caption}{label}.

Edit: optional short caption

Sigur asked in the comments for an optional short caption. This is actually slightly trickier in the sense that the following does not work:

  \begin{figure} [H]

because with no optional argument #1 is blank and the macro inserts \caption[]{...long caption...}, so the short caption is empty! To get around this you could instead make the default value of #1 equal to \relax, for example, and then in the macro use


Now the macro behaves as expected.

A better solution, however, is to use \NewDocumentCommand from the xparse package because this will allow you to have two optional arguments. Here is one way of doing it:

\NewDocumentCommand\Figure{o D<>{scale=0.8} m m m}{%
  % [optional short caption]<optional includegraphics options>{image}{caption}{label}

  \Figure{example-image-a}{Normal figure}{f:normal}

  \Figure<width=20mm>{example-image-a}{Width 20mm }{f:20mm}

  \Figure[Short caption]<width=20mm>{example-image-a}{With 20mm with short caption}{f:20mm}

  \Figure[Short caption]{example-image-a}{Short caption}{f:20mm}


An optional argument in between [...] will become the short-caption and an optional argument in between <...> will be given to \includegraphics, with the default being scale=0.8.

  • Well, what about another optional argument for short caption? – Sigur Feb 17 '16 at 20:52
  • 1
    @Sigur I have edited the solution to do this. – Andrew Feb 21 '16 at 0:20

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.