I am having issues with writing a Macro. The idea is that I am trying to setup a (simple) Macro to hold the figures for an article, but not all the figures have captions and thus I want that macro to have the option to set the:
- Source of the image (e.g., my_figure.png)
- The width with respect to the \textwidth (e.g., 0.8 -> 80% of the text width)
- The caption, if needed.
By using the \newcommand macro I can achieve this:
\usepackage{ifthen}
\newcommand*\tkzimage[3]{%
\begin{figure}[h!]
\centering
\begin{tikzpicture}
\node[inner sep=0pt] (w) at (0,0)
{\includegraphics[width=#2\textwidth]{images/#1}};
\end{tikzpicture}
\ifthenelse{\equal{#3}{}}{ }{\caption{#3}}
\end{figure}
}
The problem that I am having is when invoking the macro, I get the expected behavior, but the code doesn't work if I don't have \caption{<whatever>} in the empty slot:
\ifthenelse{\equal{#3}{}}{}{\caption{#3}} % Throws an error.
\ifthenelse{\equal{#3}{}}{ }{\caption{#3}} % Throws an error.
\ifthenelse{\equal{#3}{}}{<whatever>}{\caption{#3}} % Throws an error.
\ifthenelse{\equal{#3}{}}{\caption{<whatever>}>}{\caption{#3}} % Works!
The function is being invoked as:
\tkzimage{image\_text.png}{0.5}{} # With NO caption.
\tkzimage{image\_text.png}{0.5}{Some text} # With some caption.
I am puzzled as to why this behavior and how to solve it. The pgfkeys package is not working either, so I don't know how else to approach this; help would be appreciated.
Compiler Error
When the above code is used without the \caption code:
LaTeX Warning: `!h' float specifier changed to `!ht'.
File: images/pycharmInstallation000.png Graphic file (type png)
<use images/pycharmInstallation000.png>
Package luatex.def Info: images/pycharmInstallation000.png used on input line 18.
(luatex.def) Requested size: 234.73523pt x 189.33287pt.
./aaf_introduction_neovim.tex:18: Package hypcap Error: You have forgotten to use \caption.
Thanks!

image\_text.pngwould not work you would wantimage_text.png– David Carlisle Feb 15 '24 at 22:13example-image.pngand show the exact error message copied from the log in a code block so lines are preserved. It's impossible to debugThrows an errorif you do not say what the error is or provide a test file to reproduce it. – David Carlisle Feb 15 '24 at 22:15\ifthenelsewill only work if the caption text is safe in an edef, so no fragile commands. better would be to make that an optional argument rather than test for empty, but if you do want to test for empty see https://tex.stackexchange.com/questions/212740/safe-test-for-an-empty-expanded-macro-argument – David Carlisle Feb 15 '24 at 22:19\begin{figure}...\end{figure}, the\captioncommand is required. Trying to find a workaround now... – Andrumen1 Feb 15 '24 at 22:20figurebut as you have shown no example it's impossible to say if you are using a non standard version – David Carlisle Feb 15 '24 at 22:29