0

In LaTeX, one can declare functions in documents using the \newcommand or \newenvironment commands. Here's how one can define a simple function and an environment.

\newcommand{\myfunction}[2]{Function: #1 + #2}

In this example, \newcommand{\myfunction}[2] defines a new command called \myfunction that takes two arguments, that one can use in a document like this:

The function output is: $\myfunction{a}{b}$

Can one make a function like in other languages directly ? For instance, make something like the following.

\feuds_newteorema_simple:nn
  { 
    \newtheorem{#1_inner} 
    {\color{\feuds_teorema_setcolor_tl}#2} 
  }

I do get confused when writing all this. Would one enclose with {} when writing commands on the same line ?

Where con I find the declaration for \newtheorem ?

Veak
  • 1
  • You wouldn't define the first function that way for use in maths because the spacing will be wrong. In the second case, what's the new function supposed to be? You can't say \feuds_newteorema_simple:nn {definition} to define \feuds_newteorema_simple:nn. The code you've given would try to use \feuds_newteorema_simple:nn, so it would look for a definition of that function to feed the next two arguments. If the function doesn't exist, you'd get an error. If it does, you'd get a different error because you've only given one argument and it expects two. – cfr Sep 29 '23 at 19:28
  • what do you mean by "make something like the following" ? you can change \feuds_newteorema_simple:nn to \cs_new_protected:Npn\feuds_newteorema_simple:nn#1#2 and then that fragment defines the command. – David Carlisle Sep 29 '23 at 19:29
  • newtheorem has a definition in the latex format but several packages redefine it: amsthm, ntheorem, ... so where it is documented depends on which version you are using. – David Carlisle Sep 29 '23 at 19:32
  • See source2e.pdf, which is already somewhere on your computer, or can be downloaded from https://ctan.org/pkg/source2e?lang=en Don't forget \def\myfunction#1#2{Function: #1 + #2} – John Kormylo Sep 29 '23 at 19:42
  • How can one figure out which definition one is using ? It is one of the reasons I do not like having too many dependencies on other packages. Can one be called one in certain circumstances, whilst another one when using amsthm etc. – Veak Sep 29 '23 at 20:01
  • @DavidCarlisle But using \cs_new_protected:Npn is just to assign the protected mark rather than the defining the function. The definition of the function is \feuds_newteorema_simple. How would a function definition look like when not using the protected mark ? – Veak Sep 29 '23 at 20:29
  • No, \cs_new_protected:Npn defines a new function (which is protected and can absorb arguments which include paragraph breaks). If you don't want it to be protected, use \cs_new:Npn (allowing paragraph breaks). Then the p means you're going to specify the parameters explicitly, rather than having them inferred from the argument specification in the name. – cfr Sep 29 '23 at 20:34
  • 1
    You should know which definition you're using because you know which packages you've loaded ;). If you haven't loaded anything which changes the definition, you have the LaTeX default. If your preamble includes \usepackage{amsthm}, you're using the definition from amsthm (unless something later redefines the command again). You can also say \show\newtheorem to be shown the current definition on the terminal during compilation of the document. (However, this isn't always helpful if the command is protected, for example.) – cfr Sep 29 '23 at 20:36
  • @cfr Would that mean that I cannot just start with the name of the function but have to use an appropriate new command like \cs_new to define a function. – Veak Sep 29 '23 at 20:42
  • Yes. You have to define it first and you have to tell TeX you're defining it. Otherwise, TeX will assume you're trying to use it and give you an error. Note that \cs_new will give an error because it is not the name of a function if you omit the colon and signature. – cfr Sep 29 '23 at 20:49
  • Right. I understand the colon and signature. – Veak Sep 29 '23 at 21:03
  • no it defines the command, as described in the manual and explained at length in answers to your earlier questions – David Carlisle Sep 29 '23 at 21:14

1 Answers1

1

Your question isn't at all clear, there isn't any difference between your two examples except that you omitted the defining command from the second.

Your second block is like

\myfunction{Function: #1 + #2}

And like the first block it needs the command name to be defined.

You could use

\newcommand\feuds_newteorema_simple:nn[2]
  { 
    \newtheorem{#1_inner} 
    {\color{\feuds_teorema_setcolor_tl}#2} 
  }

which is the form used in the first example although a more idiomatic expl3 definition would be

\cs_new_protected:Npn \feuds_newteorema_simple:nn #1#2
  { 
    \newtheorem{#1_inner} 
    {\color{\l_feuds_teorema_setcolor_tl}#2} 
  }

as (as noted in an earlier question, the local variable name should start l_ to follow the expl3 naming conventions)

David Carlisle
  • 757,742