# How to automatically add \! in subscripts?

This code

\documentclass{article}
\usepackage{fouriernc}

\begin{document}
$a_1$

$a_{\!1}$
\end{document}


produces this output:

I like the spacing on the lower one much more.

Is there a way to automatically use \! on all subscripts so that I don't have to type it everytime? If this is not the optimal way to achieve this spacing, I'm eager to learn a better way, of course.

-

You can redefine the command _. Therefor you must change the catcode of the sign. The redefinition in the example should be done in the preamble. I did it in the document body to demonstrate the behaviour.

\documentclass{article}
\usepackage{fouriernc}

\begin{document}
$a_1$

$a_{\!1}$

\catcode\_\active
\def_#1{\sb{\!#1}}

$a_{1}$
\end{document}


Instead of changing the underscore global, you can set the mathcode of the token to redefine (suggest by David Carlisle and improved by egreg):

\documentclass{article}
\usepackage{fouriernc}

\begin{document}
$a_1$

$a_{\!1}$

\catcode\_=12
\mathcode\_="8000
\begingroup\lccode\~=\_
\lowercase{\endgroup\def~#1}{\sb{\!#1}}

$a_{1}$
$$1+1=1\label{eq_1}$$
Text \ref{eq_1} Text$_1$
\end{document}


However the best is to avoid _ in labels.

-
Thanks, but this produces errors when I use underscores in \label{}. How can I fix this? –  Foo Bar Apr 18 '13 at 9:05
@FooBar What exact errors you get? You might try to put the nasty code in the preamble so that the catcode is already \active at \begin{document} when the .aux file is read. –  yo' Apr 18 '13 at 15:23
@tohecz It is in the preamble and I get Missing \endcsname inserted in the .aux file on lines like \usedref{eq:label_with_underscore}`. –  Foo Bar Apr 18 '13 at 15:29
This does not work at all. Your new code does not change the spacing but also does not give any warning or error. –  Foo Bar Apr 18 '13 at 16:05
Thanks, this works now. :) –  Foo Bar Apr 18 '13 at 16:23