Text FormattingReplacing * with \cdot

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
creative
Posts: 2
Joined: Tue Jul 21, 2015 1:17 pm

Replacing * with \cdot

Post by creative »

Hi guys,

i have no idea if this is the right place for this question, so if not pls move it for me.
what i generelly want is a script or something which is able to replace the "*"-character with a \cdot. But the script should only replace the * if i'm in mathmode and not in the normal text mode. What i already found on another board is this:
1:
2: \catcode '\*=\active
3: \def *{\cdot}
4:
5:
6: \begin{document}
7:
8: $a*b=c$
9:
10:
11: Ein Stern: $\ast$
12:
13: \end{document}

it actually already works perfectly, the only problem is that it replaces ANY *-character, although i only want those to be replaced that are written in mathmode. So what i need is an if-else-kinda thing, that can check if i'm in mathmode or not. Does anyone of you have an idea how i can realise this?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

Learn LaTeX easily with newest books:

The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis

The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more

LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis

User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Re: Replacing * with \cdot

Post by Johannes_B »

Hi, i strongly discourage that. Use the search and replace feature of your editor and check every single instance for sanity. Reason: Asterisks are sometimes used as superscripts to denote a special meaning, it is not always meant as a multiplication sign.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
creative
Posts: 2
Joined: Tue Jul 21, 2015 1:17 pm

Re: Replacing * with \cdot

Post by creative »

Well i am not going to use THIS exact script. but if i would add that switch that can recognise if i'm in mathmode it would be allright, wouldn't it? or is there any superscript use of the asterisks within the mathmode?
rais
Posts: 419
Joined: Sun Nov 16, 2014 8:51 pm

Replacing * with \cdot

Post by rais »

A test for math mode is simple: \ifmmode ... \else ... \fi
But making the asterisk active also breaks commands + environments, that use it already, such as \newcommand* or, speaking of math, the *-variants like align* (amsmath)...

KR
Rainer
Post Reply