# Adjusting subscript distance by redefining command

I use Fourier as the mathematics font, but I found the subscripts for some letters be much too far away from the letter itself so it just looks broken (see (1)). Better would be (2) or maybe even (3).

Now, I can do this by hand, but I would have to do it every time so I want to redefine the \sigma command to automatically put the subscripts closer.

Also, there might be superscripts, which shouldn't be moved too close (see (4)) but rather like in (5).

Is there a way to realize this?

    \documentclass[10pt]{book}

\usepackage{fourier}
\usepackage{amsmath}

\begin{document}
\begin{align*}
\underbrace{\sigma_{\!\!i} \sigma_{\!\!j}}_{(3)} \\
\underbrace{\sigma_{\!\!i}^2 \sigma_{\!\!j}^2}_{(5)} \\
\end{align*}
\end{document}


Without additional packages, you can redefine \sigma the following way

\makeatletter
\newcommand*{\closeindex}[1]{_{\mkern-4.5mu#1}}
\let\sigmareal\sigma
\DeclareRobustCommand{\sigma}{\sigmareal\@ifnextchar_{\expandafter\closeindex\@gobble}{}}
\makeatother


The \@ifnextchar checks if \sigma is followed by an underscore, which is then eaten away by \@gobble and the following token is passed to \closeindex. I used \DeclareRobustCommand to be able to use the command e.g. in figure captions.

Note that there are some cases which will break (but are to some extend also problematic with other solutions presented here):

• Superscript must follow subscript, i.e. \sigma_i^2 works, while \sigma^2_i doesn't.
• The underscore must follow explicitely, so \sigma\foo with \newcommand{\foo}{_i} is not treated.
• Grouping is a bit different from normal behavior. E.g. normally, \sigma_\mathrm{i} would work, but doesn't here, but \sigma_{\mathrm{i}} does. The problem is, that pure \mathrm is treated as the next token and put into the subscipt. This could be fixed halfway by evaluating \mathrm earlier by adding two more \expandafters before \closeindex, but \mathrm{i} does not expand into a group, so effectively a \protect ends up in the subsript and the i follows at full height. So suggestions welcome to get this part working...
• Thanks! This order of sub- and superscript makes sense (in many cases) anyway! The \sigma_{\mathrm{i}} problem is okay for the moment, but ideas are surely welcome. However, I like that your solution doesn't need additional parameters and is independent of a specific TeX engine! – seyfe Feb 10 '17 at 16:20

You may renew the \sigma command using an optional parameter for the underscript, not changing the way to put the superscript.

I've used etoolbox to test if the superscript is present or not:

\documentclass[10pt]{book}
\usepackage{fourier}
\usepackage{amsmath}
\usepackage{etoolbox}

\let\oldsigma\sigma
\renewcommand{\sigma}[1][]{%
\ifstrempty{#1}{%
\oldsigma
}{%
\oldsigma_{\!\!#1}
}}

\begin{document}
$\sigma \quad \sigma[i] \sigma[j] \quad \sigma[i]^2 \sigma[j]^2$
\end{document}


• Thanks for your answer! While I hoped for a possibility that would not require additional arguments to the command, this might be easiest solution without getting to deep into TeX (which, unfortunately, I don’t know how to, anyway). – seyfe Feb 10 '17 at 11:21
• Meanwhile a I solution was posted which I like even better, so I chose to accept the other one. Still, I appreciate your effort! – seyfe Feb 10 '17 at 16:33

Here's a preprocessor-based approach that runs under LuaLaTeX. The code below makes different spacing adjustments depending on whether or not "j" is in subscript position.

The only restriction on input syntax is that any superscript terms must be specified after, not before, the subscript term. Thus, \sigma_i^2 will be processed correctly, but \sigma^2_i will not.

% !TeX program = lualatex
\documentclass{book}
\usepackage{fourier,amsmath}

%>>>>>> add the following code to your preamble:
\usepackage{luacode}
\begin{luacode}
function change_sigma_subs ( s )
s = string.gsub ( s , "\\sigma%s-_%s-(%b{})" , "\\sigma_{\\mkern-4mu %1}" )
s = string.gsub ( s , "\\sigma%s-_%s-j"      , "\\sigma_{\\mkern-6mu j}"  )
s = string.gsub ( s , "\\sigma%s-_%s-(%a)"   , "\\sigma_{\\mkern-4mu %1}" )
return ( s )
end
\end{luacode}
"process_input_buffer", change_sigma_subs, "change_sigma_subs")}}
%<<<<<<<<

\begin{document}

$\sigma_{k} \sigma_{m} \quad \sigma_i \sigma_j \quad\sigma_{i-2}$

$A_{\sigma _ i \sigma _j}$ $A_{B_{\sigma_ i \sigma_j}}$

\bigskip
$\sigma_{k} \sigma_{m} \quad \sigma_i \sigma_j \quad\sigma_{i-2}$
$A_{\sigma _ i \sigma _j}$ $A_{B_{\sigma_ i \sigma_j}}$