0

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.

John Joe
  • 111
  • 4
  • 1
    I don't think you can have numbers in your command name. LaTeX has certain rules about what constitutes a macro name and \s2 won't work. Try \stwo or \sii instead. 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
  • But it already worked once and is still continuing to work in the first instance despite the failure in the second instance, so I can actually have numbers in the command name. – John Joe Aug 08 '18 at 04:20
  • One-digit names like \0 to \9 work, 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
  • Okay, so it literally works with s3 for any number of times, but not s2, and s3 uses the exact same format. – John Joe Aug 08 '18 at 04:24
  • Ah, I think I see what is going on. I'd have to confirm this, but here goes. When you write \DeclareMathOperator{\s2}{s_{2}} you define a delimited macro \s that must always be followed by 2 to be complete. You then can't use \s without a trailing 2. If you try to use \s3 it breaks because there is no 2. I feel the solution is to define a macro with an argument. – moewe Aug 08 '18 at 04:28
  • You say that if I try to use \s3 it breaks, but \s3 is the one that fully functions. – John Joe Aug 08 '18 at 04:30
  • No I say that you can have exactly one of \s1, \s2, ... \s9 working. When you have several \DeclareMathOperator{\s...} the one defined last should be the only one that works. – moewe Aug 08 '18 at 04:33
  • See also https://tex.stackexchange.com/a/66671/35864 – moewe Aug 08 '18 at 04:37
  • Okay that makes sense then. So the argument is obviously going to change a lot for such a vague operator, I don't want to limit any particular version to just one argument. Is there a more efficient way to overcome that issue? – John Joe Aug 08 '18 at 04:37
  • Define a command with an argument, see for example: https://tex.stackexchange.com/q/34818/35864. If it were me I'd probably just say \DeclareMathOperator{\s}{s} and write s_1 later. If you must have the subscript in the command name use something like \sii instead of \s2. – moewe Aug 08 '18 at 04:40
  • That's grossly impractical because then I would end up having siiiiiiiiiiiii which is going to look unprofessional to anyone who reviews the document. Why wouldn't LaTex just let the authors notate their operators as they want? What could possibly be the harm in using an additional number or two? – John Joe Aug 08 '18 at 04:45
  • Obviously that would be \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

1 Answers1

1

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.

siracusa
  • 13,411