答案1
这里有一个基于LuaLaTeX的解决方案,它利用了Lua的强大string.gsub
和string.sub
功能。
%%% Must be compiled under LuaLaTeX
\documentclass{article}
\usepackage{luacode} % for 'luacode' environment
\begin{luacode}
function nicemath ( s )
s = s:gsub ( "%(([%w%_]+)/([%w%_]+)%)","\\frac{%1}{%2}" )
s = s:gsub ( "(%a+)_(%a+)" , "%1_{%2}" )
s = s:gsub ( "%u+" , "\\mathrm{%0}" )
s = s:gsub ( "%*" , "\\cdot " )
s = s:gsub ( "%b()", function ( x ) return x:sub ( 2 , -2 ) end ) -- optional
tex.sprint ( s )
end
\end{luacode}
\newcommand\nicemath[1]{\directlua{nicemath(\luastringN{#1})}}
\begin{document}
\[
\nicemath{(1/SF) * (MDD_a) * (1/LDD_b) * (MBS_b) * (1/SSA_ab)}
\]
\end{document}
答案2
答案3
我首先使用精确且非常不寻常的答案将行内方程式转换为 TeX 代码(有任何包吗?)(无变化)至解释和翻译用户的输入。
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{listofitems,ifthen}
\def\QS[#1]{%
\if+\Qsep[#1]+\else%
\if-\Qsep[#1]-\else%
\if/\Qsep[#1]\over\else%
\if=\Qsep[#1]=\else%
\if^\Qsep[#1]^\else%
\if(\Qsep[#1]\bgroup\left(\else%
\if)\Qsep[#1]\right)\egroup\else%
\if[\Qsep[#1]\bgroup\else%
\if]\Qsep[#1]\egroup\else%
\if*\Qsep[#1]\cdot\else%
\if_\Qsep[#1]\expandafter\theund\else%
\csname \Qsep[#1]\endcsname\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi%
}%
\def\theund#1[#2]{_{\mathrm{#1[#2]}}}%
\setsepchar[@]{=@(||)||[||]@^@/||*@+||-@_@alpha||beta||pi||cos||sin||tan}
\makeatletter
\def\gQ[#1]{\edef\tmp{#1}\expandafter\g@addto@macro\expandafter\Z%
\expandafter{\expandafter\Q\expandafter[\tmp]}}
\def\gQS[#1]{\edef\tmp{#1}\expandafter\g@addto@macro\expandafter\Z%
\expandafter{\expandafter\QS\expandafter[\tmp]}}
\makeatother
\newcommand\interpreteq[1]{%
\def\Z{}%
\greadlist*\Q{#1}%
\presentQ%
\Z%
}
\newcounter{lindex}
\def\presentQ{% =
\setcounter{lindex}{0}%
\whiledo{\value{lindex}<\listlen\Q[]}{%
\stepcounter{lindex}%
\presentQA[\thelindex]%
\ifnum\value{lindex}<\listlen\Q[]\relax%
\gQS[\thelindex]%
\fi%
}%
}
\newcounter{lindexA}
\def\presentQA[#1]{% ()
\setcounter{lindexA}{0}%
\whiledo{\value{lindexA}<\listlen\Q[#1]}{%
\stepcounter{lindexA}%
\presentQB[#1,\thelindexA]%
\ifnum\value{lindexA}<\listlen\Q[#1]\relax%
\gQS[#1,\thelindexA]%
\fi%
}
}
\newcounter{lindexB}
\def\presentQB[#1]{% ^
\setcounter{lindexB}{0}%
\whiledo{\value{lindexB}<\listlen\Q[#1]}{%
\stepcounter{lindexB}%
\presentQC[#1,\thelindexB]%
\ifnum\value{lindexB}<\listlen\Q[#1]\relax%
\gQS[#1,\thelindexB]%
\fi%
}
}
\newcounter{lindexC}
\def\presentQC[#1]{% /*
\setcounter{lindexC}{0}%
\whiledo{\value{lindexC}<\listlen\Q[#1]}{%
\stepcounter{lindexC}%
\presentQD[#1,\thelindexC]%
\ifnum\value{lindexC}<\listlen\Q[#1]\relax%
\gQS[#1,\thelindexC]%
\fi%
}
}
\newcounter{lindexD}
\def\presentQD[#1]{% +-
\setcounter{lindexD}{0}%
\whiledo{\value{lindexD}<\listlen\Q[#1]}{%
\stepcounter{lindexD}%
\presentQE[#1,\thelindexD]%
\ifnum\value{lindexD}<\listlen\Q[#1]\relax%
\gQS[#1,\thelindexD]%
\fi%
}
}
\newcounter{lindexE}
\def\presentQE[#1]{% _
\setcounter{lindexE}{0}%
\whiledo{\value{lindexE}<\listlen\Q[#1]}{%
\stepcounter{lindexE}%
\presentQF[#1,\thelindexE]%
\ifnum\value{lindexE}<\listlen\Q[#1]\relax%
\gQS[#1,\thelindexE]%
\fi%
}
}
\newcounter{lindexF}
\def\presentQF[#1]{% alpha beta pi cos sin tan
\setcounter{lindexF}{0}%
\whiledo{\value{lindexF}<\listlen\Q[#1]}{%
\stepcounter{lindexF}%
\gQ[#1,\thelindexF]%
\ifnum\value{lindexF}<\listlen\Q[#1]\relax%
\gQS[#1,\thelindexF]%
\fi%
}
}
% THESE ARE THE REDEFITIIONS FOR TRANSLATION
\usepackage{environ}
\def\QSALT[#1]{%
\if+\Qsep[#1]+\else%
\if-\Qsep[#1]-\else%
\if/\Qsep[#1]\over\else%
\if=\Qsep[#1]=\else%
\if^\Qsep[#1]^\else%
\if(\Qsep[#1]{\left(\else%
\if)\Qsep[#1]\right)}\else%
\if[\Qsep[#1]{\else%
\if]\Qsep[#1]}\else%
\if*\Qsep[#1]\cdot\else%
\if_\Qsep[#1]\expandafter\theundALT\else%
\expandafter\noexpand\csname \Qsep[#1]\endcsname\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi%
}%
\def\theundALT#1[#2]{_{\noexpand\mathrm{#1[#2]}}}%
\makeatletter
\newcommand\translateeq[1]{%
\bgroup%
\let\QS\QSALT%
\def\Z{}%
\greadlist*\Q{#1}%
\presentQ%
\protected@edef\ZZ{\Z}
\par\medskip\noindent%
\parbox{\linewidth}{\detokenize\expandafter{\ZZ}}%
\par\medskip%
\egroup%
}
\makeatother
\NewEnviron{translateeqs}{\expandafter\nexteqn\BODY\par\relax}
\long\def\nexteqn#1\par#2\relax{%
\translateeq{#1}\ifx\relax#2\else\nexteqn#2\relax\fi%
}
\begin{document}
\textbf{INTERPRETATING EQUATIONS}
\[
\interpreteq{(1/SF) * (MDD\_a) * (1/LDD\_b) * (MBS\_b) * (1/SSA\_ab)}
\]
\textbf{TRANSLATING EQUATIONS}
\translateeq{(1/SF) * (MDD\_a) * (1/LDD\_b) * (MBS\_b) * (1/SSA\_ab)}
\end{document}
可以进行修改以反映不同的外观。
例如,如果在和\_
的定义中,重新定义为\interpreteq
\translateeq
\def\_{_}
并通过以下方式添加到解析列表中:
\setsepchar[@]{=@(||)||[||]@^@/||*@+||-@_||\_@alpha||beta||pi||cos||sin||tan}
那么结果会看起来更好:
如果 OP 确实想要\cdot
输出中的星号而不是,那么在and的定义中用\cdot
替换就足够了,例如:*
\QS
\QSALT
\def\QS[#1]{%
\if+\Qsep[#1]+\else%
\if-\Qsep[#1]-\else%
\if/\Qsep[#1]\over\else%
\if=\Qsep[#1]=\else%
\if^\Qsep[#1]^\else%
\if(\Qsep[#1]\bgroup\left(\else%
\if)\Qsep[#1]\right)\egroup\else%
\if[\Qsep[#1]\bgroup\else%
\if]\Qsep[#1]\egroup\else%
\if*\Qsep[#1]*\else%
\if_\Qsep[#1]\expandafter\theund\else%
\csname \Qsep[#1]\endcsname\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi%
}%