This question already has an answer here:

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?


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?

marked as duplicate by Svend Tveskæg, Jesse, Heiko Oberdiek, Malipivo, Adam Liter Apr 17 '14 at 16:00

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • If you want spacing, you could do $A$ $B$. You dont need to have then in one inline math delimiter. – dustin Aug 21 '13 at 14:03
  • 1
    I'm not sure I understand what you mean. If I'm typesetting Math, is it possible for $A B$ to imply, for example, multiplication and get the extra spacing, but for $AB$ to denote a multi-letter variable and not get the extra spacing, without having to use $\mathit{AB}$? I'd prefer to get the spacing only when there is a space between the symbols. – sudosensei Aug 21 '13 at 14:06
  • In math mode, there is no spacing. That is $A B$ is the same as $AB$. If you want spacing, you either have to force a space with a slash or you can do $A$ $B$. – dustin Aug 21 '13 at 14:09
  • 1
    @sudosensei I recommend using \cs{letters}, where \cs should be a meaningful command name. Slightly less comfortable for typing, surely better to maintain information about your content. – egreg Aug 21 '13 at 15:32
  • 3
    Note it is not really spacing that is different between $NP$ and $\mathit{NP}$ It is a different font altogether and the fact that the spacing is larger is an effect of the of the math italic font being optimised for single letter variables whereas \mathit uses the text italic font which is designed for words, but as far as TeX is concerned no extra space is added between the letters. – David Carlisle Aug 21 '13 at 21:29

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


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


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.

  • Thank you, egreg. I'll mark this as the accepted answer in case someone is looking for a way to do this this without changing the default behaviour. – sudosensei Aug 22 '13 at 13:59

Here the solution for "egreg's exercise for the reader". The following example makes the letters "math active" with the following features:

  • The letters are collected until a non-letter is reached. Single letters are passed to macro \mprintsingle, multiple letters are passed to macro \mprintmulti. Both macros have two arguments. The first is intended to output the result in math mode, the second can be used for text mode.
  • Also the result is put into a subformula using \bgroup and \egroup. This allows putting multiple letters into indexes and subscripts without curly braces, e.g. F_strong.

Two applications:

  • The OP would use something like:

    \renewcommand*{\mprintsingle}[2]{#1}% the default

Usually multiple letters are text words and not variables and should be printed upright. This can be achieved by:

    \renewcommand*{\mprintsingle}[2]{#1}% the default

The full example file showing both applications:






    \expandafter\mathchardef\csname m@code@X\endcsname=\mathcode\count@
      \def\noexpand~{\m@start\csname m@code@X\endcsname X}%
      \ifx\@let@token X\else
% A-Z
% a-z


    \csname m@code@#1\endcsname





\textcolor{blue}{Single letters are set in blue},
\textcolor{red}{multiple letters in red}.
\item Multiple letters are put in \verb|\mathit|:
  \[ NP \neq N P \]

\item Multiple letters are put in \verb|\text|:
  \[ NP\ (text) \neq N P\ (two\ variables) \]
  \[ F_force = m_mass * a_acceleration \]


  • This is amazing. I always thought that this should have been the default behaviour in Math mode. But I guess Knuth knows better. ;-) I wish I could accept two answers... – sudosensei Aug 22 '13 at 13:57

Asked in comments to expand on my comment about fonts.

TeX does not control the space between letters in math mode, the apparent space appears because usually math italic fonts have wider sidebearings and inter-letter kerning (and often the characters themselves are wider) than a text italic.




\textit{NP ffiv} $NP ffiv$ $\mathit{NP ffiv}$



enter image description here

and in the log:

...\hbox(6.94444+1.94444)x345.0, glue set 224.80687fil
....\OT1/cmr/m/it/10 N
....\OT1/cmr/m/it/10 P
....\glue 3.57774 plus 1.53178 minus 1.02322
....\OT1/cmr/m/it/10 ^^N (ligature ffi)
....\OT1/cmr/m/it/10 v
....\kern 1.07637
....\glue 3.33333 plus 1.66666 minus 1.11111
....\OML/cmm/m/it/10 N
....\OML/cmm/m/it/10 P
....\OML/cmm/m/it/10 f
....\OML/cmm/m/it/10 f
....\OML/cmm/m/it/10 i
....\OML/cmm/m/it/10 v
....\glue 3.33333 plus 1.66666 minus 1.11111
.....\OT1/cmr/m/it/10 N
.....\OT1/cmr/m/it/10 P
.....\OT1/cmr/m/it/10 ^^N
.....\OT1/cmr/m/it/10 v
....\penalty 10000

Looking at the log you see that the third example uses the same font, .\OT1/cmr/m/it/10 as the first (this is a choice of the default computer modern font setup, other font packages may differ in whether they make \mathit use the same font as \textit or a font chosen to match the main math font).

The fact that there is kern of 1.09026pt between N and P (and the fact that there is no ffi ligature) in the middle example is not under the control of TeX (or visible from TeX macros except in luatex), it is a property of the font metrics. It is not just the kerning and sidebearings that differ, the letter shapes are different as well: most noticeable in v in this example. Of course if the font package changes the text fonts to be other than computer modern, but leaves the math fonts untouched, the difference between \mathit and \textit would be more pronounced.

  • This is really interesting. Thank you for taking the time to explain this, David. I guess the question is whether using $\mathit{NP}$ for multi-letter identifiers could ever cause typographical nightmares (inconsistencies etc.). – sudosensei Aug 22 '13 at 20:46
  • there's some understandable confusion on account of the naming of the fonts. "\mathit" is really the text italic font, cmi, while "math italic" is the font designed for use as math variables, cmmi. the widths of a single letter in cmmi is slightly wider than that of the same letter in cmi, on purpose so that, for example, the variable a in an italic theorem environment can be distinguished from the article a in the same text. (it's subtle, but possible; see the v in david's example.) this is unique to the computer modern fonts. – barbara beeton Feb 15 '15 at 3:29
  • Is it not rather the f that stands out? – Jost Jun 8 '16 at 12:53
  • @Jost not sure what you mean exactly but it is not so much the f itself as the lack of ff-ligatures that makes math italic ff look different to text italic ff – David Carlisle Jun 8 '16 at 13:03
  • Oh, you are right. I tested it by displaying the fs separately, i.e., $\mathit{f} \mathit{f}$ vs $\mathit{ff}. I always assumed the ligature only stretches the horizontal bar, but it actually also lengthens the 'head' of the f. – Jost Jun 8 '16 at 13:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.