3

I need to define a function that takes no parameters, then define a function with same name but with a single parameter, and call the first function from inside the second.

If this is not how it's done, I would appreciate if you explain how to create several functions with different number (and type) of parameters in a similar way expl3 package does (it has several versions of same function).

\documentclass[varwidth=50mm]{standalone}
\usepackage{expl3}
\begin{document}
\ExplSyntaxOn

% no parameters needed
\cs_new:Nn\userDefined_century:{100}
\userDefined_century:
% ----------------------
% following doesn't work
% ERROR: Illegal parameter number in definition of \userDefined_century:N
% ----------------------
% create same function but with parameter this time,
%  and set parameter (integer variable) to value of previous function
\cs_new:Nn\userDefined_century:N#1
  { \int_set:N#1\userDefined_century:
  }
% create integer and set it with our function
\int_new:N\l_userDefined_number_int
\userDefined_century:N\l_userDefined_number_int

\ExplSyntaxOff
\end{document}
1
  • 2
    Off topic to the question, but you ask “how to [...] in a similar way expl3 package does”. I am, as you are, very new to expl3, so here's a few tips from a rookie to another: To learn it I first read The LaTeX3 Interfaces (not the whole thing, of course, just to get an idea of what functions it has to offer), and now use it to search for help when trying to use a specific function. Also, when I want to know how expl3 does something I just look at the kernel code in expl3-code.tex and try to figure out what it does. These two are the main sources of help I used for expl3. May 27, 2019 at 1:35

1 Answer 1

4

First, it's \int_set:Nn, not \int_set:N. Section 3 Setting and incrementing integers of the l3int module (around page 85) of The LaTeX3 Interfaces contains the documentation of this and other related functions.

Second, the documentation for \cs_new:Nn (around page 12 of the same document) says:

<code>\cs_new:Nn</code> <code>⟨function⟩</code> <code>{⟨code⟩}</code> → Creates <code>⟨function⟩</code> to expand to <code>⟨code⟩</code> as replacement text. Within the <code>⟨code⟩</code>, the number of <code>⟨parameters⟩</code> is detected automatically from the function signature. These <code>⟨parameters⟩</code> (<code>#1</code>, <code>#2</code>, etc.) will be replaced by those absorbed by the function. The definition is global and an error results if the <code>⟨function⟩</code> is already defined.

with emphasis in “the number of ⟨parameters⟩ is detected automatically from the function signature”. That means you can't give a ⟨parameter text⟩ for \cs_new:Nn. The so-called ⟨parameter text⟩ is a p-type argument and it is used when you define a function with \cs_new:Npn (the p is not silent :).

Plus, any function which is not expandable (that is, “explodes inside \edef”) should be defined with \cs_new_protected:Nn (or \cs_new_protected:Npn or some other with _protected) to ensure that it is not expanded in a context where it will break. Your \userDefined_century:N function is not expandable because it contains an assignment (\int_set:Nn), thus it must be _protected.

Your definition needs to be either:

%                  ↓ p is silent
\cs_new_protected:Nn \userDefined_century:N % no ⟨parameter text⟩ here
  { \int_set:Nn #1 { \userDefined_century: } }

or

%                  ↓ with p
\cs_new_protected:Npn \userDefined_century:N #1 % the ⟨parameter text⟩ is '#1'
  { \int_set:Nn #1 { \userDefined_century: } }

but not anything in between.

Changing that your code works fine:

\documentclass[varwidth=50mm]{standalone}
\usepackage{expl3}
\begin{document}

\ExplSyntaxOn

\cs_new:Nn \userDefined_century: { 100 }
\userDefined_century: \par

\cs_new_protected:Nn \userDefined_century:N
  { \int_set:Nn #1 { \userDefined_century: } }

\int_new:N \l_userDefined_number_int
\userDefined_century:N \l_userDefined_number_int
\int_use:N \l_userDefined_number_int

\ExplSyntaxOff
\end{document}
0

You must log in to answer this question.

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