Math-Intensive Reports with GNU Emacs and Calc, Part 2
Calc has the burden of recognizing the formulas you type into the editing buffer. It can recognize formulas in several formats. The simplest style is to put a formula by itself, preceded and followed by empty lines. This is the easiest to type but has the disadvantage that the layout may not be maintained. That is, if you choose to indent the formulas a bit (which makes them much easier to read, in my opinion), you will lose that indentation when you recalculate the report.
For general-purpose use, the style I prefer is to bracket each formula with a pair of dollar signs. Calc will find and (re)calculate such formulas but won't mess with your indentation. (This is the style I have used in this article.)
Calc also can recognize and generate markup with the TeX typesetting language. I use LaTeX, but I haven't used it extensively with Calc (yet), so my examples will be simple ones.
Calc can reformat its equations into a TeX markup style when you select a TeX display mode. For example, the Calc assignment
x := 3.2
is rendered in TeX mode as
x \gets 3.2
The TeX command \gets doesn't exist in native TeX. The intent is for you to define your own \gets command using a macro definition in your TeX document. It would look something like this:
\def\gets\text-that-typesets-an-assignment-operator
More complex math formulas will be rewritten in TeX mode using all the considerable number of TeX math operators. The math will come out gorgeous, as only TeX can make it.
Apparently, however, "some assembly is required" to work in this mode. I cannot say much more than this because I simply haven't much explored this aspect of Calc.
Of more interest to me, however, is that LaTeX environments are maintained within Calc. Consider the following markup for a simple LaTeX document. It repeats our first example:
\documentclass{article}
\begin{document}
Let's do a simple calculation of a right triangle. If the two
short legs are denoted a and b, where (say)
\begin{verbatim}
a := 3.1
\end{verbatim}
and
\begin{verbatim}
b := 4.1
\end{verbatim}
then the length of the hypotenuse is
\begin{verbatim}
c := sqrt(a^2 + b^2) => 5.14003891036
\end{verbatim}
I didn't have to get my calculator out to do this. I did it
right inside Emacs. Furthermore, this file is live.
\end{document}
In LaTeX, a verbatim environment is enclosed between the \begin{verbatim} and \end{verbatim} tags. When typeset, the text between the tags will be set in a constant-width Courier typeface---not too pretty, but serviceable. The important thing to understand is that LaTeX environments are recognized as math delimiters by Calc. This means that I can recalculate my LaTeX document as often as I need to and never (well, hardly ever) have to do any manual editing of the numerical results. It always is up to date and it always looks great.
Although this is not the place to explain it, Emacs also has a language mode for TeX and LaTeX editing. It's another elisp package called AucTeX. Try it, you'll love it; it also works with Calc.
Emacs benefits greatly from Calc, and your calculation reports will be executed faster and more accurately than you ever imagined they could be. The interaction between Calc and Emacs is almost as seamless (if I may be forgiven for using that awfully overused computer word) as it gets. Using Emacs with Calc is one of the most friction-free ways I have ever found to write engineering analysis reports and keep them current in the face of constantly ongoing design changes.
I know of no other free general-purpose text editor that can perform mathematical evaluation. Commercial packages are available, sure, but you have to pay plenty for them. Moreover, many products in the lower price ranges, such as Mathcad, are only available for Windows. The more expensive packages (Mathematica, Matlab) generally are available on both Windows and UNIX platforms but cost upwards of a thousand dollars. In contrast, Emacs and Calc are entirely free for the downloading.
What about other free software for doing calculations? I've had some experience with Octave, and I must say that I like it very much. Although it can be used interactively, it is not an editor like Emacs is, and in my opinion it is best for batch computation. After Emacs+Calc, it is the tool I would choose for computations that require an algorithmic approach (with lots of loops and logical testing of results). The only problem I had with it as a publication tool is that your input file requires #-style comment lines for documentation, á là C. To get its output in a form that is suitable for a report with interspersed commentary, you need to perform some downstream editing with tools like sed or awk. Not at all difficult on UNIX but not easy on Windows, and the additional processing requires more steps than the combination of Emacs+Calc. I prefer the latter, because I find it handles about eighty or ninety percent of my routine calculations.
A related category of free software is the literate programming approach ascribed to Professor Don Knuth. This originated as a way to embed sections of functioning code within a running commentary on its design. This procedure allows the code to be extracted from the report and rearranged as needed for compilation. I'm not expert in this area, but I am convinced that Octave, for one, would benefit a lot from being used in a literate manner from within the body of a report. Where the literate programming approach fails, for my purposes at least, is that it seems not to have been considered as a way to compute mathematical expressions and numbers for immediate insertions into the report itself.
Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
| Trying to Tame the Tablet | May 08, 2013 |
- RSS Feeds
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Using Salt Stack and Vagrant for Drupal Development
- New Products
- Validate an E-Mail Address with PHP, the Right Way
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- Tech Tip: Really Simple HTTP Server with Python
- Home, My Backup Data Center
- Download the Free Red Hat White Paper "Using an Open Source Framework to Catch the Bad Guy"
- Android is Linux -- why no better inter-operation
24 min 44 sec ago - Connecting Android device to desktop Linux via USB
53 min 14 sec ago - Find new cell phone and tablet pc
1 hour 51 min ago - Epistle
3 hours 20 min ago - Automatically updating Guest Additions
4 hours 28 min ago - I like your topic on android
5 hours 15 min ago - Reply to comment | Linux Journal
5 hours 36 min ago - This is the easiest tutorial
11 hours 50 min ago - Ahh, the Koolaid.
17 hours 29 min ago - git-annex assistant
23 hours 29 min ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Free Webinar: Linux Backup and Recovery
Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.
In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.



