3

I fail to obtain a default value for an option of a package. What is a good way to obtain a default "string" value for an option?

Here is what I have tried.

% File: main.sty

\NeedsTeXFormat{LaTeX2e}
\RequirePackage{expl3}
\ProvidesExplPackage{main}{}{}{}

\RequirePackage{l3keys2e}


\keys_define:nn {main_opts} {
    lang .tl_set:N  = \l_main_opt_lang,
    lang .default:n = \tl_set:Nn\l_main_opt_lang{EN},
}

\ProcessKeysPackageOptions{main_opts}


\NewDocumentCommand{\test}{}{
    LANG = \l_main_opt_lang
}

% LaTeX file to test

\documentclass[12pt,a4paper]{article}

\usepackage[utf8]{inputenc}

\usepackage[lang=FR]{main}   % OK
%\usepackage{main}            % KO

\begin{document}

\test{}

\end{document}

PS: is there a good tutorial about boolean and key-value like options for packages and also macros using the LaTeX3 machinery?

2
  • 1
    You don’t need \RequirePackage{expl3}. This is loaded down by default. I guess this was always the case when you did \ProvidesExplPackage, but even standard LaTeX2e now (as of last Summer, I think) loads down expl3 and most of xparse by default.
    – Gaussler
    Jul 11, 2021 at 15:03
  • Thanks for this. I have just copied and pasted a code, to be honest. :-)
    – projetmbc
    Jul 11, 2021 at 18:48

1 Answer 1

5

It looks like you are not looking for a default value, you are looking to set the initial value (at least in l3keys parlance).

The default value becomes relevant if a key is given without a value. The initial value becomes relevant if a key is not used at all.

\begin{filecontents}{main.sty}
\NeedsTeXFormat{LaTeX2e}
\RequirePackage{expl3}
\ProvidesExplPackage{main}{}{}{}

\RequirePackage{l3keys2e}


\keys_define:nn {main_opts}
  {
    lang .tl_set:N  = \l_main_opt_lang,
    lang .initial:n = EN,
  }

\ProcessKeysPackageOptions{main_opts}


\NewDocumentCommand{\test}{}
  {
    LANG = \l_main_opt_lang
  }
\end{filecontents}

\documentclass[12pt,a4paper]{article}

\usepackage[utf8]{inputenc}

%\usepackage[lang=FR]{main}   % OK
\usepackage{main}            % KO

\begin{document}

\test{}

\end{document}

gives

LANG=EN


Some more details from the documentation interface3, Chapter 26 The l3keys package Key–value interfaces

⟨key⟩ .default:n = {⟨default⟩}

Creates a ⟨default⟩ value for ⟨key⟩, which is used if no value is given. This will be used if only the key name is given, but not if a blank ⟨value⟩ is given:

\keys_define:nn { mymodule }
{
  key .code:n = Hello~#1,
  key .default:n = World
}
\keys_set:nn { mymodule }
{
  key = Fred, % Prints ’Hello Fred’
  key,        % Prints ’Hello World’
  key = ,     % Prints ’Hello ’
}

The default does not affect keys where values are required or forbidden. Thus a required value cannot be supplied by a default value, and giving a default value for a key which cannot take a value does not trigger an error.

and

⟨key⟩ .initial:n = {⟨value⟩}

Initialises the ⟨key⟩ with the ⟨value⟩, equivalent to

\keys_set:nn {⟨module⟩} { ⟨key⟩ = ⟨value⟩ }

In this example a default value probably does not make a lot of sense and I would probably want to explicitly make passing an empty argument an error by setting .value_required:n = true

\keys_define:nn {main_opts}
  {
    lang .tl_set:N  = \l_main_opt_lang,
    lang .initial:n = EN,
    lang .value_required:n = true,
  }
0

You must log in to answer this question.

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