4

I am using starfont to get astronomical symbols in my document. Unfortunately, the package defines the symbols for text mode only. From this answer, I was able to declare math versions of the symbols and came up with this MWE:

\documentclass{article}

\usepackage[amsthm, libertine, varbb]{newtxmath}
\usepackage{amsmath}
\usepackage{siunitx}
\usepackage[starfontserif]{starfont}

\DeclareSymbolFont{starfontsym}{OT1}{sts}{m}{n}
\DeclareMathSymbol{\mathSun}{\mathord}{starfontsym}{115}
\DeclareMathSymbol{\mathMercury}{\mathord}{starfontsym}{102}
\DeclareMathSymbol{\mathVenus}{\mathord}{starfontsym}{103}
\DeclareMathSymbol{\mathTerra}{\mathord}{starfontsym}{76}
\DeclareMathSymbol{\mathvarTerra}{\mathord}{starfontsym}{108}
\DeclareMathSymbol{\mathMoon}{\mathord}{starfontsym}{100}
\DeclareMathSymbol{\mathvarMoon}{\mathord}{starfontsym}{97}
\DeclareMathSymbol{\mathMars}{\mathord}{starfontsym}{104}
\DeclareMathSymbol{\mathJupiter}{\mathord}{starfontsym}{106}
\DeclareMathSymbol{\mathSaturn}{\mathord}{starfontsym}{83}
\DeclareMathSymbol{\mathUranus}{\mathord}{starfontsym}{70}
\DeclareMathSymbol{\mathvarUranus}{\mathord}{starfontsym}{65}
\DeclareMathSymbol{\mathNeptune}{\mathord}{starfontsym}{71}
\DeclareMathSymbol{\mathPluto}{\mathord}{starfontsym}{74}
\DeclareMathSymbol{\mathvarPluto}{\mathord}{starfontsym}{72}

\begin{document}
\begin{tabular}{rcc}
Sun & \Sun \\
Mercury & \Mercury \\
Venus & \Venus \\
Earth & \Terra & \varTerra \\
Moon & \Moon & \varMoon \\
Mars & \Mars \\
Jupiter & \Jupiter \\
Saturn & \Saturn \\
Uranus & \Uranus & \varUranus \\
Neptune & \Neptune \\
Pluto & \Pluto & \varPluto
\end{tabular}
\begin{align}
    & \mathSaturn : g_\mathSaturn, \quad \mathTerra : g_\mathTerra
\end{align}
\begin{align}
    &\frac{\Delta m_\mathSaturn}{m-m_\mathTerra} = \left( 1 - e^{-\frac{\Delta v_\mathSaturn}{I_{\mathrm{sp}}g_\mathTerra}} \right)
\end{align}
\[ M_{\mathSun} = \SI{1.989e33}{\gram} \qquad M_{\Sun} = \SI{1.989e33}{\gram} \]
\[ M_{\mathTerra} = \SI{5.9722e27}{\gram} \qquad M_{\Terra} = \SI{5.9722e27}{\gram} \]
\[ M_{\mathMoon} = \SI{7.342e25}{\gram} \qquad M_{\Moon} = \SI{7.342e25}{\gram} \]
\[ M_{\mathVenus} = \SI{4.8673e27}{\gram} \qquad M_{\Venus} = \SI{4.8673e27}{\gram} \]
\end{document}

which gives me:

I have two problems with this setup that I'd like to address:

  • Using the regular symbols in math mode does work, but produces the wrong symbols. It's very easy to type \Sun instead of \mathSun so I can see myself making this mistake multiple times. I'd prefer to get a warning or an error whenever I try to use the regular symbols in math mode to avoid this mistake.
  • Having two separate versions of the same symbol isn't nice. I'd prefer to have a single \Sun command which would produce the text version in text mode and the math version in math mode. Obviously, this would solve the first issue as well, and it would be a better experience to have a single command that adapts to the environment it's used in.

