为什么不是所有的数学函数都包含在ensuremath中?

为什么不是所有的数学函数都包含在ensuremath中?

这似乎会让生活变得更轻松,对吧?

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

有没有办法强制所有或大多数数学命令自动确保数学?也许是一个环境?

我知道数学表达式无法解析,但我们还是使用老方法。我说的是,在普通文本中,总是$ ... $使用来显示数学符号或类似的东西。$ ... $

答案1

(预先说明一下:我不建议采用这里所示的解决方案。我认为明确地切换内联数学模式要好得多。)

这是一个基于 LuaLaTeX 的解决方案。它可以处理两种类型的情况:

  • 数学模式宏采用一个必须(或至少应该)用花括号括起来的参数,例如\vec{x}\hat{y});和

  • 不接受参数的数学模式宏,例如\alpha\beta

我认为尝试将此设置扩展到采用不需要任何分隔符包围的参数的宏(例如,\ln2\ln 2\sin \theta或 )是没有意义的\sin\theta。有太多复杂因素需要考虑。我认为期望用户编写ln{2}或 是不合理的;毕竟,编写和需要\sin{\theta}同样多的击键次数。$\ln2$$\sin\theta$

请注意,虽然此代码可以处理诸如 之类的句子片段the letters \alpha\ and \beta,但它并不适用于处理 之类的内容the expression \alpha+\beta is well defined。它也无法正确处理诸如 之类的表达式;和符号a^2+b^2=c^2周围的间距将与数学材料一开始就以数学模式呈现时不同。+=

用户的工作是使用合适的数学宏填充 Lua 表(称为Table_A和 )Table_B。请注意,必须使用双倍的反斜杠字符,即必须提供\\vec和形式的输入\\alpha。这是因为反斜杠字符在 TeX 和 Lua 中的用途完全不同。特别是,为了让 Lua 搜索单身的反斜杠字符(\),必须输入该字符为\\

在此处输入图片描述

% !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
%% 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$.
\end{document}

答案2

对于以下情况来说,这非常容易做到\vec

\let\latexvec\vec
\renewcommand{\vec}[1]{\ensuremath{\latexvec{#1}}}

现在尝试一下

\documentclass{article}
\usepackage{amsmath}

\let\latexvec\vec
\renewcommand{\vec}[1]{\ensuremath{\latexvec{#1}}}

\begin{document}

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}

\end{document}

在此处输入图片描述

看?

这些小例子表明你的想法注定会失败:数学模式是很多不仅仅是歪斜的字母。

有时文本中只有一个向量或数学变量:使用$...$它可以使你的意图更清晰。但数学符号最常用的地方是在复杂的公式中,无论如何你都需要它$...$。能够写

the vector \vec{x} is nice

代替

the vector $\vec{x}$ is nice

看起来并不是很大的进步,特别是因为你随后被引导输入

The sum of vectors \vec{x}+\vec{y} is even nicer

答案3

\ensuremath尚未提供此功能来帮助用户在不明确进入数学模式的情况下排版材料(从而节省 2 次击键)。此功能可用于确保某些内容在数学模式中出现(因为从技术上讲它们必须出现),即使就文档而言,这不一定是公式。

例如

\ProvideTextCommandDefault{\textdegree}{\ensuremath{{^\circ}}}

定义文本字符,但需要跳转到数学模式才能构建它。因此,如果将 \textdegree 用作公式的一部分,则在定义中使用显式 $...$ 会破坏它。

事后看来,我们或许应该将其作为 @ensuremath 提供,以明确这实际上不是文档级命令。但是,有些情况下用户想要定义类似的东西,所以它最终是用户级的。

但回到语义上。LaTeX 主要是编写逻辑结构化的文档,其中很重要的一部分是清楚地说明公式(数学元素)的位置和文本的位置。即使一个数字(通常)也是数学对象,尽管数字可以是文本对象。

\alpha现在从技术上讲,如果您定义或\vec在必要时进入数学模式,那么一切都会很好,正如您所说,您可以照顾好它,看起来不错,避免\alpha + \beta在没有包围它的情况下做$...$。但是,如果您将 LaTeX 源文件视为具有意义的东西,并且可能由 TeX 引擎以外的其他东西处理,那么从长远来看,省略数学对象的标记可能是一个错误。

举个小例子(虽然情况略有不同):Don Knuth 曾在某处说过,他通过优化自己的写作并像...the value can be either 1 or $-1$...在书中那样做事而遭遇了严重的挫折。直到他编写了《具体数学》并决定使用欧拉数学字体来写数学题、使用 Concrete Roman 字体来写文本,这种做法才算成功。当然,您的情况有所不同,也就是说,如果您更改定义,只要您留在 TeX 引擎环境中,一切都会好起来。但这种情况可能有一天会改变,然后只是缺少标记,从而改变了语义。

答案4

您忘记了数学模式具有语义含义。在数学模式下,符号$x$可能表示变量,但在文本模式下,符号只是拉丁字母x


在 ConTeXt 中,您可以在文本模式下输入一些数学运算,但结果可能不会如您所愿。

\starttext

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

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

\stoptext

在此处输入图片描述

相关内容