答案1
如果你愿意并且能够使用LuaLaTeX编译文档时,以下解决方案可能(应该?)会引起您的兴趣。它定义了一个 Lua 函数,如果激活,则使用\mathrm
如果_
和^
字符以及下标/上标参数之间没有空格。
该解决方案还提供了两个实用程序宏,分别名为\SubSupToMathrmOn
和\SubSupToMathrmOff
,用于激活和停用 Lua 函数。我所说的“激活”是指“将 Lua 函数分配给 LuaTeX 的process_input_buffer
回调,使其充当预处理器。”
如果你想用直立字母呈现下标或上标术语没有停用 Lua 函数,只需确保在 _
和^
字符后立即留出空格。
% !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}