将语句转换为数学表达式

将语句转换为数学表达式

有没有一种方法可以让我自动转换字符串(使用命令),例如:

(1/SF) * (MDD\_a) * (1/LDD\_b) * (MBS\_b) * (1/SSA\_ab)

变成如下数学表达式:

$(\frac{1}{SF}) * (MDD\_a) * (\frac{1}{LDD\_b}) * (MBS\_b) * (\frac{1}{SSA\_ab})$

那么看起来像:

在此处输入图片描述

我有几个上述形式的动态语句,我会自动将它们转换为数学表达式。

答案1

这里有一个基于LuaLaTeX的解决方案,它利用了Lua的强大string.gsubstring.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

当你将括号放置得稍微不同时,你可以使用 ConTeXt 的asciimath模块。

\usemodule[asciimath]

\starttext

\startformula
    \asciimath{1/(SF) * MDD_a * 1/(LDD_b) * MBS_b * 1/(SSA_(ab))}
\stopformula

\stoptext

在此处输入图片描述

答案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%
}%

在此处输入图片描述

相关内容