0

I'm looking for a way to get the textfont (lmodern for my case) into mathmode as in 2) but without using \text{} each time I need to write text in math mode. How do we change the font of mathmode in such a way?

enter image description here

EDIT : I put an image to be more explicit, I hope you understand better what I mean. It's simple in fact, I don't want the standard mathmode font at all whether it is between $…$ or in an equation environment or anywhere else. I don't want to use \text{} command each time I need to write something on a line in math mode.

enter image description here

4
  • 1
    Welcome to the site! It’s not clear what the problem is, but your example appears to show the command you used to get output like you want. What problem are you having, what is your current code, and what output do you want instead?
    – Davislor
    Commented Jan 29, 2022 at 21:54
  • 2
    You ordinariiy would not use \text for this, as it inherits the formatting of the surrounding text, and therefore might show up italicized in a theorem statement. You would normally use \textup for the upright text font or \textit for the italic text font. Both are in the amstext package included as part of amsmath.
    – Davislor
    Commented Jan 29, 2022 at 21:56
  • Are you willing and able to use LuaLaTeX to compile your document?
    – Mico
    Commented Jan 29, 2022 at 22:18
  • 2
    your question is not at all clear, you should show some example code. \text is the standard command to get text in math, but here normally you would use \mathrm rather than text but in either case what is your actual problem? Commented Jan 29, 2022 at 23:18

1 Answer 1

3

If you're willing and able to use LuaLaTeX to compile your document, the following solution may (should?) be of interest to you. It defines a Lua function which, if activated, renders subscript and superscript terms with \mathrm if there is no whitespace between the _ and ^ characters and the sub/super-script arguments.

The solution also provides two utility macros, named \SubSupToMathrmOn and \SubSupToMathrmOff, respectively, to activate and deactivate the Lua function. By "activate", I mean "assign the Lua function to LuaTeX's process_input_buffer callback so that it functions as a preprocessor."

enter image description here

If you want to render a subscript or superscript term in upright letters without deactivating the Lua function, just make sure to leave whitespace immediately after the _ and ^ characters.

% !TEX TS-program = lualatex
% see also https://tex.stackexchange.com/a/630382/5001

\documentclass{article}
\usepackage{luacode} % for 'luacode' environment
\begin{luacode} 
  -- Define the Lua function that does all of the work:
  function subsup2mathrm ( s )
    s = s:gsub ( "_(%b{})"  , "_{\\mathrm%1}" )
    s = s:gsub ( "_(%a)"    , "_{\\mathrm{%1}}" )
    s = s:gsub ( "%^(%b{})" , "^{\\mathrm%1}" )
    s = s:gsub ( "%^(%a)"   , "^{\\mathrm{%1}}" )
    return s
  end
\end{luacode}

%% LaTeX utility macros to activate and deactivate the Lua function:
\newcommand\SubSupToMathrmOn{\directlua{luatexbase.add_to_callback ( 
   "process_input_buffer" , subsup2mathrm , "subsup2mathrm" )}}
\newcommand\SubSupToMathrmOff{\directlua{luatexbase.remove_from_callback ( 
   "process_input_buffer" , "subsup2mathrm" )}}

\begin{document}
$u_v^w$ $\mu_{something}^{anything}$ % Lua function isn't activated yet

\medskip
\SubSupToMathrmOn % now activate the Lua function
$u_v^w$ $\mu_{something} ^{anything}$ \quad $u_ v ^ w$
\end{document}
13
  • Hi, i think that is what i want but does it work on overleaf ? it doesn't seem to understand the code you provided. Commented Jan 30, 2022 at 11:29
  • 2
    @BachirHassaine - To use LuaLaTeX on Overleaf, click on the main menu selector in the top-left corner, scroll down to "Settings", click on the "Compiler", button, and select LuaLaTeX from the drop-down menu.
    – Mico
    Commented Jan 30, 2022 at 11:38
  • wow nice, it works fine thank you Commented Jan 30, 2022 at 13:27
  • Hello, It's great but how can we extend this function to text that is "normal" i.e not in index neither exponent. If i write $something$ in the code it will print it in mathmode, i want exactly what you did before but extended to "normal" writing. I'm sorry i'm not familiar with function in latex. Commented Jan 31, 2022 at 23:09
  • @BachirHassaine - I'm afraid I don't understand the objective of your follow-up question. If you don't want normal text to look like it was typeset in math mode, I suggest you simply remove the math mode initiators and terminators (which sound like they would be superfluous anyway). If that's not what you're looking to achieve, please post a new query and describe your use case in more detail.
    – Mico
    Commented Feb 1, 2022 at 12:46

You must log in to answer this question.

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