4

I have a macro to print (Center of Mass) \CM in text roman wherever the place in the book it is expanded. But when in subscript I want it much smaller than normal subscript size defaults to print it, so I have this:

\newcommand\CM{\ifmmode\textrm{\fontsize{3.5}{5}\selectfont CM}\else{{\textrm{CM}}}\fi}

but I wanted no \ifmathmode but I would like: \ifsubsupmode or any thing guessing the way the argument is going to be printed is in smaller size (super/sub script)

But I haven't found anything similar

juanfal
  • 181
  • Search the site for \mathchoice and use this in the definition of \CM. – yannisl Mar 19 '24 at 12:13
  • Of course there is, and not merely to guess but to know.

    Why not start by showing how \ifmathmode and \ifsubsupmode should be directly comparable?

    When you read the manual, how did it let you down? Where did it go wrong?

    – Robbie Goodwin Mar 23 '24 at 00:13

3 Answers3

4

I'd simply use \mathrm{CM}:

\documentclass[twocolumn]{article}
\usepackage{amsmath}

\newcommand{\CM}{\mathrm{CM}}

\begin{document}

\begin{gather} \CM_{\CM_{\CM}} \ \CM=x_{\CM}+y \ \CM=x^{}_{\CM}+y \end{gather}

\end{document}

enter image description here

Note: twocolumn is just to make a smaller picture.

You see that, in (3), the subscript is lowered. I'd go with (2), anyway.

If you really want to reduce the size in subscripts, you can.

\documentclass[twocolumn]{article}
\usepackage{amsmath}

\usepackage{unicode-math}

\makeatletter \newcommand{\CM}{{\mathpalette\CM@\relax}} \newcommand{\CM@}[2]{% \ifx#1\displaystyle\mathrm{CM}\else \ifx#1\textstyle\mathrm{CM}\else \ifx#1\scriptstyle\CM@@{\sf@size}\else \CM@@{\ssf@size}\fi\fi\fi } \newcommand{\CM@@}[1]{% \mbox{\fontsize{\fpeval{#1*0.8}}{0}$\m@th\mathrm{CM}$}% } \makeatletter

\begin{document}

\begin{gather} \CM_{\CM_{\CM}} \ \CM=x_{\CM}+y_{z_{\CM}} \end{gather}

\end{document}

enter image description here

egreg
  • 1,121,712
2

As said by Mico, use \mathrm in math mode.

And as mentioned by yannisl, mathchoice allows customization of the four math styles (\displaystyle, \textstyle, \scriptstyle and \scriptscriptstyle). You can force \scriptscriptstyle in place of \scriptstyle:

\documentclass{article}

\newcommand\CM{\mathchoice {\mathrm{CM}}% \displaystyle {\mathrm{CM}}% \textstyle {\scriptscriptstyle\mathrm{CM}}% \scriptstyle {\mathrm{CM}}% \scriptscriptstyle }

\begin{document}

\begin{displaymath} \displaystyle \CM \qquad \textstyle \CM \qquad \scriptstyle \CM \qquad \scriptscriptstyle \CM \end{displaymath}

\begin{displaymath} \CM_{{\CM}_{\CM}} \end{displaymath}

\end{document}

customization with mathchoice

jlab
  • 1,834
  • 1
  • 13
  • Thanks a lot. I've been trying but it is impossible for me to reduce the size of the subscript mathrm below scriptscript size, you need \text{} to allow \fontsize{}{}\selectfont there. I need a size below scriptscript size because CM is very large at scriptscript size, it has no so important role, it is simply a desperate sign (instead of \bar or \quote) and at scriptscript size looks larger and more important than the lowercase vowels like a in a_\CM. Finally (\mathchoice should have a 5 parameter for normal text!) : scripscript: \text{\fontsize{3}{4}\selectfont CM} – juanfal Mar 21 '24 at 08:54
  • @juanfal LaTeX has 4 math sizes. My answer is just a workaround to force \scripscriptsize instead of \scriptsize. If you want more flexibility look at egreg's answer (change the factor 0.8 to anything you want). – jlab Mar 21 '24 at 12:21
  • @juanfal Another notation for quantities that are relative to the center of mass is a star: $a^\ast$. – jlab Mar 21 '24 at 12:24
  • The problem of using ^\ast and other signs surrounding a letter is that you interfere with the exponents. An expressive subscript like _\CM is quicker to understand. Take into account, there is not a standard notation for this! – juanfal Mar 24 '24 at 13:10
  • The size of $a_{\scriptscriptstyle\textrm{CM}} is excessive in comparison with the size in \newcommand\CM{\text{\fontsize{4.75}{6}\selectfont CM}} and $a_\CM$ – juanfal Mar 24 '24 at 13:18
0

My final solution (thanks to your help!)

\documentclass[border=2pt]{standalone}

\newcommand\CM{\ifmmode{\mathchoice% {\textrm{CM}}% \displaystyle {\textrm{CM}}% \textstyle {\textrm{\fontsize{4.5}{6}\selectfont CM}}% \scriptstyle {\textrm{\fontsize{3}{4}\selectfont CM}}% \scriptscriptstyle }\else{\textrm{CM}}\fi}

\newcommand\wCM{\ifmmode\mathrm{CM}\else{CM}\fi}

\begin{document}

$A^\textrm{\fontsize{2}{3}\selectfont A}$ $A^\mathrm{\fontsize{2}{3}\selectfont A}$

\begin{tabular}{lll} Normal text: & a \wCM & a \CM \ Math text: & $a \wCM$ & $a \CM$ \ Disp. math: & $\displaystyle a\wCM$ & $\displaystyle a\CM$ \ Script: & $a_\wCM$ & $a_\CM$ \ Scriptscript: & $a_{\wCM_\wCM}$ & $a_{\CM_\CM}$ \ \end{tabular}

\end{document}

enter image description here

juanfal
  • 181
  • Finally I've needed to abandon the whole \mathchoice idea, and revert to the direct:\newcommand\CM{\text{\fontsize{4}{5.5}\selectfont CM}} since \caption{Resultados de $a_\CM$ y $\T{a}_\CM$ de esfera metálica sobre madera} is unable to tell it is being rendered in math mode /\ifmmode fails to apply there) and always renders the subscript in normal size :( – juanfal Mar 21 '24 at 10:45