7

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).

  • 2
    The typographically correct way to use words in equations is to print them in roman, i.e., \mathrm{offer} = \mathrm{surface}\times\mathrm{force}(\mathrm{now}). – ChrisS Dec 19 '14 at 6:36
  • @ChrisS Math variables, terms, functions and predicates are typically written in italics. Do you write $y=z \times f(z)$ or $\mathrm{y} = \mathrm{z} \times \mathrm{f}(\mathrm{z})$. – Guido Dec 19 '14 at 6:58
  • The proper way surely isn't changing codes for the letters. Use semantic markup: offer is the product of five quantities, \var{offer} can be the name of a variable. – egreg Dec 19 '14 at 7:29
  • 1
    If you do this you would need \mathnormal around every 1-letter variable not just using {} to break ligatures, it's a different font not just different kerning and ligature information. – David Carlisle Dec 19 '14 at 9:40
  • 1
    @Guido No I would disagree, as I commented in my answer, letters (in the absence of other markup) assumed to be denoting separate variables in TeX. That is why they are spaced out as your original image shows. It can in some contexts be useful to mark them up explicitly, especially if converting to other formats, but they shouldn't use the \mathit font in any case, so if you wanted \var{c} markup you would need a more complicated definition that checked on the length of the string and chose an appropriate font. – David Carlisle Dec 19 '14 at 12:01
11

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}
| improve this answer | |
5

The reason why the global reassigning the letters in math mode is not good idea was mentioned in another answer and comments. But if you really need this, then you can do:

\everymath{\it}\everydisplay{\it}

at beginning of the document. If you need to keep the numbers in roman style, then you can set:

\def\mathcodes#1{\mathcode`#1=\numexpr\mathcode`#1-"7000\relax 
   \ifx#10\else\expandafter\mathcodes\fi}
\mathcodes1234567890

This means, for example:

\documentclass[12pt]{article}
\everymath{\it}\everydisplay{\it}
\def\mathcodes#1{\mathcode`#1=\numexpr\mathcode`#1-"7000\relax 
   \ifx#10\else\expandafter\mathcodes\fi}
\mathcodes1234567890
\begin{document}

$offer = surface \times force(now) + 1$

\end{document}

If you are using plain TeX then you can use \itvariables command which is provided after \input ams-math or \input opmac. This command sets mathcodes of all letters to the text italic math family (as default). For example:

\input ams-math \itvariables

$offer = surface \times force(now) + 2$

\bye
| improve this answer | |
  • 1
    Class scrartcl Warning: Usage of deprecated font command `\it'! You should note, that in 1994 font command `\it' has been defined for compatiblitiy to Script 2.0 only. Now, after two decades of LaTeX2e and NFSS2, you shouldn't use such commands any longer and within KOMA-Script usage of `\it' is definitely deprecated. Try with memoir for another surprise. – egreg Dec 19 '14 at 8:34
  • 2
    My solution is working independent of the used format (LaTeX or plainTeX). I always prefer such independent solutions. I know that \it works in LaTeX too. If it is or isn't deprecated in LaTeX is not so important to me as the mentioned independence. – wipet Dec 19 '14 at 9:13
  • \it won't be supported by KOMA-script with one of the next versions. Why should it? It is deeprecated for 20 years and is only around for compatibility. But that is already said in the warning issued by KOMA. The point is, that the old (deprecated) font commands are provided by the class, not the format. – Johannes_B Dec 19 '14 at 9:18
  • 1
    I hope that if LaTeX users have such problems with \it then they are able to simply substitute this command to something more acceptable. This is only LaTeX problem. – wipet Dec 19 '14 at 9:22
  • \it isn't defined in the latex format, some classes notably the standard ones such as article may load a package to define it for compatibility reasons. – David Carlisle Dec 19 '14 at 9:27
4

I would agree with egreg that it is far better to make the markup match the meaning here and mark up each identifier separately. The math markup in TeX, as well as the default fonts, are designed on the idea that adjacent letters are separate variables, typically with implied multiplication or concatenation operators.

However if you are not convinced and want to default to the text italic font there is no need to allocate a new math font for this, it is already set up for use with \mathit you just need to make it escape the implied grouping that \mathit implies.

\documentclass{article}


\let\v\mathit
\begin{document}


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



$\v{offer} = \v{surface} \times \v{force}(\v{now})$%%%USE THIS

\everymath{\mathit{\xdef\tmp{\fam\the\fam\relax}}\tmp}


$offer = surface \times force(now)$

\end{document}
| improve this answer | |
  • The last one (\everymath) changes numerals to italics as well. – Guido Dec 19 '14 at 10:03
  • @Guido It does exactly the same as your suggested use of \mathit around the whole expression, yes. – David Carlisle Dec 19 '14 at 10:04
  • I was a bit sloppy:-) The idea was to wrap each "string" with \mathit (and indeed I use a shortcut for it). – Guido Dec 19 '14 at 10:09
  • 1
    @Guido you could do as you suggest resetting the matchcode of letters, but as noted here you can re-use the font slot assigned to \mathit (there are only 16 so allocating the same font to two slots is expensive) but I wouldn't do it, I have done the above with \mathsf where you want everything including digits to be in sans in presentations or very elementary worksheets – David Carlisle Dec 19 '14 at 10:13
  • Some packages use \everymath and \everydisplay for their highly respectable own purposes; thus it would be better not to overwrite them but just add to their contents. – user4686 Dec 19 '14 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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