3

Suppose in a (wx)Maxima session I have the following

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

Now I want to have it output a text file containing something like, for example

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

I found the tex and tex1 functions but I think I need some additional string processing to be able to do what I want.

Any help appreciated.

EDIT: Further clarifications.

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, a question looks like this:

\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}
}

As can be seen, it is just LaTeX. Now, with a little modification, I can turn this example into a math question

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

This is the sort of output I want. I'll have, say, a list of functions then execute a loop calculating their derivatives and so on.

booNlatoT
  • 571
  • 3
  • 14
  • By the way, if you can be more specific about what you want to output, maybe I can try to help figure it out. – Robert Dodier Aug 25 '17 at 19:54
  • There's a software called [Auto Multiple Choice](http://project.auto-multiple-choice.net/projects/auto-multiple-choice/) that helps us generate questionaires. The idea is to use wxMaxima to batch-generate questions with their respective solutions and have it output files that can be used by AMC. – booNlatoT Sep 13 '17 at 14:40
  • OK, that makes sense. If you can be more specific about what you want the output to be, I can give some pointers. – Robert Dodier Sep 13 '17 at 16:48
  • Yes. I edited the OP. The goal is conceptually simple. – booNlatoT Sep 13 '17 at 17:08

2 Answers2

2

OK, in response to your updated question. My advice is to work with questions and answers as expressions -- build up your list of questions first, and then when you have the list in the structure that you want, then output the TeX file as the last step. It is generally much clearer and simpler to work with expressions than with strings.

E.g. Here is a simplistic approach. I'll use defstruct to define a structure so that I can refer to its parts by name.

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];

You can also write

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

I don't know which form is more convenient for you.

Then you can make an output function similar to this:

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}~%));

where output_stream is an output stream as returned by openw (which see).

It may take a little bit of trying different stuff to get derivatives to be output in just the format you want. My advice is to put the logic for that into the output function.

A side effect of working with expressions is that it is straightforward to output some representations other than TeX (e.g. plain text, XML, HTML). That might or might not become important for your project.

Robert Dodier
  • 16,905
  • 2
  • 31
  • 48
1

Well, tex is the TeX output function. It can be customized to some extent via texput (which see).

As to post-processing via string manipulation, I don't recommend it. However, if you want to go down that road, there are regex functions which you can access via load(sregex). Unfortunately it's not yet documented; see the comment header of sregex.lisp (somewhere in your Maxima installation) for examples.

Robert Dodier
  • 16,905
  • 2
  • 31
  • 48
  • Thanks. I'm back from vacation, so sorry for the late response. See my reply to your comment. That's what I want to do. – booNlatoT Sep 13 '17 at 14:41