# Change spacing around operators in particular commands

I have a command to display subsequences, which I defined as follows:

\newcommand{\stx}[3] {\ensuremath{#1[#2\!:\!#3]}\xspace}


My initial intention was to render things like this:

The problem is that, in order to get the desired spacing around +, I have to write code like this:

\stx{s}{a\!+\!m}{b\!+\!n}


Otherwise the spacing around + is too large and the indices get unclear/unpleasant. But this problematic in case I want (and I probably will) change the definition of \stx.

I thought of changing the spacing around the operators with a command, like this

\newcommand{\tightBinOps}[1]   {\medmuskip=1mu\relax #1}
\newcommand{\stx}[3]   {\ensuremath{#1[\tightBinOps{#2}\!:\!\tightBinOps{#3}]}\xspace}


However, this has two problems:

1. It affects the whole document. I don't know how to restrict the spacing modification only to that specific expression;
2. I guess I shouldn't rely on the spacing between operators being \medmuskip or any other particular measure in every circumstance.

How could I safely write \tightBinOps—and, more importantly, should I write it or is there a better solution?

MCVE

\documentclass{article}

\newcommand{\tightBinOps}[1]   {\medmuskip=1mu\relax #1}
\newcommand{\stxOriginal}[3]   {#1[#2\!:\!#3]}
\newcommand{\stxModified}[3]   {#1[\tightBinOps{#2}\!:\!\tightBinOps{#3}]}

\begin{document}
Too much space in $a+1$ here: $\stxOriginal{s}{a}{a+1}$

Desired space in $a+1$ here: $\stxModified{s}{a}{a+1}$.

But this affects the + operator when I use it in the same math
entry as a subsequence:

Compare:

\noindent$1+1~\stxModified{s}{a}{a+1}$\\
$1+1~\stxOriginal{s}{a}{a+1}$
\end{document}

• you can effectively change the class of any operator or relation to "ordinary" simply by wrapping it in braces: {+} or {:}. much less complicated than trying to redefine the spacing. another approach is to use \mathord as shown in answers to this question: Correct spacing when using \sim as negation – barbara beeton Feb 8 '17 at 20:24
• While this didn't solve my original problem, it was a great tip! Seems to solve my issues when using comma as decimal separator---$1{,}25$, for instance gives the correct spacing, while $1,25$ does not. – giusti Feb 8 '17 at 23:08

First of all, give up with \ensuremath that adds nothing and, worse, breaks consistent markup: math should be treated as math. However, since only the value of the spacing parameters current at the end of the formula is used for the whole math list, you have to box the particular piece where you want different spacing.

In the following code, the automatic spaces around operation and relation symbols are set to \thinmuskip, which is small and not flexible.

\documentclass{article}

\makeatletter
\newcommand{\stx}[3]{\mathpalette\giusti@stx{{#1}{#2}{#3}}}
\newcommand{\giusti@stx}[2]{\giusti@stx@a{#1}#2}
\newcommand{\giusti@stx@a}[4]{%
\mbox{%
\medmuskip=\thinmuskip
\thickmuskip=\thinmuskip
$\m@th#1#2[{#3}:{#4}]$%
}%
}
\makeatother

\begin{document}

Example subsequence: $\stx{s}{a}{b}$

Example subsequence: $\stx{s}{a}{b+1}$

Example subsequence: $\stx{s}{a+m}{b+n}$

\end{document}


On the other hand, such a notation is cumbersome. You get the same output with

\documentclass{article}

\makeatletter
\newcommand{\stx}[1]{\mathpalette\giusti@stx{#1}}
\newcommand{\giusti@stx}[2]{%
\mbox{%
\medmuskip=\thinmuskip
\thickmuskip=\thinmuskip
$\m@th#1#2$%
}%
}
\makeatother

\begin{document}

Example subsequence: $\stx{s[a:b]}$

Example subsequence: $\stx{s[a:b+1]}$

Example subsequence: $\stx{s[a+m:b+n]}$

\end{document}


which is not more difficult to type and clearer.

If you type

Subscript: $A_{\stx{s}{a}{b}}$


for the first version, or

Subscript: $A_{\stx{s[a:b]}}$


for the second version, you get

• Thanks for the answer, but I'm not sure I understand what you're doing there. What I'm looking for is that a+m is tighter than a:b. I'm also curious why \ensuremath is wrong here. My notation of s[a:b] is that a:b are indices over s, so I'm using this command outside the math mode more frequently than in math mode. – giusti Feb 8 '17 at 21:51
• @giusti If you want tighter spacing around +, instead of \medmuskip=\thinmuskip do \medmuskip=1mu. But I'd not do it. Wherever you use \stx, it's math nonetheless. Add \ensuremath if you want; I still recommend not to. – egreg Feb 8 '17 at 21:54
• That sure works! But I'm actually interested in the reason why you wouldn't do that and why \ensuremath is wrong. Are you saying that the $..$ should always be explicit? – giusti Feb 8 '17 at 22:16
• @giusti Yes. No markup=bad. – egreg Feb 8 '17 at 22:21