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

Ingmar
  • 6,690
  • 5
  • 26
  • 47
  • 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 Jan 29 '22 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 Jan 29 '22 at 21:56
  • Are you willing and able to use LuaLaTeX to compile your document? – Mico Jan 29 '22 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? – David Carlisle Jan 29 '22 at 23:18

1 Answers1

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}
Mico
  • 506,678
  • Hi, i think that is what i want but does it work on overleaf ? it doesn't seem to understand the code you provided. – Bachir Hassaine Jan 30 '22 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 Jan 30 '22 at 11:38
  • wow nice, it works fine thank you – Bachir Hassaine Jan 30 '22 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. – Bachir Hassaine Jan 31 '22 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 Feb 01 '22 at 12:46
  • Ok that's what i mean, i don't want the font of the mathmode at all anywhere in the document. How do we remove math mode initiators and terminators as you suggest ? – Bachir Hassaine Feb 02 '22 at 19:59
  • hi i up the precedent message – Bachir Hassaine Feb 09 '22 at 19:51
  • @BachirHassaine - One generally uses the symbol $ to both initiate and terminate inline math mode; that's why writing $something$ renders the string "something" in inline math mode. If that's not what you want, i.e., if you want to render the string "something" in text mode, just omit the $ symbols, i.e., just write something. – Mico Feb 09 '22 at 19:56
  • i edited my original post to be clearer if u can have a look please @mico – Bachir Hassaine Feb 11 '22 at 18:05
  • @BachirHassaine - If you're still working with the LuaLaTeX-based solution, give something${}_{anything}^{anything}$ a try. – Mico Feb 11 '22 at 18:57
  • ok yes it works fine. Ok here's the thing if want to type the same thing but in a math environment lets say equation environment :

    \begin{equation} something{}_{anything}^{anything} \end{equation}

    it will display something in mathmode font again, how do you deal with it ?

    – Bachir Hassaine Feb 11 '22 at 23:02
  • @BachirHassaine - This site works best if each query revolves around a single topic. The topic in your original query concerned the typesetting of material in superscript and subscript positions. Your follow-up questions have brought up new topics. Please post a new query to ask about how to solve a new typesetting issue. – Mico Feb 12 '22 at 02:01
  • Hi, i created another post here https://tex.stackexchange.com/questions/635111/how-to-change-the-mathmode-font-v2 – Bachir Hassaine Feb 24 '22 at 15:46