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.

  • 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 tex.stackexchange.com/q/9718/35864 – moewe Aug 8 '18 at 4: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 8 '18 at 4: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 8 '18 at 4: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 8 '18 at 4: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 8 '18 at 4:28
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.