Comments
Re: Math-Intensive Reports with GNU Emacs and Calc, Part 2
The author seemingly does not have worked extensively with calc. calc-2.02f does not work too well with Emacs-21. There are a lot of problems with symbolic simplifications, and some quirks with regard to input, scrolling and display.
For that reason, Emacs-21 users should, if at all, get calc directly from the Emacs CVS archive at Savannah.gnu.org where it is maintained as part of the next new-feature release of Emacs (probably 21.4). That version works quite better.
It is a pity that calc's author Dave Gillespie has stopped working on calc while still having a significantly faster (calc-2 does not even use Emacs' floating point variables!) and more extensive version of calc up his sleeves and already in use privately. If anybody manages to wheedle out those floppy disks or whatever else he has been storing this work on...
Re: Math-Intensive Reports with GNU Emacs and Calc, Part 2
You're correct, in that at the time I wrote the article I was quite inexperienced with Calc. Yes, I had noticed some problems on the symbolic side when using 2.02f with Emacs-21 (my setup). I thought it might be me, but you have confirmed the existence of a general problem. Thanks! And I also thank you for the pointer to the CVS version--and for the intelligence about Dave Gillespie's newer version. I, too, hope he takes steps to release it to the public.
But my article's emphasis was mostly on straight-ahead numerical evaluation, as that accounts for 95% or more of my typical usage. My setup works quite well for that, and I would advise anybody whose patterns of usage resembles mine not to be put off too much by the issues you quite correctly raise in your letter.
Thanks for the comments.
Charles Hethcoat, author.
Re: Math-Intensive Reports with GNU Emacs and Calc, Part 2
The auther writes:
"The only problem I had with it as a publication tool is that your input file requires #-style comment lines for documentation,
Re: Math-Intensive Reports with GNU Emacs and Calc, Part 2
Hi, everyone. I didn't realize until today that my article was already on-line! I appreciate the nice feedback.
Concerning the syntax of Calc: I repeat, I'm still fairly new to it, but my reading of the manual leads me to believe that it's probably configurable within elisp, but I haven't tried to figure out how. For my purposes, the `=>' is fine. In any event, you certainly can post-process these marks to taste within or without Emacs.
The only thing to keep in mind is whether you may need to re-process the file with Calc later on. (Remember my discussion of design changes?) You don't want to leave your master file damaged with respect to Calc's ability to read and process the formulas.
So if you create a filter to fix up the syntax, try to create the corresponding "unfilter" as well. Better still, create a processing pipeline to first /copy/ your file to another name and work on that one. Now your original remains untouched. When the need arises to update your original, just rerun your pipeline.
Charles Hethcoat
Re: Math-Intensive Reports with GNU Emacs and Calc, Part 2
You're right, Emacs certainly can auto-convert those comments. One would need to use something like:
M-x replace-regexp ^# RET RET
to remove all #s at the starts of words, and then
M-x replace-regexp ^[^$] RET # SPACE RET
to put them back again. (This assumes all lines start either with a Calc $ or a comment #, which may be incorrect - I haven't used Calc myself.)
But the author was referring to Octave, not to Emacs+Calc. He wrote:
"you need to perform some downstream editing with tools like sed or awk. Not at all difficult on UNIX but not easy on Windows, and the additional processing requires more steps than the combination of Emacs+Calc."
So the fact that it needs processing that could be easily done in Emacs may be precisely his point...
calc math syntax constraint
Thanks a lot. Very interesting article.
One question. Is there a way to get free of the calc package syntax constraint? I mean that in my report, i want to use the '=' caracter for assignments as well as for 'evaluates to' operations. For example, I want to see things like:
c = sqrt( a^2 + b^2 ) = 5.14003891036
and NOT:
c := sqrt(a^2 + b^2) => 5.14003891036
Is it possible, or are we stuck with one and only one syntax?
Re: calc math syntax constraint
Ok, done a bit of reading and the following should work
M-x replace-regexp RET:=|=>RET=RET
Do what you need to do to print the file then Ctrl-_ will set things back to how they were .
Hope that's of help
Euan
Re: calc math syntax constraint
BAH! Bloody stripped the backslash!
M-x replace-regexp RET :=BACKSLASH|=>RET=RET
Re: calc math syntax constraint
One possible solution is to run a regex-replace on the document prior to printing. I'm fairly new to Emacs so not too sure of the syntax for regex. In Vi it would look something like this:
1,$ s/=>|:=/=/g
Which tranlates to search the whole file for => or := and replace with =.
Once you've published it you can then hit Ctrl_ to undo the regex.
If anyone's read this and knows how to do it in Emacs regex then please post :-)
Re: calc math syntax constraint
oops, regex missing a backslash. Should read:
1,$ s/=>|:=/=/g
Sorry,
Euan
Re: calc math syntax constraint
OK, for reasons unknown the backlsash gets stripped when I post. Here we go once more:
1,$ s/=>BACKSLASH|:=/=/g
>sigh<
Re: calc math syntax constraint
Not that i know of... This syntax is used as a programming language one, rather than a symbolic one. And, as such, it has been elaborated since the early days of Pascal...