It seems like it would make life easier, right?

\vec{x} vs $\vec{x}$

Could there be a way to say, force all, or most math commands to automatically ensure math? Maybe an environment?

I realize math expressions won't parse, but we just then use the old way of $ ... $. I'm talking about having to use $ ... $ all the time in normal text just to show a math symbol or something.

  • 1
    Somewhat related: tex.stackexchange.com/q/34830/134574 – Phelype Oleinik Aug 5 '18 at 4:02
  • Would a LuaLaTeX-based solution be of interest to you? – Mico Aug 5 '18 at 6:57
  • 8
    How do you distinguish between “a” as an article or a math variable? What about “α”? Is it a Greek text letter or is it a math symbol? Did you try \ensuremath{\alpha}+\ensuremath{\beta} versus $\alpha+\beta$? – egreg Aug 5 '18 at 8:59

(A comment up-front: I do not recommend pursuing the solution shown here. I think it's far, far better to switch in and out of inline math mode explicitly.)

Here's a LuaLaTeX-based solution. It can handle two types of cases:

  • math-mode macros that take one argument that must be (or at least should be) encased in curly braces, e.g., \vec{x} and \hat{y}); and

  • math-mode macros that do not take an argument, e.g., \alpha and \beta.

I don't think it makes sense to try to extend this setup to macros which take an argument that need not be encased by any kind of delimiter, e.g., \ln2, \ln 2, \sin \theta, or \sin\theta. There are just too many complications to contemplate. I don't think it's reasonable to expect users to write ln{2} or \sin{\theta}; after all, it would take just as many keystrokes to write $\ln2$ and $\sin\theta$ in the first place.

Do note that while this code can handle sentence fragments such as the letters \alpha\ and \beta, it's not meant to handle things like the expression \alpha+\beta is well defined. It also can't handle expressions such as a^2+b^2=c^2 properly; the spacing around the + and = symbols will not be the same as if the math material were were rendered math mode to begin with.

It's the user's job to populate the Lua tables called Table_A and Table_B with suitable math macros. Observe that it's necessary to double up on the backslash characters, i.e., one must provide inputs of the form \\vec and \\alpha. This is because the backslash character serves entirely different purposes in TeX and Lua. In particular, in order to get Lua to search for a single backslash character (\), it's necessary to input the character as \\.

enter image description here

% !TEX TS-program = lualatex

%% Store the Lua code in an external file called, say, "mymath.lua".
-- List of "functions" (macros, really) that should processed in math mode.
-- 'Table_A' is for macros that take one argument enclosed in curly braces.
Table_A = { "\\vec" , 
            "\\abs" ,
            "\\hat" ,
            "\\widehat" ,
            "\\dot" ,
            "\\tilde" ,
            "\\widetilde" ,
            "\\bar" , 
            "\\overline"  } -- as many items as needed    
-- 'Table_B' is for macros that do not take an argument.
Table_B = { "\\alpha" ,
            "\\beta" , 
            "\\omega"  } -- as many items as needed
-- The function 'mymath' does most of the work.
function mymath ( s )
   -- cycle over all items in the 2 Lua tables
   for i,j in ipairs ( Table_A ) do 
      s = s:gsub ( Table_A[i] .. "%s-%b{}" , "\\ensuremath{%0}" )
   for k,l in ipairs ( Table_B ) do 
      s = s:gsub ( Table_B[k] , "\\ensuremath{%0}" )
   return s
-- Assign the function 'mymath' to the 'process_input_buffer' callback.
luatexbase.add_to_callback ( "process_input_buffer", mymath , "mymath" )

\usepackage{mathtools} % loads the 'amsmath' package automatically
\DeclarePairedDelimiter{\abs}{\lvert}{\rvert} % create a new math-mode macro
% Load the Lua code from the external file:
\directlua{ require ("mymath.lua") }

\vec {x} or   \abs{ -1} or \hat { y } or \widetilde { W}.

The letters \alpha, \beta, etc through \omega.

%% Verifying that the same output results if $ symbols are used:
$\vec {x}$ or   $\abs{ -1}$ or $\hat { y }$ or $\widetilde { W}$.

