这是一个有趣的问题,再次表明Maxima可能受益于更灵活的TeX输出系统,但我岔开了话题。这里有一个可能的解决方案。
这个方案使用模式匹配(在
defmatch
中)来识别进一步处理的分数表达式,并在匹配成功时提取分数的分子和分母。模式匹配器无法识别像
b/c^2
这样的内容--这可能应该被视为模式匹配器中的一个错误。
默认的TeX输出函数是一个名为TEX-MEXPT的Lisp函数。我编写了一行Lisp代码(诚然有点晦涩)来调用它。
/* tex_mexpt.mac -- look for quotient in exponent
* copyright 2021 by Robert Dodier
* I release this work under terms of the GNU General Public License
*/
matchdeclare (aa, all);
matchdeclare (bb, "#"(1));
defmatch (matches_quotient, aa/bb);
defmatch (matches_minus_quotient, -aa/bb);
:lisp (defun $tex_mexpt_default (e) (let (lop rop) (apply 'concatenate 'string (tex-mexpt e nil nil))))
my_tex_mexpt_quotient (base, expt_num, expt_denom) :=
if expt_num = 1
then printf(false, "\\sqrt[~a]{~a}", tex1 (expt_denom), tex1 (base))
else printf(false, "\\sqrt[~a]{~a}", tex1 (expt_denom), tex_mexpt_default (base^expt_num));
my_tex_mexpt_minus_quotient (base, expt_num, expt_denom) :=
if expt_num = 1
then printf(false, "\\sqrt[~a]{{1}\\over{~a}}", tex1 (expt_denom), tex1 (base))
else printf(false, "\\sqrt[~a]{~a}", tex1 (expt_denom), tex_mexpt_default (base^-expt_num));
my_tex_mexpt (e) :=
if tex_mexpt_look_for_quotient
then block ([base, expt],
[base, expt]: args(e),
if matches_quotient(expt) # false
then my_tex_mexpt_quotient (base, aa, bb)
elseif matches_minus_quotient(expt) # false
then my_tex_mexpt_minus_quotient (base, aa, bb)
else tex_mexpt_default (e))
else tex_mexpt_default (e);
/* examples */
stringdisp: true $
verbatim_and_equation (e) ::= printf (S, "\\begin{verbatim}~
S: openw ("/tmp/foo.tex");
printf (S, "\\documentclass{article}~
/* first without my_tex_mexpt at all */
verbatim_and_equation (a^(b/c));
verbatim_and_equation (a^-(b/c));
verbatim_and_equation (a^(-b/c));
verbatim_and_equation (a^(1/c));
verbatim_and_equation (a^-(1/c));
verbatim_and_equation (a^(-1/c));
verbatim_and_equation ((1 + (1 - x)^((y - z)/(y - w)))/((2*u - v)^(1/(n + 1))));
/* now enable my_tex_mexpt */
texput ("^", my_tex_mexpt);
tex_mexpt_look_for_quotient:true;
verbatim_and_equation (a^(b/c));
verbatim_and_equation (a^-(b/c));
verbatim_and_equation (a^(-b/c));
verbatim_and_equation (a^(1/c));
verbatim_and_equation (a^-(1/c));
verbatim_and_equation (a^(-1/c));
verbatim_and_equation ((1 + (1 - x)^((y - z)/(y - w)))/((2*u - v)^(1/(n + 1))));
/* verify disabled produces same output as originally */
tex_mexpt_look_for_quotient:false;
verbatim_and_equation (a^(b/c));
verbatim_and_equation (a^-(b/c));
verbatim_and_equation (a^(-b/c));
verbatim_and_equation (a^(1/c));
verbatim_and_equation (a^-(1/c));
verbatim_and_equation (a^(-1/c));
verbatim_and_equation ((1 + (1 - x)^((y - z)/(y - w)))/((2*u - v)^(1/(n + 1))));
printf (S, "\\end{document}~
close(S);
如您所见,我在其中放置了许多示例以验证输出结果。您可以通过 maxima --batch=foo.mac
或保存文件的名称执行它。它会在 /tmp/foo.tex
中生成输出。我使用 latex
处理它,然后用 xdvi
查看它。
记录一下,这是我得到的 foo.tex
输出。
\documentclass{article}
\begin{document}
\begin{verbatim}
a^(b/c)
\end{verbatim}
$$a^{{{b}\over{c}}}$$
\begin{verbatim}
a^-(b/c)
\end{verbatim}
$${{1}\over{a^{{{b}\over{c}}}}}$$
\begin{verbatim}
a^((-b)/c)
\end{verbatim}
$${{1}\over{a^{{{b}\over{c}}}}}$$
\begin{verbatim}
a^(1/c)
\end{verbatim}
$$a^{{{1}\over{c}}}$$
\begin{verbatim}
a^-(1/c)
\end{verbatim}
$${{1}\over{a^{{{1}\over{c}}}}}$$
\begin{verbatim}
a^((-1)/c)
\end{verbatim}
$${{1}\over{a^{{{1}\over{c}}}}}$$
\begin{verbatim}
(1+(1-x)^((y-z)/(y-w)))/(2*u-v)^(1/(n+1))
\end{verbatim}
$${{\left(1-x\right)^{{{y-z}\over{y-w}}}+1}\over{\left(2\,u-v\right)^{{{1}\over{n+1}}}}}$$
\begin{verbatim}
a^(b/c)
\end{verbatim}
$$\sqrt[c]{a^{b}}$$
\begin{verbatim}
a^-(b/c)
\end{verbatim}
$${{1}\over{\sqrt[c]{a^{b}}}}$$
\begin{verbatim}
a^((-b)/c)
\end{verbatim}
$${{1}\over{\sqrt[c]{a^{b}}}}$$
\begin{verbatim}
a^(1/c)
\end{verbatim}
$$\sqrt[c]{a}$$
\begin{verbatim}
a^-(1/c)
\end{verbatim}
$${{1}\over{\sqrt[c]{a}}}$$
\begin{verbatim}
a^((-1)/c)
\end{verbatim}
$${{1}\over{\sqrt[c]{a}}}$$
\begin{verbatim}
(1+(1-x)^((y-z)/(y-w)))/(2*u-v)^(1/(n+1))
\end{verbatim}
$${{\sqrt[y-w]{\left(1-x\right)^{y-z}}+1}\over{\sqrt[n+1]{2\,u-v}}}$$
\begin{verbatim}
a^(b/c)
\end{verbatim}
$$a^{{{b}\over{c}}}$$
\begin{verbatim}
a^-(b/c)
\end{verbatim}
$${{1}\over{a^{{{b}\over{c}}}}}$$
\begin{verbatim}
a^((-b)/c)
\end{verbatim}
$${{1}\over{a^{{{b}\over{c}}}}}$$
\begin{verbatim}
a^(1/c)
\end{verbatim}
$$a^{{{1}\over{c}}}$$
\begin{verbatim}
a^-(1/c)
\end{verbatim}
$${{1}\over{a^{{{1}\over{c}}}}}$$
\begin{verbatim}
a^((-1)/c)
\end{verbatim}
$${{1}\over{a^{{{1}\over{c}}}}}$$
\begin{verbatim}
(1+(1-x)^((y-z)/(y-w)))/(2*u-v)^(1/(n+1))
\end{verbatim}
$${{\left(1-x\right)^{{{y-z}\over{y-w}}}+1}\over{\left(2\,u-v\right)^{{{1}\over{n+1}}}}}$$
\end{document}