Is there a way to patch or modify those commands to work correctly in math mode? If not and I really must have two versions of each symbol, then is it at least possible to make the text versions issue a warning if used in math mode?

2 Answers 2

4

The (few) commands that latex defines to work in text and math work by defining separate commands \textfoo and \mathfoo then defining \foo by

\NewDocumentCommand\foo{}{\TextOrMath\textfoo\mathfoo}

so then \foo uses \textfoo in text mode and \mathfoo in math.

Using \NewDocumentCommand rather than \newcommand ensures it is a \protected robust command which will not do the in-math test at the wrong time, in section headings etc.

3

I'd simply redefine the commands to yield \text{\stchr<number>}

\documentclass{article}

\usepackage[amsthm, libertine, varbb]{newtxmath}
\usepackage{amsmath}
\usepackage{siunitx}
\usepackage[starfontserif]{starfont}

\ExplSyntaxOn
\tl_map_inline:nn
 {
  \Sun\Mercury\Terra\varTerra\Moon\varMoon\Mars\Jupiter
  \Saturn\Uranus\varUranus\Neptune\Pluto\varPluto
 }
 {
  \cs_set_protected:Npe #1 { \exp_not:N \text \exp_not:V #1 }
 }
\ExplSyntaxOff

\begin{document}

\begin{tabular}{rcc}
Sun & \Sun \\
Mercury & \Mercury \\
Venus & \Venus \\
Earth & \Terra & \varTerra \\
Moon & \Moon & \varMoon \\
Mars & \Mars \\
Jupiter & \Jupiter \\
Saturn & \Saturn \\
Uranus & \Uranus & \varUranus \\
Neptune & \Neptune \\
Pluto & \Pluto & \varPluto
\end{tabular}
\begin{align}
    & \Saturn : g_{\Saturn}, \quad \Terra : g_{\Terra}
\end{align}
\begin{align}
    &\frac{\Delta m_\Saturn}{m-m_{\Terra}} = 
    1 - e^{-\frac{\Delta v_{\Saturn}}{I_{\mathrm{sp}}g_{\Terra}}}
\end{align}
\[ M_{\Sun} = \SI{1.989e33}{\gram} \qquad M_{\Sun} = \SI{1.989e33}{\gram} \]
\[ M_{\Terra} = \SI{5.9722e27}{\gram} \qquad M_{\Terra} = \SI{5.9722e27}{\gram} \]
\[ M_{\Moon} = \SI{7.342e25}{\gram} \qquad M_{\Moon} = \SI{7.342e25}{\gram} \]
\[ M_{\Venus} = \SI{4.8673e27}{\gram} \qquad M_{\Venus} = \SI{4.8673e27}{\gram} \]

\end{document}

An equivalent, but longer to type, method would be to replace the code between \ExplSyntaxOn and \ExplSyntaxOff (inclusive) with

\begingroup
\def\redeclarestarsymbol#1{%
  \protected\xdef#1{\noexpand\text\unexpanded\expandafter{#1}}%
}
\redeclarestarsymbol\Sun
\redeclarestarsymbol\Mercury
\redeclarestarsymbol\Venus
\redeclarestarsymbol\Terra
\redeclarestarsymbol\varTerra
\redeclarestarsymbol\Moon
\redeclarestarsymbol\varMoon
\redeclarestarsymbol\Mars
\redeclarestarsymbol\Jupiter
\redeclarestarsymbol\Saturn
\redeclarestarsymbol\Uranus
\redeclarestarsymbol\varUranus
\redeclarestarsymbol\Neptune
\redeclarestarsymbol\Pluto
\redeclarestarsymbol\varPluto
\endgroup

Avoid leaving out braces around subscripts even if it appears to work (it does by chance).

enter image description here

2
  • 1
    Where am I using \rm in the example I provided?
    – Shai Avr
    Commented Jun 28 at 10:17
  • 1
    @ShaiAvr Oh, sorry, it was my fault with a “replace all”. I beg your pardon.
    – egreg
    Commented Jun 28 at 12:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .