[Tex/LaTex] Change spacing around operators in particular commands

equationsspacing

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:

Example of subsequence rendering

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}

Best Answer

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}

enter image description here

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

enter image description here