I am new to expl3
and would like to create an environment with various key-value arguments.
At the moment I am struggling to detect weather a key was set or not.
\documentclass{article}
\usepackage{xparse,graphicx}
\ExplSyntaxOn
% keys
\keys_define:nn { mybodule/bfigure }
{
caption .tl_set:N = \l_mybodule_caption,
shift .dim_set:N = \l_mybodule_shift ,
shift .default:n = 0em
}
\NewDocumentEnvironment{bfigure}{O{}}{
\keys_set:nn { mybodule/bfigure } { #1 }
\tl_if_empty:NTF \l_mybodule_caption {true} {false}
}{}
\ExplSyntaxOff
\begin{document}
\begin{bfigure}[caption=mycaption]
\includegraphics[width=3cm]{example-grid-100x100pt}
\end{bfigure}
\end{document}
What it the correct way to implement the if caption set then ... else ...
part?
Am I even using the \keys_define
and \keys_set:nn
correctly?
shift .default:n = 0em
orshift .initial:n = 0em
. The.default
value is used when you give the key without value ([shift]
), the.initial
value is the pre-set value that is used if the key is not used.\tl_if_empty:NTF
appears to work. Since a newly definetl
variable is always empty your code should also be safe. But you should probably wait for a realexpl3
expert for confirmation. If you want to be on the safe side you can addcaption .initial:n = {},
, but if I'm right this is not needed as long as you keep your\keys_set:nn { mybodule/bfigure } { #1 }
calls local and don't re-use\l_mybodule_caption
anywhere..tl_set:N
s and checks for their presence with\tl_if_empty:NTF
/\tl_if_empty:NF
. Look for\l_guide_number_tl
and\l_guide_keywords_tl
.\l_mybodule_caption
should probably be called\l_mybodule_caption_tl
and\l_mybodule_shift
would be\l_mybodule_shift_dim
if you want to followexpl3
naming conventions.