[OS X TeX] Calculating in LaTeX

Joachim Kock kock at math.uqam.ca
Wed Aug 25 18:19:55 EDT 2004


> Is there any way in LaTeX to calculate certain cells of a table? 
> Maybe through a macro or script? Has anyone ever tried something 
> like this? I am not looking for an Excel replacement, just very 
> basic calculations like additions or conversions.

In my experience it is difficult to do any serious computations with TeX
itself --- that's not what it's made for.  I think it is better to do all
computations in the editor (eventually linked to a math programme), and
have it generate the latex code for you.

I like the notion of worksheets: these are plain text files in your editor,
where certain cells are linked to an interpreter, which could be the Tcl or
Lisp interpreter (in Alpha or Emacs) or an external math programme like 
Maple or Maxima.  It brings computational power directly into your tex 
document.

It works like this: a certain pattern defines a prompt, say

%%

If you type in this prompt and hit a trigger, the string you typed is sent
to evaluation, and the result is inserted in an 'output region' just below
it, cf. examples below.  So the 'calculator' reads what's in the %% line,
and produces an output, while TeX ignores the input instructions in the %%
line and reads only the output.

Here I describe my own worksheet extension to Alpha (downloadable from
http://www.cirget.uqam.ca/~kock/alpha-tcl/worksheet.tar.gz).  I know that
there exists something similar for Emacs.

I don't know what your need are exactly, perhaps my posting is completely 
off the point, but if you are interested and if you have some special 
needs, I will be happy to help you customising the worksheet interface in
Alpha.

Cheers,
Joachim.


------------------------------------------------------------

At the most primitive level, the latex document acquires a built-in
calculator.  We can write some text like this:

|This gives a total area of
|%% expr 230*31
|7130	 	
|square millimeters.

In this example, just after 'area of' we pressed <return>, and then invoked
the prompt by Ctrl-Y ctrl-Y, then wrote the Tcl calculation ([expr] is the
Tcl command for doing arithmetics), then pressed Ctrl-Y ctrl-Y again to
evaluate; the result was inserted on the line below, and the cursor on the
line below again, and we can continue writing.

This simple idea takes new dimensions when linked to a fancy 'calculator'
like Maple or Maxima.  With the Alpha interface to maxima, we might also
write something like this in our tex document:

|Exercise 1.  Factor the polynomial
|%% maximaTex expand ( (x+2*y)*(3*x-z) )
|$-2\,y\,z-x\,z+6\,x\,y+3\,x^2$		
|(using only pencil and paper).

Again, all it takes is a couple of Ctrl-Y ctrl-Y. In this example,
[maximaTex] is a proc that asks Maxima to evaluate and return the result 
as tex code.  Writing exercise sheets and exams is a piece of cake:

|Exercise 2.  Diagonalise the matrix
|%% set D [maxima { matrix( [3,0], [0,5] ) }]
|%% set S [maxima { matrix( [1,2], [0,1] ) }]
|%% maximaTex $S . $D . $S^^-1
|$\pmatrix{3&4\cr 0&5\cr }$	 	

(Maxima uses the dot operator for matrix multiplication.)

And nothing prevents you from editing the latex code by hand afterwards,
for example changing the single dollar to double dollar if that's better.
Note that the whole block of %% prefixed lines evaluates in one go ---
you don't have to press the evaluate trigger three times.  It is also
easy to go back in the input region and change one of the numbers if
necessary, and hit the trigger again.  This will replace the output region
with the new value.  

But how can the mechanism know what the output region is?  There are two
approaches, depending on a preference flag: one approach is heuristic: the
mechanism always appends a tab-space-tab sequence to the output, and
recognises output regions as lines with a traling "\t \t".  Such a trailing
sequence is not very common anywhere, so this method very seldom does any
dammage (in any case these insertions are undo-able, if necessary).  The
second approach goes to the cautious extreme: it inserts a date stamp and a
checksum.  In this way you can always see exactly when the output was
generated, and before replacing it the mechanism checks the region against
the stamped checksum to avoid overwriting something that had been manually
edited in the interim.  This is the preferred method in serious tex
documents or when dealing with complicated data, but it seems a bit
heavy if you are just using the worksheet as a sort of scratchpad for 
some computations.  Here is what the stamped output looks like:

|This week's controversy factor index is not less than
|%% set slope 2.31004
|%% set density 0.4823
|%% maximaTex (320 * $density / ($slope)^2 )
|%----------------------------------output written 2004/02/19 01:42:02 EDT----
|$28.92199455953416$
|%----------------------------------end of output------checksum:0x5BF39C42----


Worksheet mechanisms are also useful for code generation.  Define a little
procedure to produce some tex code and invoke it as above.  Something like:

|%% square a b c d
|\begin{diagram}a&\rTo&b\\ \dTo&&\dTo\\ c&\rTo&d\end{diagram}	 	

(The second line is generated by the first.)

This has certain advantages over defining macros in your editor, namely
that the command and the parameters that produced the latex code is
engraved in the document itself as explanation, and you can easily change
the parameters and reevaluate if needed.  It also has some advantages over
latex macros, namely that you can edit the result before running tex if
some customisation is needed.  When it comes to reusing complicated
diagrams or big drawings, this flexibility can be very handy.


Finally, worksheets are very useful when documenting code.  For example
in conjunction with the listings package:

|The first login will look like
|\begin{lstlisting}
|%% ssh -l newuser cvs.sourceforge.net
|The authenticity of host 'cvs.sourceforge.net can't be established.
|Are you sure you want to continue connecting (yes/no)? yes
|\end{lstlisting}



--------------------- Info ---------------------
Mac-TeX Website: http://www.esm.psu.edu/mac-tex/
           & FAQ: http://latex.yauh.de/faq/
TeX FAQ: http://www.tex.ac.uk/faq
List Post: <mailto:MacOSX-TeX at email.esm.psu.edu>





More information about the MacOSX-TeX mailing list