# Why aren't all math functions wrapped in ensuremath? [closed]

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.

• Somewhat related: tex.stackexchange.com/q/34830/134574 Aug 5, 2018 at 4:02
• Would a LuaLaTeX-based solution be of interest to you?
– Mico
Aug 5, 2018 at 6:57
• 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$? Aug 5, 2018 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 \\.

% !TEX TS-program = lualatex

\RequirePackage{filecontents}
%% Store the Lua code in an external file called, say, "mymath.lua".
\begin{filecontents*}{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}" )
end
for k,l in ipairs ( Table_B ) do
s = s:gsub ( Table_B[k] , "\\ensuremath{%0}" )
end
return s
end
-- Assign the function 'mymath' to the 'process_input_buffer' callback.
luatexbase.add_to_callback ( "process_input_buffer", mymath , "mymath" )

\end{filecontents*}

\documentclass{article}
\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") }

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

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

\bigskip

\stoptext


• @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. Aug 5, 2018 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. Jan 11, 2019 at 9:03