0

I really do not understand the purpose of .initial:n. It is only useful for the first call to a command that does not have user-values, after that the current values are used.

Would like to have the starred version always using the initial values when not having the argument, default when just using the key names, and using the supplied values when set as arguments.

For the unstarred version, I would like to use the initial or current values when not having the argument, default when just using the key names, and using the supplied values when set as arguments.

\documentclass{article}

\ExplSyntaxOn

\dim_new:N \l_mymodule_width_dim \dim_new:N \l_mymodule_height_dim \dim_new:N \l_mymodule_margin_dim

\keys_define:nn { mymodule } { width .dim_set:N = \l_mymodule_width_dim, width .initial:n = 34pt, width .default:n = 36pt,

height .dim_set:N = \l_mymodule_height_dim,
height .initial:n = 34pt,
height .default:n = 36pt,

margin .dim_set:N = \l_mymodule_margin_dim,
margin .initial:n = 34pt,
margin .default:n = 36pt,

}

\NewDocumentCommand {\setdimensionsC} { s o } { \IfBooleanTF {#1} { \IfValueTF {#2} { \keys_set:nn { mymodule } { width = 36pt, height = 36pt, margin = 36pt } \keys_set:nn {mymodule} {#2} } { \keys_set:nn { mymodule } { width = 36pt, height = 36pt, margin = 36pt } } } { \IfValueTF {#2} { \keys_set:nn {mymodule} {#2} } { \keys_set:nn {mymodule} {} } }

  % Use the dimensions in your document or perform other actions
  \fbox { \begin{tabular}{ll}
            Width:  & \dim_use:N \l_mymodule_width_dim  \\
            Height: & \dim_use:N \l_mymodule_height_dim \\
            Margin: & \dim_use:N \l_mymodule_margin_dim
          \end{tabular} }

}

\ExplSyntaxOff

\begin{document}

With Initial Values: \setdimensionsC

With Default values: \setdimensionsC[width,height,margin]

With Current Values: \setdimensionsC

\end{document}

Ragonese
  • 171
  • 4
  • What precisely you're not understanding? The code is behaving as expected. Possibly your expectations aren't correct. – egreg Mar 01 '24 at 23:15
  • did you intent to use the module _mymodule (that has no keys) in the second set command? – David Carlisle Mar 01 '24 at 23:18
  • ! LaTeX Error: The key '\mymodule/width' is unknown and is being ignored. – David Carlisle Mar 01 '24 at 23:19
  • similarly you presumably meant mymodule not \mymodule in the first setting. – David Carlisle Mar 01 '24 at 23:22
  • why are you calling \keys_set:nn {mymodule} {} as you are explicitly calling it with an empty key list so this will never do anything? (as previously explained it does not reset the .initial values which is what I guess you are hoping). – David Carlisle Mar 02 '24 at 00:12
  • You are correct, the values in the variables do not get set to the values in .initial:n. I then wonder about the real usefulness of .initial:n. – Ragonese Mar 02 '24 at 01:34
  • initial:n is there as a convenience for those who would like to put everything in \keys_define:nn rather than initialising with a use of \keys_set:nn. This has been explained several times already. – Joseph Wright Mar 02 '24 at 06:45
  • as answered in your earlier questions initial sets the value at the point of definition why is that not clear, that you keep re-asking the same thing? – David Carlisle Mar 02 '24 at 10:07
  • I want to be certain about the choices to ensure that I make conclusions and decisions with supreme confidence. @JosephWright has made a very valuable comment that had not been discussed until now. – Ragonese Mar 02 '24 at 20:18
  • 1
    How about you read the answers to your earlier questions? For instance, read https://tex.stackexchange.com/a/711964/117050 and then think about why you "wonder about the real usefulness of .initial:n". – Skillmon Mar 02 '24 at 20:44

1 Answers1

1

You seem to want to use strategy 2, as outlined in my previous answer. So the .initial:n bits are completely irrelevant.

\documentclass{article}

\ExplSyntaxOn

\tl_new:N \l_mymodule_initial_tl

\keys_define:nn { mymodule } { width .dim_set:N = \l_mymodule_width_dim, width .default:n = 36pt,

height .dim_set:N = \l_mymodule_height_dim,
height .default:n = 36pt,

margin .dim_set:N = \l_mymodule_margin_dim,
margin .default:n = 36pt,

} \keys_precompile:nnN { mymodule } { width=34pt, height=34pt, margin=34pt } \l_mymodule_initial_tl

\NewDocumentCommand {\setdimensionsC} { s O{} } { \IfBooleanTF {#1} { \keys_set:nn { mymodule } { width = 8cm, height = 4cm, margin = 1.5cm } \keys_set:nn {mymodule} {#2} } { \tl_use:N \l_mymodule_initial_tl \keys_set:nn {mymodule} {#2} }

  % Use the dimensions in your document or perform other actions
  \fbox { \begin{tabular}{ll}
            Width:  & \dim_use:N \l_mymodule_width_dim  \\
            Height: & \dim_use:N \l_mymodule_height_dim \\
            Margin: & \dim_use:N \l_mymodule_margin_dim
          \end{tabular} }

}

\ExplSyntaxOff

\begin{document}

With Initial Values: \setdimensionsC

With Default values: \setdimensionsC[width,height,margin]

With Initial Values: \setdimensionsC

With : \setdimensionsC

With * (modified): \setdimensionsC*[height=20pt]

\end{document}

enter image description here

Note that you don't need o, just to pass O{} as default.

egreg
  • 1,121,712
  • Although one can use O{}, I want to see how handling the case of 'o' would look like. You are suggesting not to use the empty {} case to capture the current values, – Ragonese Mar 01 '24 at 23:32
  • @Ragonese Sorry, but what you write doesn't make sense – egreg Mar 02 '24 at 00:11
  • It does not look that .initial:n has much good use. Because the values are only be accessed upon the first call to \setdimensionsC, and only if the values are not supplied. Any other way, the current values are accessed upon calling \setdimensionsC. – Ragonese Mar 02 '24 at 00:14
  • @Ragonese It has, if you use strategy 1. – egreg Mar 02 '24 at 00:50
  • With strategy 1, you never actually change the values in the variables. Are keys customarily intended to use the values in initial when no arguments are supplied to a command ? – Ragonese Mar 02 '24 at 01:31
  • @Ragonese with strategy 1 you change the values locally on each call. You can additionally provide means to change them for any future call. This is the way key=value arguments in LaTeX are implemented at least 90% of the time. Strategy 2 is rather exotic and is only needed if you for some reason have to use no groups (can happen, but is rather rare). Initial is just that, an initial value, same as if you used a programming language like C and said int foo = 5;, you'd declare foo an Integer and initialise it to the value 5, and later can change that value. – Skillmon Mar 02 '24 at 21:57