[Tex/LaTex] Making math behaving like \mathit

fontsmath-mode

I often use descriptive names for predicates, terms and functions. However, due to the lack of kerning they don't look nice.

For example

$offer = surface \times force(now)$

renders as

enter image description here

Thus to avoid it use \mathit.

$\mathit{offer = surface \times force(now)}$

which renders as

enter image description here

which, arguably, looks better and it is easier to read.

To avoid to type \mathitm, I defined

\DeclareSymbolFont{italics}{\encodingdefault}{\rmdefault}{m}{it}

and

\DeclareMathSymbol{a}{\mathalpha}{italics}{`a}
...
\DeclareMathSymbol{z}{\mathalpha}{italics}{`z}
\DeclareMathSymbol{A}{\mathalpha}{italics}{`A}
...
\DeclareMathSymbol{z}{\mathalpha}{italics}{`Z}

Question(s)

Is this the "proper" way to do this?

What are the disadvantages/drawbacks of this approach? (I can use \mathnormal to go back to the standard behaviour, and I know that, for example, for the product of f and i I have to use f{}i to prevent the ligature).

Best Answer

It depends on several factors. If your math formulas are all built like that, then you might be justified in changing the mathcodes for the letters, although I recommend you not to do it.

Prefer a semantic markup: multiletter identifiers denote either variables or functions; define two commands, say \var and \func and type your formula as

\[
\var{offer}=\var{surface}\cdot\func{force}(\var{now})
\]

(don't use \times, please!). Now you have the freedom of choosing whatever representation for variables and functions you need, for instance

\newcommand{\var}[1]{\mathit{#1}}
\newcommand{\func}[1]{\mathit{#1}}

When your supervisor or a journal copy editor will tell you “Nice, but functions should be typeset in upright letters”, you'll answer “Wait a minute“, change the second line into

\newcommand{\func}[1]{\mathrm{#1}}

and recompile. Would it be the same with the change to the math codes?

Is it harder to type? I don't think so, particularly if you are an Emacs expert who's able to define a couple of shorthands.


Changing the math code of all letters is quite easy, as the code is repetitive. Don't forget to redeclare \mathit with \DeclareSymbolFontAlphabet in order not to waste a math family.

\DeclareSymbolFont{italics}{\encodingdefault}{\rmdefault}{m}{it}
\DeclareSymbolFontAlphabet{\mathit}{italics}

\makeatletter
\count@=`a \advance\count@\m@ne
\@whilenum{\count@<`z}\do{%
  \advance\count@\@ne
  \begingroup\lccode`A=\count@
  \lowercase{\endgroup
    \DeclareMathSymbol{A}{\mathalpha}{italics}{`A}%
  }%
}
\count@=`A \advance\count@\m@ne
\@whilenum{\count@<`Z}\do{%
  \advance\count@\@ne
  \begingroup\lccode`A=\count@
  \lowercase{\endgroup
    \DeclareMathSymbol{A}{\mathalpha}{italics}{`A}%
  }%
}
\makeatother

The loops can be simplified with expl3:

\usepackage{expl3}

\DeclareSymbolFont{italics}{\encodingdefault}{\rmdefault}{m}{it}
\DeclareSymbolFontAlphabet{\mathit}{italics}

\ExplSyntaxOn
\int_step_inline:nnnn { `A } { 1 } { `Z }
 {
  \group_begin:
  \char_set_lccode:nn { `A } { #1 }
  \char_set_lccode:nn { `B } { #1 + 32 }
  \tl_to_lowercase:n
   {
    \group_end:
    \DeclareMathSymbol{A}{\mathalpha}{italics}{`A}
    \DeclareMathSymbol{B}{\mathalpha}{italics}{`B}
   }
 }
\ExplSyntaxOff

The main problem, which requires using the \lowercase trick, is that it's the only way to generate a character token knowing its character code.

With a recent version of expl3 (released after 2015-09-09), one can avoid the \lowercase trick using \char_generate:nn.

\documentclass{article}
\usepackage{expl3}

\DeclareSymbolFont{italics}{\encodingdefault}{\rmdefault}{m}{it}
\DeclareSymbolFontAlphabet{\mathit}{italics}

\ExplSyntaxOn
\int_step_inline:nnnn { `A } { 1 } { `Z }
 {
  \exp_args:Nf \DeclareMathSymbol{\char_generate:nn{#1}{11}}{\mathalpha}{italics}{#1}
 }
\int_step_inline:nnnn { `a } { 1 } { `z }
 {
  \exp_args:Nf \DeclareMathSymbol{\char_generate:nn{#1}{11}}{\mathalpha}{italics}{#1}
 }
\ExplSyntaxOff

\begin{document}

\[
offer=surface\cdot force(now)
\]

\end{document}