0

I've defined an environment similar to what appears below, and I want two of the arguments to be required. My impression from the interface3 document is that saying .value_required in the keys_define has the effect of doing what I want: if the argument isn't provided to the environment, then compilation should fail. Instead, if the user omits that argument, then the first variable (a string) is left as the empty string -- or maybe it's undefined. The second value (a float) is set to zero.

How can I tell whether the user has provided the required arguments?

\documentclass[10pt]{article}

\ExplSyntaxOn

\NewDocumentEnvironment{intfig} { O{} }
  {
    \group_begin:
    \keys_set:nn{intfig}{#1}
    
    \rm{string\ is\ } \l_intfig_astring_str \\
    \rm{height\ is\ } \fp_eval:n { \l_intfig_height_fp }\\

    % There isn't anything like \bool_eval:n it seems.
    \rm{done\ boolean\ is\ }  \bool_if:NTF\l_intfig_done_bool{TRUE}{FALSE} \\
    \group_end:
}{}

\keys_define:nn { intfig }
{
  astring.str_set:N = \l_intfig_astring_str,
  astring.value_required:n = true,
  height.fp_set:N = \l_intfig_height_fp,
  height.value_required:n = true,
  done.bool_set:N = \l_intfig_done_bool,
}

\ExplSyntaxOff

\begin{document}

\begin{intfig}
  random body
\end{intfig}

\end{document}
6
  • unrelated but \rm is not defined by default in latex, but in document classes where it is defined (for compatibility with documents from the 1980s) the syntax is {\rm abc} not \rm{abc} Feb 5, 2022 at 22:10
  • You're right. Old habits. They may have been formed in the 1980s. Feb 5, 2022 at 22:14
  • if the user should always set some of the keys you should use {m} not { O{} } as if you have to use the key set argument no point in making it optional. Feb 5, 2022 at 22:32
  • I'm not sure what you mean. Are you saying that for clarity regarding my (the programmer) intent, it would be better to use {m}? I don't see that it changes the code in any other way. Feb 5, 2022 at 22:53
  • it would change the user syntax to be \begin{intfig}{astring=zzz} not \begin{intfig}[astring=zzz] as it is a bit weird to make the key setting argument optional if you are going to raise an error if keys are not set. Feb 5, 2022 at 22:58

2 Answers 2

3

The .value_required means that you can not use just astring you have to give a value astring=xxx

so this gives an error

\documentclass[10pt]{article}

\ExplSyntaxOn

\NewDocumentEnvironment{intfig} { O{} }
  {
    \group_begin:
    \keys_set:nn{intfig}{#1}
    
    \textrm{string\ is\ } \l_intfig_astring_str \\
    \textrm{height\ is\ } \fp_eval:n { \l_intfig_height_fp }\\

    % There isn't anything like \bool_eval:n it seems.
    \textrm{done\ boolean\ is\ }  \bool_if:NTF\l_intfig_done_bool{TRUE}{FALSE} \\
    \group_end:
}{}

\keys_define:nn { intfig }
{
  astring.str_set:N = \l_intfig_astring_str,
  astring.value_required:n = true,
  height.fp_set:N = \l_intfig_height_fp,
  height.value_required:n = true,
  done.bool_set:N = \l_intfig_done_bool,
}

\ExplSyntaxOff

\begin{document}

\begin{intfig}[astring]
  random body
\end{intfig}

\end{document}
 ! LaTeX3 Error: The key 'intfig/astring' requires a value.

If you want to detect the case that astring is not used at all you can use .initial to give it a distinguished value that you can detect as being different from any user supplied value.

2
  • So "required" means that if the user refers to that key, then he is required to provide a value. Or, he could also choose not to refer to that key at all, and that would not be in error. Thank you. Feb 5, 2022 at 22:22
  • It looks like there is no reason to "require" these keys. There needs to be a "this is wrong" setting for those values that I'll have to check in any case. Feb 5, 2022 at 22:24
1
  1. .value_required:n means: If the key is provided, then it must be provided with a value.
    .value_required:n does not mean that the key must be provided.
  2. If you want compilation to fail = to stop with an error-message(?) in case some specific keys are not provided, then, in my humble opinion, the argument for passing keys and values should probably not be optional with empty default-value but either optional with a default which provides default-values for the keys or mandatory so that if you want compilation to work out you don't get around providing that argument.
  3. You can introduce a boolean and instead of the handler .set_str:N you can use the handler .code:n both for switching the boolean and for setting the string-variable.
\documentclass[10pt]{article}

\ExplSyntaxOn

\NewDocumentEnvironment{intfig} { m }
  {
    \group_begin:
    \bool_set_false:N \l_intfig_done_bool
    \keys_set:nn{intfig}{#1}
    
    \textrm{string\ is\ } \l_intfig_astring_str \\
    \textrm{height\ is\ } \fp_eval:n { \l_intfig_height_fp }\\

    \textrm{done\ boolean\ is\ }  \bool_if:NTF\l_intfig_done_bool{TRUE}{FALSE} \\
    \group_end:
}{}

\bool_new:N \l_intfig_done_bool
\str_new:N \l_intfig_astring_str

\keys_define:nn { intfig }
{
  astring.code:n= \bool_set_true:N  \l_intfig_done_bool 
                  \str_set:Nn \l_intfig_astring_str {#1},
  astring.value_required:n = true,
  height.fp_set:N = \l_intfig_height_fp,
  height.value_required:n = true,
  done.bool_set:N = \l_intfig_done_bool,
}

\ExplSyntaxOff

\begin{document}

\begin{intfig}{astring=Some string}
  random body
\end{intfig}

\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .