编写包含最大值表达式的 LaTeX 代码的文本文件

Writing a text file containing LaTeX code from maxima expressions

假设在 (wx)Maxima 会话中我有以下内容

f:sin(x);
df:diff(f,x);

现在我想让它输出一个包含类似内容的文本文件,例如

If $f(x)=\sin(x)$, then $f^\prime(x)=\cos(x)$.

我找到了 textex1 函数,但我认为我需要一些额外的字符串处理才能执行我想要的操作。

感谢任何帮助。

编辑:进一步说明。

Auto Multiple Choice is a software that helps you create and manage questionaires. To declare questions one may use LaTeX syntax. From AMC's documentation,题目是这样的:

\element{geographie}{
\begin{question}{Cameroon}
    Which is the capital city of Cameroon?
    \begin{choices}
    \correctchoice{Yaoundé}
    \wrongchoice{Douala}
    \wrongchoice{Abou-Dabi}
    \end{choices}
\end{question}
}

可以看出,就是LaTeX。现在,稍微修改一下,我可以把这个例子变成一道数学题

\element{derivatives}{
\begin{question}{trig_fun_diff_1}
    If $f(x)=\sin(x)$ then $f^\prime(0)$ is
    \begin{choices}
    \correctchoice{$}
    \wrongchoice{$-1$}
    \wrongchoice{[=12=]$}
    \end{choices}
\end{question}
}

这就是我想要的那种输出。比方说,我会有一个函数列表,然后执行一个循环来计算它们的导数等等。

嗯,tex是TeX输出函数。它可以通过texput(见)在某种程度上进行定制。

至于post-processing通过字符串操作,我不推荐。但是,如果您想走那条路,可以通过 load(sregex) 访问正则表达式函数。不幸的是,它还没有记录在案;有关示例,请参阅 sregex.lisp 的评论 header(在您的 Maxima 安装中的某处)。

好的,回答你更新的问题。我的建议是将问题和答案作为表达式来处理——首先构建问题列表,然后当列表具有所需的结构时,最后一步输出 TeX 文件。使用表达式通常比使用字符串更清晰、更简单。

例如这是一个简单的方法。我将使用 defstruct 定义一个结构,以便我可以按名称引用它的部分。

defstruct (question (name, datum, item, correct, incorrect));

myq1 : new (question);
myq1@name : "trig_fun_diff_1";
myq1@datum : f(x) = sin(x);
myq1@item : 'at ('diff (f(x), x), x = 0);
myq1@correct : 1;
myq1@incorrect : [0, -1];

你也可以这样写

myq1 : question ("trig_fun_diff_1", f(x) = sin(x), 
                 'at ('diff (f(x), x), x = 0), 1, [0, -1]);

不知道哪种形式更方便你

然后你可以做一个类似这样的输出函数:

tex_question (q, output_stream) :=
  (printf (output_stream, "\begin{question}{~a}~%", q@name),
   printf (output_stream, "If $~a$, then $~a$ is:~%", tex1 (q@datum), tex1 (q@item)),
   printf (output_stream, "\begin{choices}~%"),
   /* make a list comprising correct and incorrect here */
   /* shuffle the list (see random_permutation) */
   /* output each correct or incorrect here */
   printf (output_stream, "\end{choices}~%"),
   printf (output_stream, "\end{question}~%));

其中 output_streamopenw 返回的输出流(参见)。

可能需要尝试一些不同的东西才能让导数以您想要的格式输出。我的建议是将其逻辑放入输出函数中。

使用表达式的一个副作用是可以直接输出一些不同于 TeX 的表示形式(例如纯文本、XML、HTML)。这对您的项目可能重要,也可能不重要。