1

I would like to create a command for Landau's notations (small o for example), which would have an optional argument consisting of two elements (the variable and the point) separated by a comma. How do I do this?

Here is an example.

\documentclass[11pt]{article}
\usepackage{amsmath}

\begin{document}

% \petito[x, \infty]{\ln x}. Expected result : $ \underset{x \rightarrow \infty}{\mathrm{o}} \left( \ln x \right)$

% \petito{\ln x}. Expected result : $ \mathrm{o} \left( \ln x \right)$

\end{document}

jowe_19
  • 939
  • Is there a special reason why this is tagged with expl3, latex3 and xparse? – Jasper Habicht Mar 08 '23 at 15:17
  • 1
    I thought a simple solution would be to use latex3as here: https://tex.stackexchange.com/questions/87407/iterating-through-comma-separated-arguments – jowe_19 Mar 08 '23 at 15:20

2 Answers2

2

To me the \petito[x,\infty]{...} syntax is bad, both to read and to parse, easier to just use [x\to\infty] directly

\documentclass[11pt]{article}
\usepackage{amsmath}
\DeclareMathOperator*{\ooo}{o}
\NewDocumentCommand\petito{o m}{
  \IfNoValueTF{#1}{
    % normal case
    \ooo\left(#2\right)
  }{
    \ooo_{#1}\left(#2\right)
  }
}
\begin{document}

$\petito[x\to\infty]{\ln x}$

[ \petito[x\to\infty]{\ln x} ]

$ \underset{x \rightarrow \infty}{\mathrm{o}} \left( \ln x \right)$

$\petito{\ln x}$

$ \mathrm{o} \left( \ln x \right)$

\end{document}

Notes: $...$ should never write below, that disturbs the line spacing, thus \DeclareMathOperator* is used to make a macro that behaves like \lim.

Additionally, I'm not a fan of the fence autoscaling, but I've left it here.

daleif
  • 54,450
1

I don't see how the proposed syntax would be clearer or easier than

\petito_{x\to\infty}(\log x)

or, if you want to specify in an easier way the size of the parentheses,

\petito_{x\to\infty}{\log x}

with maybe

\petito_{x\to\infty}[big]{\log x}

for larger parentheses or

\petito_{x\to\infty}[*]{\log x}

for autosizing ones.

You can realize the latter syntax quite easily with ltcmd (formerly xparse).

\documentclass{article}
\usepackage{amsmath}

\NewDocumentCommand{\petito}{e{}O{}m}{% \genericlandau{o}{#1}{#2}{#3}% } \NewDocumentCommand{\grando}{e{}O{}m}{% \genericlandau{\mathit{O}}{#1}{#2}{#3}% }

\ExplSyntaxOn

\NewDocumentCommand{\genericlandau}{mmmm} { \operatorname*{#1} \IfValueT{#2}{\sb{#2}}% the subscript \jowe_landau_arg:nn { #3 } { #4 } }

\cs_new_protected:Nn \jowe_landau_arg:nn { \str_case:nnF { #1 } { {}{\left} {}{} } { \use:c { #1l } } ( #2 \str_case:nnF { #1 } { {}{\right} {}{} } { \use:c { #1r } } ) }

\ExplSyntaxOff

\begin{document}

\begin{gather} \petito{\log x} \ \petito[big]{\log x} \ \petito[Big]{\log x} \ \petito[*]{\frac{\sin x}{x}} \ \petito_{x\to\infty}{\log x} \ \petito_{x\to\infty}[big]{\log x} \ \petito_{x\to\infty}[Big]{\log x} \ \petito_{x\to\infty}[*]{\frac{\sin x}{x}} \ \grando{\log x} \ \grando[big]{\log x} \ \grando[Big]{\log x} \ \grando[*]{\frac{\sin x}{x}} \ \grando_{x\to\infty}{\log x} \ \grando_{x\to\infty}[big]{\log x} \ \grando_{x\to\infty}[Big]{\log x} \ \grando_{x\to\infty}[*]{\frac{\sin x}{x}} \end{gather}

\end{document}

enter image description here

egreg
  • 1,121,712