I'm trying to make a macro using the xparse library together with xkeyval for optional key value pairs. Here's a minimal example of what I'm trying to do
cmd.sty
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{cmd}
\RequirePackage{xparse}
\RequirePackage{xkeyval}
\define@key{keyvalTest}{width}{\def\kt@width{#1}}
\define@key{keyvalTest}{caption}{\def\kt@caption{#1}}
\NewDocumentCommand{\keyvalTest}{ o m }{
\begingroup
% Set the default values, and override any extra if any optional arguments are present, else just set the default values
\IfValueTF{#1}
{\setkeys{keyvalTest}{width={50mm}, caption=\NoValue, #1}}
{\setkeys{keyvalTest}{width={50mm}, caption=\NoValue}}
\noindent
Path: #2 \\
Width: \kt@width \\
% Should print the caption if specified as an optional arg, else print "No Caption"
\IfValueTF{\kt@caption}
{Caption: \kt@caption}
{No Caption}
\endgroup
}
main.tex
\documentclass{article}
\usepackage{cmd}
\begin{document}
\keyvalTest{diagrams/pathToDiagram.pdf} \\
\keyvalTest[caption={hello}, width=3cm]{diagrams/pathToDiagram.pdf}
\end{document}
The example gives me this:
And an error saying that \NoValue{}
is not defined. I've only seen it used several places (for example here), but I've never seen it used in actual code. I suppose it might be deprecated, or is only valid for expl3?
I've been through the documentation for xparse without finding any macro that would act as NoValue. I've also tried reading through its source code, but I would like to avoid expl3 if possible.
I'm wondering where I should go from here. Should I rather be using blank, and using some kind of isblank
or isempty
function, or is there a better approach to this problem? I would think doing if else logic on missing named optional parameters would be quite common, but I can't seem to find any examples anywhere.