3

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

\begin{figure} [H]

\centering

\includegraphics[scale=0.8]{#FILENAME}

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

\end{figure}

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
6

Have you tried:

\newcommand\Figure[3]{%
  \begin{figure} [H]
    \centering
    \includegraphics[scale=0.8]{#1}
    \caption{#2}\label{#3}
  \end{figure}
}

You would use this as

\Figure{filename}{caption}{label}

Actually, even better would be:

\newcommand\Figure[4][scale=0.8]{%
  \begin{figure} [H]
    \centering
    \includegraphics[#1]{#2}
    \caption{#3}\label{#4}
  \end{figure}
}

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:

\newcommand\Figure[4][]{%
  \begin{figure} [H]
    \centering
    \includegraphics[scale=0.8]{#2}
    \caption[#1]{#3}\label{#4}
  \end{figure}
}

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

\ifx#1\relax\relax\caption{#3}\else\caption[#1]{#3}\fi

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:

\documentclass{article}
\usepackage{mwe}
\usepackage{xparse}
\usepackage{float}
\NewDocumentCommand\Figure{o D<>{scale=0.8} m m m}{%
  % [optional short caption]<optional includegraphics options>{image}{caption}{label}
  \begin{figure}[H]
    \centering
    \includegraphics[#2]{#3}
    \IfNoValueTF{#1}{\caption{#4}}{\caption[#1]{#4}}
    \label{#5}
  \end{figure}
}
\begin{document}

  \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}

\end{document}

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.

| improve this answer | |
  • 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.