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.
{}
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){}
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)