2

I am a physics student and we have been told that indices that abbreviate some descriptive word or indices that are just to distinguish between say different objects like the mass of two objects A and B shouldn't be in italics in an equation. An example for that would be $m_\text{tot} = m_\text{A} + m_\text{B}$. For physical constants, or any non made up index for that matter, the indices should be in italics as the rest of the equation. Here an example would be the mass of an electron $m_e$.

Since basically every index is for things you make up, for almost every index I have to write out _\text{...}. This just clutters my equations so much that I tried to find a way to automate a switch into text mode after a _ character, but found nothing. So here I am asking you people: Do you know a way?

Thanks in advance for your answers.

EDIT: See also Is there a way to make math mode subscripts automatically non-italic?

Luguza
  • 23
  • Welcome to TeX.SE. – Mico Jan 16 '22 at 12:34
  • 2
    note that you should not use \text otherwise you will get the current font from before the math, eg italic in theorems. use m_{\mathrm{tot}} – David Carlisle Jan 16 '22 at 12:38
  • 3
    I would not redefine the math primitives, although that is possible just do \newcommand\z[1]{_{\mathrm{#1}} then you can use m\z{tot} which is only one character more than m_{tot} – David Carlisle Jan 16 '22 at 12:40

2 Answers2

4

You might use a different character to introduce textual subscripts, so you can keep _ to do its usual job.

I chose ? because it's unlikely to show up in math formulas.

\documentclass{article}
\usepackage{amsmath}

\newcommand{\textualsubscript}[1]{% _{\textnormal{\upshape#1}}% } \begingroup\lccode~=? \lowercase{\endgroup\let~}\textualsubscript \AtBeginDocument{\mathcode`?="8000 }

\begin{document}

$m?{tot}=m?{A}+m?{B}-m_{e}$

\end{document}

enter image description here

With a slightly different approach we can allow for nested subscripts (of course ? can never appear in such places).

\documentclass{article}
\usepackage{amsmath}

\ExplSyntaxOn

\tl_new:N \l__egreg_textualsb_arg_tl

\cs_new_protected:Nn \egreg_textualsb:n { \tl_set:Nn \l__egreg_textualsb_arg_tl { #1 } \tl_replace_all:Nnn \l__egreg_textualsb_arg_tl { ? } { __egreg_textualsb:n } __egreg_textualsb:V \l__egreg_textualsb_arg_tl } \char_set_active_eq:nN { `? } \egreg_textualsb:n

\cs_new_protected:Nn __egreg_textualsb:n { \ensuremath{\sb{\textnormal{\upshape#1}}}% } \cs_generate_variant:Nn __egreg_textualsb:n { V }

\AtBeginDocument { \char_set_mathcode:nn { `? } { "8000 } }

\ExplSyntaxOff

\begin{document}

$m?{tot}=m?{A?{téxt}}+m?{B}-m_{e}$

\end{document}

enter image description here

egreg
  • 1,121,712
  • Thanks a lot for the help. Since I'm new to LaTeX I don't know a lot about the things you can customize, but I'm eager to learn. Would you care to briefly explain what your code does? – Luguza Jan 16 '22 at 16:18
  • @Luguza This has been explained elsewhere in the site. Basically, \mathcode`@="8000 tells TeX to treat ? as a command \let equal to the one introduced beforehand with a known trick. Look for “math active”. – egreg Jan 16 '22 at 16:24
  • This works great for only one subscript, but with multiple levels of subscripts it doesn't work, for example: \begin{equation} A?{B?C} \end{equation} will just give me A subscripted with the text "B?C", not A subscripted with B subscripted with C.

    Could you edit your answer to also allow for this?

    – Luguza Jan 23 '23 at 12:03
  • 1
    @Luguza Added as requested – egreg Jan 23 '23 at 13:25
  • 1
    You might want to add a command \? which prints the original question mark. – Gaussler Jan 23 '23 at 16:40
  • Anyway, I think this “nested subscripts” business goes against the original logic of the syntax. The idea was that _ meant math mode subscript while ? meant text mode subscript. So m?{A?{téxt}} should really be m_{A?{téxt}}. – Gaussler Jan 23 '23 at 16:42
2

If you're willing and able to use LuaLaTeX to compile your document, the following solution may be of interest to you. The solution defines a Lua function, called sub2mathrm, which encases subscript terms in \mathrm wrappers if there is no whitespace between the _ (underscore) character and (a) material encased in curly braces -- e.g., {tot} -- or (b) a single alphabetic character -- e.g., A or B. Conversely, if there is some whitespace after the _ character, the Lua function does nothing. The Lua function does nothing with A_\sigma either, since the backslash character in \sigma is not an alphabetical character. sub2mathrm performs its work with the help of Lua's powerful string.gsub ("generalized substitution") function.

The Lua function is activated by executing \SubToMathrmOn, a utility macro that assigns sub2mathrm to the LuaTeX process_input_buffer callback. It is deactivated by running the utility macro \SubToMathrmOn.

enter image description here

% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{luacode} % for 'luacode' environment
\begin{luacode}
  function sub2mathrm ( s )
    s = s:gsub ( "_(%b{})" , "_{\\mathrm%1}" )
    s = s:gsub ( "_(%a)" , "_{\\mathrm{%1}}" )
    return s
  end
\end{luacode}

%% define two utility macros: \newcommand\SubToMathrmOn{\directlua{luatexbase.add_to_callback ( "process_input_buffer" , sub2mathrm , "sub2mathrm" )}} \newcommand\SubToMathrmOff{\directlua{luatexbase.remove_from_callback ( "process_input_buffer" , "sub2mathrm" )}}

\begin{document} \SubToMathrmOn % Activate the Lua function

$m_{tot} = m_A + m_B$ % no whitespace after "_"

$m_ {tot} = m _ A + m_ B$ % whitespace after "_"

\SubToMathrmOff % Deactivate the Lua function

$m_{tot} = m_A + m_B$

\end{document}

Mico
  • 506,678
  • It's really elegant, but I'm working in overleaf and I don't think I can use LuaLaTeX there. – Luguza Jan 24 '23 at 13:41
  • 1
    @Luguza - Not true. There's a pull-down menu from which you can choose pdfLaTeX, XeLaTeX, or LuaLaTeX. – Mico Jan 24 '23 at 14:01