I defined an operator
\DeclareMathOperator{\s2}{s_{2}}
And the second time I use it, it tells me "!Use of \s doesn't match its definition. \s 2..." In another instance of the document, it works perfectly fine.
As you can't have digits in command names normally, one option is to declare a command that accepts one additional number argument that is used internally as part of the command name:
\documentclass{article}
\usepackage{amsmath}
\newcommand{\s}[1]{\csname s#1\endcsname}
\newcommand{\declareS}[2]{%
\expandafter\DeclareMathOperator
\expandafter{\csname s#1\endcsname}{#2}%
}
\declareS1{s_{1}}
\declareS2{s_{2}}
\declareS{30}{s_{30}}
\begin{document}
$\s1(t) + \s2(t) > \s{30}(t)$
\end{document}
This defines a new command \s which requires one argument and then calls a command \sXX, where XX is the argument. To define the called command, you use \declareS which takes the same XX as first parameter and as second parameter the body of \DeclareMathOperator.
Make sure to use braces for the number argument if it is longer than a single digit. The \declareS command is also very limited, it always declares an \s... command and always maps it to a math operator. Adding extra parameters to make it more general is possible, of course.
\s2won't work. Try\stwoor\siiinstead. Or even better, define a macro with an argument. See also https://tex.stackexchange.com/q/9718/35864 – moewe Aug 08 '18 at 04:19\0to\9work, but letter-digit combinations won't. Depending on what exactly you did maybe it only worked by accident. Can you shows an example of the macro that worked, please? – moewe Aug 08 '18 at 04:23\DeclareMathOperator{\s2}{s_{2}}you define a delimited macro\sthat must always be followed by2to be complete. You then can't use\swithout a trailing2. If you try to use\s3it breaks because there is no2. I feel the solution is to define a macro with an argument. – moewe Aug 08 '18 at 04:28\s1,\s2, ...\s9working. When you have several\DeclareMathOperator{\s...}the one defined last should be the only one that works. – moewe Aug 08 '18 at 04:33\DeclareMathOperator{\s}{s}and writes_1later. If you must have the subscript in the command name use something like\siiinstead of\s2. – moewe Aug 08 '18 at 04:40\sxiii, but I see what you are getting at. It is, however, also impractical to define thirteen or more macros with almost the same definition that only differ in the subscript. In that case a macro with argument seems the way to go. There are ways around this (see my first link), but they do have their disadvantages. – moewe Aug 08 '18 at 04:50