0

I have been trying to implement a macro for my team to be able to include images with a macro. With this macro:

  • Size of the image
  • Image path selection
  • Caption of the image

can be defined by the end user. Macro has one default (hardcoded) argument (first argument) which is being used to defining the size of the image. In a default way, size of image fixed to the 0.75\textwidth, but user can overwrite it by using the [] brackets as most of us knows.

\newcommand{\addImageSeventyFive}[3][0.75]{
    \begin{figure}[H]
        \centering
        \includegraphics[width=#1\textwidth]{#2}
        \caption{#3 (size of #1)}\label{fig:1}
    \end{figure}
}

For now everything was going fine.. As you can see, I need to define three different arguments for this macro.

The problem I have been facing is, even if the first argument should be the size of image (hardcoded value: 0.75) it is mapping with the path of the image. And the middle curly brackets are mapping with the caption. Thus, the last curly brackets are left empty which should be originally maps with caption.

\addImageSeventyFive{}{}{}

I do not why this argument shift happens but below given example works perfectly fine, which should not, right?

\addImageSeventyFive{example-image-a}{Caption\ Test}{}

In addition, overwrite function is also works perfectly fine, which should not, right?

\addImageSeventyFive[0.40]{example-image-a}{Caption\ Test}{}

So, why the last curly brackets are appearing empty even it should not. I hope my question is clear.


Used packages:

  • \usepackage[utf8]{inputenc}
  • \usepackage{babel}
  • \usepackage[a4paper]{geometry}
  • \usepackage{float}
  • \usepackage{graphicx}

Used editor:

  • vscode with LaTeX Workshop extension.
6
  • 1
    you have two mandatory arguments (#2 and #3) and one optional (#1). Sep 7, 2021 at 11:28
  • Ahh, actually it is written intentionally. Just to get an caption like this: "Resized Example Image of C (size of 0.13)" to show the scale factor for the test cases since its' default value fixed to 0.75. Sep 7, 2021 at 11:42
  • 1
    oh in that case everything is working as expected and I do not see what your question is? (the {} that appear at the end of some of your examples are just an empty group in following text and are not used by the command at all) Sep 7, 2021 at 11:51
  • I think you are right. Because whenever I fill the last curly brackets with a random text, it is printed into document as text so command is not processing it at all.. But, I do not why the automatic syntax completer of the vscode+latex workshop is showing last empty curly brackets even they are not used.. Rather than this issue, commands are working fine. Do I need to consider anything for the future of the document, or can I just ignore it? Sep 7, 2021 at 11:57
  • 2
    I can not tell you to ignore anything as I can not guess what your issue is. You have defined a command that has two {} arguments so anything in a following 3rd {} group is just following text unrelated to the command. If your editor is adding them then that is a problem with the editor, not a latex question really. As others have said the \label{fig:1} in your definition will generate errors if you use the command more than once, also [H] is something to use rarely to force effects for special cases, I would never use it routinely ([H] was my idea originally) Sep 7, 2021 at 14:59

2 Answers 2

2

If you do

\newcommand{\addImageSeventyFive}[3][0.75]{
    \begin{figure}[H]
        \centering
        \includegraphics[width=#1\textwidth]{#2}
        \caption{#3 (size of #1)}\label{fig:1}
    \end{figure}
}

then the appropriate calls are of the form

\addImageSeventyFive{example-image}{Caption text}

if you're satisfied with the 75% size, or

\addImageSeventyFive[0.4]{example-image}{Caption text}

if you want to specify another fraction.

If your interface to LaTeX (VS Code, I guess from your question) adds a {} at the end of the calls, then it's an editor “feature”.

On the other hand, I'm not sure this is something I'd recommend doing for various reasons:

  1. the [H] option is very inflexible and leads to poor typesetting, generally;
  2. there is no way to set a \label, unless you type it inside the caption text;
  3. the \label{fig:1} in the code is wrong and will just produce a load of warnings about duplicate labels.

However, you know better about your document, so I suggest some improvement

\newcommand{\addImageSeventyFive}[3][0.75]{%
    \par
    \begin{figure}[H]
        \centering
        \includegraphics[width=#1\textwidth]{#2}
        \caption{#3 (size of #1)}
    \end{figure}
    \par
}

(figures are best placed between paragraphs, so LaTeX has at least some chance in doing the right page breaks).

If you need to label one of these figures, you can do

\addImageSeventyFive{example-image}{Caption text\label{fig:example}}
2
  • I was able to set \label command but it does not work with arguments. Actually I was dealing with it currently.. Sep 7, 2021 at 14:09
  • I think the "extra" curly brackets are the vscodes' thing.. Sep 8, 2021 at 5:07
1

I recommend not to hardcode the name of the cross-referencing-label. Otherwise you will get warnings/error-messages about multiply-defined-labels.

About the empty brace-group:

Probably this is due to the initial intention being something like

\newcommand{\addImageSeventyFive}[4][0.75]{%%%
    \begin{figure}[H]{%%%
        \centering
        \includegraphics[width=#1\textwidth]{#2}{%%%
        \caption{#3 (size of #1)}\label{#4}{%%%
    \end{figure}{%%%
}
...
\addImageSeventyFive{example-image-a}{Caption\ Test}{fig:1}
...
\addImageSeventyFive[0.40]{example-image-a}{Caption\ Test}{fig:2}
4
  • Thanks for the advice! But I guess, \label{} command is not working with the arguments, am I right? I have already test it, but could not produce proper output.. Sep 7, 2021 at 13:47
  • @OnurGürdoğan \label does not produce output to the .pdf-file. \label writes a record of cross-referencing-data to the .aux-file in terms of \newlabel{}{...}. Cross-referencing-commands like \ref and \pageref use this data. During the first compilation .aux-file doesn't exist yet, thus during the first compilation you get ?? instead of proper cross-referencing-phrases. For everything to match out correctly at least two compilations are needed without having .aux-files deleted between compilations. (The .log-file and the terminal usually inform about the need of re-running LaTeX.) Sep 7, 2021 at 13:55
  • I meant with the "output" cross-reference, you get the idea hahaha. Anyway it does not still accept the argument even if I used it inside the caption or outside of it.. Such as \label{fig:#3}. Sep 7, 2021 at 14:14
  • 1
    @OnurGürdoğan As the \caption-command does adjust the value of the figure-counter, a \label-command for "capturing" that value to .aux needs to be placed behind the \caption-command. I recommend not to place it into the argument of the \caption-command. || With \label{fig:#3} the argument #3 must denote tokens that can safely be put between \csname..\endcsname, i.e., only characters and fully-expandable stuff in the end expanding to characters is allowed. Sep 7, 2021 at 14:21

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.