[Tex/LaTex] Multiple letters without spacing in Math

amsmathmacrosmath-modespacing

Is there something I can add to the preamble of my document such that multiple letters without spacing in a Math environment (such as $NP$) do not have extra spacing between them and are not considered multiple distinct variables? Right now, I have to use $\mathit{NP}$ to prevent any extra spacing, but I feel that it is more natural to add a space between the letters if they are a different variable ($N P$).

For example, if I want the multi-letter variable NP, I want to be able to say $NP$ which would automatically be replaced by $\mathit{NP}$. If I want N and P to have the proper inter-variable spacing, I want to do that only when they are separated by a space, like so: $N P$.

Is there a way to achieve this? Would it break something that I'm unaware of?

Edit:

Here's a screenshot that shows what I mean:

enter image description here

So basically, is it possible for line 1 to automatically give me line 3 and for line 2 to give me line 2?

Best Answer

It's theoretically possible to make LaTeX recognize clusters of letters and typeset them as if they were input as argument to \mathit, but it would be deadly slow: each alphabetic character should be turned into a “math active” one, which checks whether the following item is an alphabetic character; if it is it should typeset itself, starting \mathit if it's the first one, and then pass the same control to the next character; if not followed by an alphabetic character it should end \mathit.

It's instead better to do something like

\newcommand{\mli}[1]{\mathit{#1}}

where \mli stands for MultiLetter Identifier; use any control sequence name. Then you'd input

$\mli{P}=\mli{NP}$

that has a good deal of advantages, but mainly keeps information about the input.

Note: I have a nice proof of the above statement, but unfortunately there are length limitations for posts on this site.


Heiko's solution in expl3.

\documentclass[fleqn]{article}
\usepackage{xcolor}
\usepackage{amsmath}
\usepackage{xparse}

\ExplSyntaxOn

% 1. Define an equivalent for each letter
\cs_new_protected:Nn \__egreg_letter_loop:nn
 {
  \int_step_inline:nnn { `#1 } { `#2 }
   {
    \exp_args:Nc \mathchardef 
     { __egreg_letter_code_##1: } % generate the old code
     =
     \mathcode##1 % the old code
     \scan_stop:
    \cs_new_protected:cx { __egreg_letter_act_##1: }
     {
      \exp_not:N \__egreg_letter_scan:nw { ##1 }
     }
    \char_set_active_eq:nc { ##1 } { __egreg_letter_act_##1: }
   }
 }
\__egreg_letter_loop:nn { A } { Z }
\__egreg_letter_loop:nn { a } { z }

% 2. Define the main macro that does the scanning
\tl_new:N \l__egreg_letter_scanned_tl

\cs_new_protected:Npn \__egreg_letter_scan:nw #1
 {
  \tl_if_empty:NT \l__egreg_letter_scanned_tl { \c_group_begin_token } % to be closed later
  \tl_put_right:Nx \l__egreg_letter_scanned_tl { \exp_not:c { __egreg_letter_code_#1: } }
  \peek_catcode:NF A
   {% next char is not a letter
    \__egreg_letter_deliver:V \l__egreg_letter_scanned_tl
   }
 }
\cs_new_protected:Nn \__egreg_letter_deliver:n
 {
  \tl_if_single:nTF { #1 }
   {
    \__egreg_letter_single:n { #1 }
   }
   {
    \__egreg_letter_group:n { #1 }
   }
  \c_group_end_token % finish the group
 }
\cs_generate_variant:Nn \__egreg_letter_deliver:n { V }

% 3. The interface for defining the actions
\NewDocumentCommand{\definelettersingle}{m}
 {
  \cs_set_protected:Nn \__egreg_letter_single:n { { #1 } }
 }
\NewDocumentCommand{\definelettergroup}{m}
 {
  \cs_set_protected:Nn \__egreg_letter_group:n { { #1 } }
 }

% 4. Make all letters math active
\int_step_inline:nnn { `A } { `Z } { \mathcode#1="8000 }
\int_step_inline:nnn { `a } { `z } { \mathcode#1="8000 }

\ExplSyntaxOff

% initialize

\definelettersingle{#1}
\definelettergroup{\mathit{#1}}

\begin{document}

First a standard formula $NP\ne N P$

\bigskip

Now \textcolor{blue}{single letters are set in blue},
\textcolor{red}{multiple letters in red}.
\begin{itemize}
\item Multiple letters are put in \verb|\mathit|:
  \definelettersingle{{\color{blue}#1}}%
  \definelettergroup{\mathit{\color{red}#1}}%
  \[ NP \neq N P \]

\item Multiple letters are put in \verb|\mathrm|:
  \definelettersingle{{\color{blue}#1}}%
  \definelettergroup{\mathrm{\color{red}#1}}%
  \[ NP\ (text) \neq N P\ (two\ variables) \]
  \[ F_force = m_mass a_acceleration \]
\end{itemize}
\end{document}

enter image description here

Related Question