4

I have the following command in LaTeX:

\DeclareMathOperator{\logistic}{lgc}

I would like to modify it as follows:

  • If the user simply writes \logistic, it should display just "lgc", as it already does.
  • If the user writes \logistic{L}{P0}{k}, it should display "lgc_{L, P0, k}".

Thank you in advance.

1
  • 5
    Note that optional arguments in {} is generally not recommend as it is very confusing to users.
    – daleif
    Commented Nov 29 at 7:46

3 Answers 3

6

Your very special syntax can be implemented simply at TeX primitive level:

\def\logistic{\mathop{\rm lgc}\nolimits \futurelet\next\logisticA}
\def\logisticA{\ifx\next \bgroup _\bgroup \expandafter\logisticB \fi}
\def\logisticB#1{#1\futurelet\next\logisticC}
\def\logisticC{\ifx\next\bgroup ,\expandafter\logisticB \else \egroup\fi}

Tests:

$\logistic$

$\logistic_h$

$\logistic{A}{B_0}{C}$

\bye

No one is convincing me that using expl3 is easier.

3
  • Not even if I buy you an ice cream?
    – Gaussler
    Commented Nov 29 at 17:17
  • We have almost winter in our country at the moment.
    – wipet
    Commented Nov 29 at 19:39
  • @wipet Thank you very much for your response. The problem is solved! Commented Dec 1 at 7:49
8

Here's a solution that defines the macro \logistic to take a single, optional argument. According to LaTeX syntax rules, the optional argument of a macro must be encased in square brackets, not curly braces. Hence, instead of \logistic{L}{P0}{k}, you'd write \logistic[L,P0,k].

enter image description here

\documentclass{article} % or some other suitable document class

\usepackage{amsmath} % for '\DeclareMathOperator' macro
\DeclareMathOperator{\lgc}{lgc}
\newcommand\logistic[1][]{\lgc_{#1}}

\begin{document}
$\logistic \quad \logistic[L,P0,k]$
\end{document}
8
  • thank you very much for the response. However, there is a problem. In some cases, I need to write, for example, "lgc_n". With this code, an error is generated: ! Double subscript.. Commented Nov 29 at 8:24
  • 2
    @KώσταςΚούδας - I take it that you've written \logistic{n}. That would be wrong; you should write \logistic[n]. You could also write \lgc_{n}. And, for that matter, you could write \lgc_{L,P0,k} instead of \logistic[L,P0,k]. Note the use of curly braces to denote the "de facto" argument of \lgc and of square brackets to denote the optional argument of \logistic. BTW, I use the qualifier "de facto" deliberately, because strictly speaking \lgc does not actually take an argument. What LaTeX processes is not just {n} but, rather, _{n}.
    – Mico
    Commented Nov 29 at 9:07
  • 1
    @KώσταςΚούδας - Sorry, but I have no idea what you are trying to say. For instance, what does it mean to say that lgc_n "already exists"? After all, "lgc" by itself isn't even a command. Which command(s) did you define? Did you use the \DeclareMathOperator macro of the amsmath package to define \lgc (note the backslash character)? If not, why not?
    – Mico
    Commented Dec 1 at 7:56
  • 1
    @KώσταςΚούδας - How did you define \logistic, and how do you use this macro to generate "lgc_n"? Does \logistic take either a mandatory or an optional argument? Sorry, but my mind-reading skills are non-existent.
    – Mico
    Commented Dec 1 at 8:20
  • 1
    @KώσταςΚούδας - Best wishes to you.
    – Mico
    Commented Dec 1 at 8:26
8

Why?

This is a serious question, preliminary to a discussion. My answer?

If it is a subscript, input it as a subscript!

With

\DeclareMathOperator{\logistic}{lgc}

you'd use

\logistic_{n,P_0,k}

and you're just following the standard input conventions.

An alternative would be as Mico suggests:

\NewDocumentCommand{\logistic}{o}{%
  \operatorname{lgc}\IfValueT{#1}{_{#1}}%
}

where you'd input

\logistic[n,P_0,k]

But where's the advantage over the direct conventional input described above?

OK, if you insist in nonstandard and confusing syntax,…

\documentclass{article}
\usepackage{amsmath}

\ExplSyntaxOn

\NewDocumentCommand{\logistic}{}
 {
  \group_begin:
  \operatorname{lgc}
  \__koudas_lgc_grab:
 }

\seq_new:N \l__koudas_lgc_seq

\cs_new_protected:Nn \__koudas_lgc_grab:
 {
  \peek_meaning:NTF \c_group_begin_token
   {% start the recursion
    \__koudas_lgc_grab_next:n
   }
   {
    \__koudas_lgc_grab_end:
   }
 }
\cs_new_protected:Nn \__koudas_lgc_grab_next:n
 {
  \seq_put_right:Nn \l__koudas_lgc_seq {#1}
  \__koudas_lgc_grab:
 }
\cs_new_protected:Nn \__koudas_lgc_grab_end:
 {
  \seq_if_empty:NF \l__koudas_lgc_seq { \sb{ \seq_use:Nn \l__koudas_lgc_seq {,} } }
  \group_end:
 }

\ExplSyntaxOff

\begin{document}

$\logistic(x)$

$\logistic{n}(x)$

$\logistic{n}{P_0}(x)$

$\logistic{n}{P_0}{k}(x)$

$\logistic{n,P_0,k}(x)$

\end{document}

examples

Look at the last two lines: if you really think that inputting

\logistic{n}{P_0}{k}(x)

is clearer and easier than

\logistic{n,P_0,k}(x)

go for it. If this example convinces you that the latter is better, then, why not forget about all the peeks and recursion? Just use

\logistic_{n,P_0,k}(x)

as suggested at the beginning with the simple definition

\DeclareMathOperator{\logistic}{lgc}
1
  • Why? Because I expected it to be much easier! In Python or R, it’s easy to set a default value in a function definition, and I assumed something similar would be possible here. Also, since I’ve already written a text that includes this notation, I wanted a solution that would allow me to adopt the new notation without having to make changes to the main text. Needless to say, I truly appreciate the time you’ve taken to help me! Commented Dec 1 at 7:47

You must log in to answer this question.

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