The letters $\alpha$, $\beta$, etc through $\omega$.
  • This is not the way, this is what I would have done, I guess it is better than nothing but it requires one to create the list of math commands to wrap. While I suppose I could build the table over time, initially it would be a pain because I'd have to add every symbol I currently use. If there is a way to get a list of common symbols(probably a few thousand) and stick it in then it would work. Although, I'm not sure what the spaces are. – AbstractDissonance Aug 5 '18 at 9:54
  • 4
    @AbstractDissonance - The reason it's necessary to build a list (or two lists, actually) is that TeX, on its own, doesn't "know" if a given symbol -- a, or -, or whatever -- is a math symbol or not. About your claim that LuaLaTeX isn't needed: While it may not be needed, strictly speaking, it makes for a much easier setup than if one had to do a \renewcommand job on every macro of interest. – Mico Aug 5 '18 at 10:00
  • 2
    @AbstractDissonance - "If there is a way to get a list of common symbols (probably a few thousand)..." I'm definitely not familiar with any such list, at least not a list that's stored in a form that's easy to parse by machine. The "Comprehensive symbols list" mentions more than 14,000 [!] symbols; however, this list is definitely not stored in a way that's suitable for machine parsing. An even this "comprehensvie" symbol list would never be anywhere close to being complete... – Mico Aug 5 '18 at 10:16
  • 1
    The problem is that if it requires more work to create/update the list than using $$ then it is obviously not worth using. I'm glad you did come up with specific solution, though... at least it provides a means to an end. – AbstractDissonance Aug 5 '18 at 11:17
  • 1
    @AbstractDissonance - You commented, "I'm not sure what the spaces are." I assume you're referring to the %s- substring -- short for "zero or more space characters -- in the search string Table_A[i] .. "%s-%b{}". It's there to allow for input such as \vec {x}, which is every bit as syntactically valid as \vec{x} is, as far as TeX is concerned. (If you're still curious: %b{} stands for "something surrounded by matching curly braces".) – Mico Aug 5 '18 at 18:20

It is very easy to do that for \vec:


Now try this




A sum of vectors $\vec{x}+\vec{y}$

A sum of vectors \vec{x}+\vec{y}

The negative of a vector $-\vec{x}$

The negative of a vector -\vec{x}


enter image description here


These small examples show your idea is doomed to failure: math mode is much more that skewing letters.

Sometimes you have a single vector or math variable in text: using $...$ around it makes your intention clear. But the most usages of math symbols is in complex formulas, where you need $...$ anyhow. Being able to write

the vector \vec{x} is nice

instead of

the vector $\vec{x}$ is nice

doesn't seem so big an advance, particularly because you then are elicited to type

The sum of vectors \vec{x}+\vec{y} is even nicer
  • I should be able to be the one who decides if the documents I write look correct or not, a subjective thing, rather that someone else who has no possible way to know what I'm doing to even judge it properly. Instead of focusing on the visual differences between the two it would be nice if you focused on actually solving the original problem which is wrapping all math commands(or even most, I can think of one way using automation but I don't want to list all the math commands I might use). – AbstractDissonance Aug 5 '18 at 9:49
  • 10
    @AbstractDissonance No, just as you have to use proper type specifications in most programming languages. I answered your question about why and showed how to override the standard. If you want to go against the rules, just do it. Don’t blame LaTeX if the output is poor, though. – egreg Aug 5 '18 at 11:15

\ensuremath has not been provided to help users to typeset material without explicitly entering math mode (and thus saving 2 key strokes). It is available to ensure that certain things apear in math mode (because technally they have to) even though that is not necessarily a formula as far as the document is concerned.

For example


defines a text character but it requires to jump into math mode to build it. So if \textdegree is used as part of a formula using explicit $...$ inside the definition would break it.

In hindsight we should perhaps have provided that as @ensuremath to make it clear that this isn't really a document level command. But then, there are cases where users want to define similar things and so it ended up user-level.

But back to semantics. LaTeX is largely about writing a logically structured documents and as part of that it is important to clearly state where the formulas (math elements) are and where the text is. And even a single digit is (normally) a math object, though numbers can be text objects.

Now technically all will be fine if you define \alpha or \vec to jump into math mode if necessary and as you say, you can take care of it looking alright and avoid doing \alpha + \beta wothout surrounding it by $...$. But if you consider the LaTeX source file as something that carries meaning and is potentially processed by something other than a TeX engine then leaving out the markup for math objects is potentially a mistake in the long run.

As a small anecdote (though a slightly different scenario): Don Knuth made a remark somewhere that he got horribly tripped by optimizing his writing and doing things like ...the value can be either 1 or $-1$... in his books. That worked fine until the day he wrote Concrete Mathematics and decided to use Euler math fonts for math and Concrete Roman for text. Of course your case is different, i.e., if you change the definitions all is good as long as you stay within the TeX engine environment. But that may change one day and then it is just missing markup that changes the semantics.

  • 3
    "remark somewhere" is in don's article "typesetting concrete mahematics" in tugboat. – barbara beeton Aug 6 '18 at 17:47

You are forgetting that math mode carries semantic meaning. In math mode the symbol $x$ might denote a variable but in text mode is just the Latin letter x.

In ConTeXt you can type some mathematics in text mode but it might not result in what you were expecting.


\alpha \cdot \beta = \sum_{i=0}^N \alpha_i \beta_i

$\alpha \cdot \beta = \sum_{i=0}^N \alpha_i \beta_i$


enter image description here

  • 10
    @AbstractDissonance I don't see how TeX could be able to tell semantic meaning without the user invoking it. What I was trying to say was that if you type x how does TeX know that you mean the variable (to be printed in math italics) rather than the letter (to be printed upright). You have to give TeX this semantic hint by enclosing x in math mode. – Henri Menke Aug 5 '18 at 11:52
  • @AbstractDissonance: It is not just Henri which assumes that you are using it in formulas and expression, it is TeX itself which assumes that. This is basically the answer to your question "Why are not all math function wrapped in \ensuremath? Because TeX assumes that you are using them mostly in math mode! If you want to use some outside you just need to change to math mode for it. Also \ensuremath is much younger than the math functions. And, in general, you could use a nicer tone to other users which want to help and teach you. – Martin Scharrer Jan 11 '19 at 9:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.