4

A short example code will show that the answer to this question is yes. The example code (which I give in my answer) is for a simple LaTeX editor that is almost a drop-in replacement for TeXworks. The only(?) editing features it lacks are syntax coloring, delimiter balancing, and being able to sync cursor positions between source and preview.

The value of this simple LaTeX editor is primarily conceptual. It is, in my opinion, the best starting point for thinking about and discussing a much more difficult question: How best to get LaTeX and Mathematica working easily together for the average user?

That this question was not answered at least 15 years ago, is I suppose, some indication of its difficulty. But now we can rephrase it as a more structured question that is much easier to answer: What editing features would we like to add to this simple LaTeX editor?

Of course, our wish-list should include features like generating LaTeX code from Mathematica output, well beyond what a "normal" LaTeX editor can do.

Disclosure statement: I am the author of a commercial package, EnableTeX, that implements most of my own wish-list of features for such an editor. I am using this post to announce the release of my package, to share my ideas, and to ask the following question which is perhaps too broad and open ended to be asked as a main question:

How is Mathematica currently being used to generate LaTeX documents, or parts of documents? To clarify: I'm interested not only in functions and packages that have been written for this purpose, but also the general workflows that have been adopted.

Andrew Norton
  • 847
  • 4
  • 13

1 Answers1

7

I am the author of a commercial product called EnableTeX, which I've just released. The question in the title is what I asked myself several years ago while struggling to publish research that used a lot of Mathematica computation. That Qn eventually led to here.

To answer the question in the title, I created a new cell style. The new cell style was similar to Text, in that text could be edited within the new cell. However, the new style specified a CellEvaluationFunction for the cell. Let's call it f. This means that when the cell is evaluated, it returns the value f[str], where str is the string contents of the cell.

I knew then that the definitive answer to my question is YES! My reasoning was as follows: If f[str] returns Null then the new cell will not only look like a Text cell, it will also print no Output cell in the notebook (also like a Text cell). Instead of printing output, f[str] could write str to a file, have it processed by pdflatex, and then the resulting PDF file could be displayed by an auto-reloading PDF viewer.

A notebook that contained just one such cell would be equivalent to something like TeXworks, and would already have built-in spell-check, find-and-replace, and a host of keyboard shortcuts. All this from just a single cell style definition and one 3 line function.

Here is a complete working code for the One-Cell-Editor. The style definition needs to be saved in a stylesheet and the definition for the function OneCellEditor (which is called f in the above) needs to be evaluated in the notebook:

   Cell[StyleData["One-Cell-Editor"],
   Evaluatable->True,
   CellEvaluationFunction->OneCellEditor,
   MenuSortingValue->900]

   OneCellEditor[str_, TextForm] := 
              (WriteString["demo.tex", str];  
               Close["demo.tex"];  
               RunProcess[{"pdflatex", "demo.tex"}];)  

To get it going, insert a One-Cell-Editor cell into the notebook (it will be at the top of the cell style menu because of the MenuSortingValue that has been used), and open an auto-reloading PDF viewer for demo.pdf. Whenever the cell is evaluated, the LaTeX document it contains will be displayed. Note: Open notebooks have to be re-opened before a changed style file will take effect.

Of course, Mathematica allows for a few improvements to the One-Cell-Editor. A multi-cell version of the editor can offer built-in notebook features such as notebook headings and collapsible sections that surpass those of many LaTeX editors. If one also factors in the potential for interleaved Mathematica calculations, and the possibility of having selected results automatically translated to LaTeX and written to file, then the possibilities become quite awesome.

The ideas that I've found useful to implement and the way that I use Mathematica to generate LaTeX documents are described in the EnableTeX user guide, which can be download here: EnableTeX-UserGuide.pdf.

Licenses for EnableTeX are not free and the code may not be distributed. Nevertheless, licensees have access to the source code, and are allowed to modify their copies of the code if they want to.

Andrew Norton
  • 847
  • 4
  • 13
  • You user guide link doesn’t work for me. – Chris K Jul 12 '19 at 15:04
  • @ChrisK I've edited the link for the user guide to go directly to the PDF rather than the home page. Thanks. – Andrew Norton Jul 12 '19 at 16:18
  • 3
    I wouldnt mind this answer if it was just someone sharing their experience and solution to a difficult problem..and providing their solution to the community for free...Not selling it. Not sure S.E. Is supposed to be used as an advertisement platform for a paid product... – DrMrstheMonarch Jul 12 '19 at 16:54
  • @morbo 6 months ago I almost did release it for free. I'd already been using it for a year or so and I figured other people would like it too. But I knew that with no documentation, no tutorials, and no examples, no one would be able to use it. So for 6 months I re-wrote and cleaned it up so other people could use it too. Not many people can work for 6 months without pay, and since the site has Community Promotion Ads I thought this might be OK. – Andrew Norton Jul 12 '19 at 17:36
  • 9
    Why would there be a problem with a commercial package, if it solves the problem? – Jason B. Jul 12 '19 at 19:37
  • You don't need a CellEvaluationFunction if you just bind something to {"MenuCommand", "Save"}. I've been doing the same to work with Markdown from Mathematica for like 2+ years now along with a bunch of other formats. Other people have undoubtedly been doing the same. You can be more sophisticated, too, about your saving function, too, by using some simple front-end programming to automatically parse your document into the desired format. To be honest, a nice Mathematica-as-file-format-front-end package can be written in general, not just for TeX, by making use of this kind of framework. – b3m2a1 Jul 12 '19 at 20:17
  • 2
    I don't like this mainly because it's not straightforward presented as the release of a commercial solution. It's not even presented as a good Q&A. I'm not sure what's the policy, I assume it's allowed given that moderators didn't follow up on my flag. But subjectively, it feels like sneaky way to sell what everybody else here share free of charge. – rhermans Jul 12 '19 at 22:47
  • 17
    @rhermans and Andrew: unless policies have changed recently, and I may not be aware as presently I am mostly inactive, I interpret this as being OK *however* you must announce your affiliation to the product, and excessive self-promotion is against the rules. Please edit your answer to make it clear that you are the author/proprietor of this product, and please do not let its promotion become a major thrust of your participation on this site. Thank you. – Mr.Wizard Jul 13 '19 at 07:36
  • 1
    Probably we should move this discussion to [meta]. I found a guideline on promotion, and based on those criteria, IMO this is more "Tell" than "Show", it's not answering a question from the community, it's a poorly-written Q&A, and doesn't disclose the nature of the post in a straightforwardly. So not cool, but also not blatantly illegal based on general guidelines. I assume that as a community we are also entitled to our own variations around such policy and to define the subtleties of what is desirable. Votes suggest this is controversial – rhermans Jul 13 '19 at 08:13
  • Answering your own question in the context of self-promotion is not directly addresses by the policy. I have just asked that question in [meta]. – rhermans Jul 13 '19 at 08:42
  • @Mr.Wizard: I have edited my answer as you suggested.Thank you for the advice. – Andrew Norton Jul 13 '19 at 13:23