Recently I've been writing some work up in LaTeX. As I usually work in Mathematica, I found myself going back and forth between Mathematica and my Tex editor as I adjusted figures I was rendering with Mathematica code. I began to wonder if I could set up my notebook to turn itself into LaTeX, cutting out the middleman (me!).
To achieve this I've done two things, firstly I wrote a notebook that could select sections of itself and convert them to plaintext. I do this using various bits of Mathematicas front end interface. Notably the notebook saves and deletes output cells, this means one can write an input "hello " <> "world", and trust that it will be converted to output, without having to view that output, which might be repetitive or contain a lot of superfluous scaffolding. Find this notebook "Notebook to plaintext" is attached.
The second step was augmenting the above notebook to save the generated plaintext as a tex file, and automatically run pdfLatex.exe on the tex file to receive the ultimate output file. By writing Mathematica functions that export Mathematica plots, and return LaTeX that imports said plots, one can write a LaTeX document in Mathematica, the same environment that we specify our graphics. This notebook "Notebook to LaTex" is also attached, but one requires pdfLatex.exe, as is included in a MikTex installation for example. The pdf output of this example is also attached.
I had not previously worked with front end controls. As write time and run time are so close in Mathematica, we have fairly unique capacity to muck about with the structure of the work environment; automatically updating and evaluating cells. I hope this proves interesting.
This is very nice and indeed it is good to be able to work in one single environment. The way your notebook saves a figure in LaTeX format is impressive, but how about equations, especially numbered equations which are referred to in the text, and also references which are cited in the text? Because plain text (of course without figures) can be saved directly from a notebook, so no extra LaTeX generator is needed.
I tried to elaborate another (far less advanced) solution to this some time ago: made a style file which makes a notebook look like a latex file as much as possible. Then I do all work in the notebook without converting it into latex. When finally I want submit the the file for publication, I save it from the notebook as latex, then I have to do manual adjustments (e.g. reference list which I cannot make in my styled notebook, and figures also must be imported manually) and submit the final product.
I wrote a note on this in this discussion group three years ago, see:
Styled notebook with LaTeX appearance
Some of the problems discussed there have been solved through the discussion, and I implemented them in the style file. I attach it here.
The best would be to combine the functionality of your notebook with my style file, and include the capability of numbered referred equations (if they are already in your notebook, please let me know, I was unable to create equations with the notebook you supplied.
Is there some way to use your code, say from a palette, so that one does not have to copy the code into the notebook that you want to convert?
Yes, there is. You need to download the style file, and save locally on your computer. Then, if you want to reformat any existing (or new) notebook without copying the content, you choose Format -> Stylesheet -> Other
. In the file selection dialogue box which opens, you look up the file "latex-style.nb" (you can of course re-name it after your taste) and click on it. This will implement the style definitions into your notebook. You can use numbered and unnumbered equations in the style menu, and you can start and finish in-line formulas with Ctrl-9 and Ctrl-0 (at least on a Mac). The formulas should appear in Euclid italics, as in the sample file I had in my discussion entry.