# Replace all instances of * with \cdot only in mathmode

As i understand the most accepted form to write the multiplication symbol in LaTeX in \cdot. However i have a rather large document already with * as the multiplication operator. I would like to replace all instances of * with \cdot, but only in mathmode($Math$) and displaymathmode ($$display math$$). I would like to do this with future documents as well, as i think it improves readability of the code.

Is there a way to do this with xstring for example? Or any other way to do this for the whole document?

• Welcome to TeX.SX! As an unrelated note, you shouldn't be using double dollar signs for displayed math, see e.g. Why is $...$ preferable to $$...$$? – Torbjørn T. Jul 16 '15 at 8:11
• I'd say this is a job for your editor programme. Not LaTeX as you basically cannot ask LaTeX to first run the entire doc through xstring. Also remember that in many cases the multiplication dot can be ignored. – daleif Jul 16 '15 at 8:11
• Maybe it is possible to replace all occurrences using the find&replace functionality that almost every text editor provides. If your code is really well written, replacing the string  *  (with spaces!) might be sufficient, as this would not destroy starred commands. – JBantje Jul 16 '15 at 8:20

\cdot is defined by

\DeclareMathSymbol{\cdot}{\mathbin}{symbols}{"01}


So you just need to put this in your preamble

\DeclareMathSymbol{*}{\mathbin}{symbols}{"01}


If you insist on not changing the body of your source, you could make * math-active

\documentclass{article}
\mathcode\*="8000 %
{\catcode\*=\active\gdef*{\cdot}}
\begin{document}

Here we have some asterisks: * * * * *

Now we some some mathematics: $a*b*c$.

\end{document}

• David's answer is more elegant, of course :-) – Joseph Wright Jul 16 '15 at 8:28
• But your's should work with plain-tex too :) – ikrabbe Jul 16 '15 at 11:25
• @JosephWright could you explain the difference between your approach and the David's one? Thank you – sound wave Aug 13 '19 at 17:24
• @soundwave I've made * active and simply replaced it by \cdot in a math mode context. David has used the TeX math symbol machinery to do the replacement 'later'. – Joseph Wright Aug 13 '19 at 17:30
• @JosephWright thank you! so do the asterisks defined in the two ways behave in the same way? that is, by writing * in math mode the symbol \cdot is shown, while outside math mode by writing * it is shown the asterisk symbol – sound wave Aug 13 '19 at 17:38