I am writing a book in the Wolfram CDF format and would like to improve upon the default equation typesetting of Mathematica. To this end I've loaded @Szabolcs' valuable MaTeX, which imports $\LaTeX$ typesetting into the Mathematica document:

ConfigureMaTeX["pdfLaTeX" -> "/usr/texbin/pdflatex"]

Here's a comparison between $\LaTeX$ at the top, and Mathematica at the bottom. There is no question whatsoever that the aesthetic and legibility difference is profound.

enter image description here

Here is just one small part of an actual equation that will appear in my book:

enter image description here

Again: not the slightest doubt that the $\LaTeX$ version (bottom) is vastly superior to that in Mathematica (top).

When I use DisplayNumberedFormula and cut and past an input cell into it, I get something that looks like this:

enter image description here

which I find particularly ugly and illegible, at least compared to $\LaTeX$.

Let me stress that I consider myself an expert $\LaTeX$ user (published many books, solution manuals, etc., with sales into the high 10s of thousands) as well as an expert Mathematica programmer. I'm happy to use whichever tool leads to the best result.

Here is a sample of the rendered Mathematica document. (I've left open the equation cell.) Notice how legible is the rendered equation.

Sample book page with MaTeX equation

A number of problems arise, however:

  1. The default size of the rendered MaTeX equation is too small, so I have to adjust the output using ImageSize, as shown above. However, this option sets the overall size of the rendered equation, of course not the size of individual variables. Some equations have one line, others two or more. Is there a way to automatically set the image size so that the rendered variable (e.g., an $x$-height) is constant?
  2. Of course for the final book, I double-click on the bottom half of the highlighted cell to display only the MaTeX-rendered equation, not its source code. I want to be able to edit the typesetting commands and hit return and get an "output" cell of the type NumberedEquation. In short, is there a way to make a special "Equation Input" cell such that its output cell (an equation) is of a type NumberedEquation which retains its Tag and other properties as I type in variations of the source MaTeX code?

Any suggestions?

  • $\begingroup$ Your edit just demonstrates a lack of understanding of typesetting Mathematica equations for output. At the end of the day people should use whatever code enables them to get the job done the way they prefer. If you know LaTeX and prefer that then of course use that but the example above is an apples and oranges comparison. $\endgroup$ – Mike Honeychurch Feb 27 '16 at 0:31
  • $\begingroup$ Mike Honeychurch: My book (Pattern Classification, 3rd ed.) is meant to be read and understood by students. The book has nothing to do with using "whatever code enables them to get the job done." Students don't care what typesetting I use... they need to read and understand the text and especially the equations. If there is some Mathematica display setting that yields high-quality equation typesetting, then I'm eager to learn of it. But all Mathematica's defaults are woefully inadequate, as my examples above (and many others) prove. I know well both $\LaTeX$ and Mathematica. $\endgroup$ – David G. Stork Feb 27 '16 at 0:51
  • 1
    $\begingroup$ I don't know if this is of use to you, but I always use a version-8 style sheet in which TraditionalForm looks really traditional (as close to $\LaTeX$ as it ever was). See my question Inconsistent display of TraditionalForm in version 9 for ways to get this older styling back. To enter displayed equations, I follow some variation of this answer. I usually create PDFs of notebooks and distribute them along with the source. However, PDF export is never great (e.g., no reference links). $\endgroup$ – Jens Feb 27 '16 at 3:59
  • 2
    $\begingroup$ MaTeX has the options FontSize and Magnification both of which can be used to achieve consistent resizing of text. $\endgroup$ – Quantum_Oli Feb 27 '16 at 12:08
  • 2
    $\begingroup$ Please do make sure that you use version 1.2.0 of MaTeX, released just a few days ago! It corrects some subtle sizing bugs. Check it with MaTeX`Developer`$Version. $\endgroup$ – Szabolcs Feb 29 '16 at 18:04

this is (now a very long) comment not an answer.

The objective is to write a book using Mathematica. Within the book equations are generated from LaTex rather than typeset directly using Mathematica. By definition if this was to be done entirely using Mathematica, the equation typesetting would not be done in an input cell. It would be done either in one of the bundled styles that have been designed for rendering typeset equations, such as "DisplayFormulaNumbered", or a custom cell style with similar properties. Comparing output to input is a disingenuous apples/oranges comparison. If you use Mathematica correctly for creating equations then you can create a book with typesetting like this:

enter image description here

Now this may still be unsatisfactory to some people (who may prefer other methods such as LaTex) but it is also quite different to the apples/oranges comparison presented in the question.

So the first part of the question, when modified to compare like to like, ultimately gets down to what best enables you to do the job (i.e. typesetting a document) efficiently and meet your aesthetic preferences.

The second part of the question was a required code modification so that you can evaluate a cell and have the output in a numbered equation style rather than output. It is disappointing when contributors here devote time to provide an answer, demonstrate the answer, and have it ignored.

enter image description here

enter image description here

enter image description here

enter image description here

Edit to reply in more detail to first comment.

My deleted answer to part 2 was just that. Part 2 asks "In short, is there a way to make a special "Equation Input" cell such that its output cell (an equation) is of a type NumberedEquation". In other words this is not asking about typesetting it is asking about changing a generated cell style. My deleted answer provided a solution.

In your comment you say "Your retracted code that I saw before did not (as far as I could tell) lead to reasonably nice equations, such as in 11.48, above."

As per first edited sentence the answer was a solution to changing a generated cell it was not a solution to typesetting. If you want to typeset in Mathematica you do not type into input, evaluate input, and display the output (and probably hide the input) in your document. That is a fundamentally wrong way to go about typesetting a document ("natively") in Mathematica. If you want to use the "MaTex" package then yes, that is what you need to do, but if you want to typeset directly in Mathematica then that is the last thing you want to do.

  • $\begingroup$ It is true, I am not an expert in using Mathematica for display and am eager to learn. (My book will be an eBook, so the tools for typesetting to paper are not relevant.) Your retracted code that I saw before did not (as far as I could tell) lead to reasonably nice equations, such as in 11.48, above. Would it have been that nice? If so, point me to such styles. Regardless, one must ask: Why did expert szabolcs (reputation 90k) feel the need to create MaTeX if the quality of Mma output was high like you say? I'm not alone in wanting improvements over Mathematica typesetting. $\endgroup$ – David G. Stork Feb 27 '16 at 1:05
  • $\begingroup$ The objective is absolutely positively NOT to write a book using Mathematica! Not at all. It is instead to create a document (paper or eDocument) that will best inform students and experts alike, whatever the creation tool. If MS Word led to better typesetting, layout, control, etc., I would use it. Likewise FrameMaker. Or any other software. I want to use Mma for dynamic figures created by computer science expert (and friend) Stephen Wolfram, but wish to preserve the superb typography developed by typographical sophisticate (and friend) Donald Knuth. $\endgroup$ – David G. Stork Feb 27 '16 at 1:11
  • $\begingroup$ Maybe szabolcs already knows LaTeX -- you need to ask him. For anyone who knows LaTeX then I don't see any reason to switch. The point is that, and I realise this is subjective, pretty good equation rendering can be done in Mathematica. I am not saying it has to be done in Mma I am saying it can be done if you choose to. ... and reiterate that the examples in the question are apples and oranges $\endgroup$ – Mike Honeychurch Feb 27 '16 at 1:14
  • $\begingroup$ Mike: I like the equations you have posted. How do I create these in an electronic Mathematica document? I couldn't see that your earlier code would accomplish that, but maybe I'm wrong. [Surely szabolcs knows well both Mathematica and $\LaTeX$.] $\endgroup$ – David G. Stork Feb 27 '16 at 1:15
  • $\begingroup$ @DavidG.Stork please see my edit. Szabolcs is a Mma expert but I cannot speak to why he made the package other than if he was already expert in LaTeX then it would make sense to typeset in LaTeX. You need to ask him. I do not know LaTeX but the impression I get is that the rendering can be done a whole lot easier (it can still be done in Mma but generally it can often take a fair bit of tweeking). You should not be using input cells. You should be using e.g. DisplayFormulaNumbered and then creating an inline cell. Do that and cut and paste one of your input cell eqns as a first try $\endgroup$ – Mike Honeychurch Feb 27 '16 at 1:26

I guess my questions are 1) why you are not doing the typesetting in Mathematica; 2) for those of us unfamiliar with MaTeX are the equations rendered as vectors or bitmap or what?

Here is a solution to part 2 of your question.

Step 1. Make a new style. I am going to call it "MaTeX". In this style you will use the CellEpilog option to change the output cell to your desired style.

 Cell[StyleData["MaTeX", StyleDefinitions -> StyleData["Input"]],
  MenuSortingValue -> 1510,
  CellEpilog :> (SelectionMove[EvaluationCell[], All, GeneratedCell]; 
    FrontEndTokenExecute[EvaluationCell[], "Style", 

Step 2. Add this style to a private style sheet or for regular work create a new stylesheet with this cell.

Step 3. Use this style as per GIF below. In the GIF I first evaluate an input cell and it creates an output cell. Then I repeat but first change the cell style to "MaTeX" (which occurs offscreen) and then you see that the output is a "DisplayFormulaNumbered" cell.

enter image description here

  • 1
    $\begingroup$ Mike Honeychurch: As I wrote, I "...would like to improve upon the default equation typesetting of Mathematica." Type a complex formula with lots of integral signs, summations, powers (of powers), factorials, upper- and lower-case Greek letters and such. They look ugly in Mathematica but gorgeous in $\LaTeX$. Your simple examples of "1 + 1" are not nearly sophisticated enough to show the graphic limitations of Mathematica. Even my simple example, above, shows the superior legibility of $\LaTeX$. $\endgroup$ – David G. Stork Feb 27 '16 at 0:14
  • $\begingroup$ @DavidG.Stork beauty is in the eye of the beholder. You don't provide any examples of Mathematica being deficient in typesetting. Certainly the example doesn't appear to be an improvement on Mathematica (IMO). I've typeset probably 2200 pages of Mathematica for publication over the years, maybe more, and had not had issues with the aesthetics. ...I should add that if we are talking about ease of typesetting then that would be a different issue. Anyway is the above a solution for you? The example of 1+1 is solely to generate your desired output cell type. I thought that would be obvious $\endgroup$ – Mike Honeychurch Feb 27 '16 at 0:22

I did not notice this post until today.

MaTeX has two options for setting the size of the output:

  • Magnification just scales everything (i.e. the PDF file output by LaTeX) proportionally.

  • FontSize passes the font size to LaTeX and thus gives access to the special variants for each point size.

When I work with MaTeX, I tend to set all its option to my preferred value at the start of the session, like this

SetOptions[MaTeX, Magnification -> ..., FontSize -> ..., ...]

I.e. just what @martin mentioned.

Mathematica's default font size is 14, but MaTeX's is 12. So you may want to start with FontSize -> 14 and leave Magnification at the default value of 1.

Here's a little demo of how FontSize and magnification differ:

MaTeX["\\int_0^\\infty e^{-x}\\, dx", FontSize -> 20, Magnification -> 1]

enter image description here

MaTeX["\\int_0^\\infty e^{-x}\\, dx", FontSize -> 10, Magnification -> 2]

enter image description here

MaTeX["\\int_0^\\infty e^{-x}\\, dx", FontSize -> 5, Magnification -> 4]

enter image description here

5 pt is designed for legibility at small sizes and looks awkward magnified 4 times. 20 pt is designed for beauty when displayed at its actual size.

Since version 1.2.0 it also has the options ContentPadding and LineSpacing which effectively control vertical margins and shouldn't be necessary most of the time. Here's a small demo:

f = Framed[#, ContentPadding -> False, FrameMargins -> None, 
    FrameStyle -> AbsoluteThickness[1]] &;

f /@ MaTeX /@ {"x", "y", "M"}

enter image description here

f /@ (MaTeX[#, LineSpacing -> {1, 0}] &) /@ {"x", "y", "M"}

enter image description here

f /@ (MaTeX[#, ContentPadding -> False] &) /@ {"x", "y", "M"}

enter image description here

Both options follow the standard Mathematica usage. LineSpacing -> {m,a} makes the line height be m*fontsize + a, in points. ContentPadding -> True enforces a minimal height equal to one line heigh for the output. A display style output is usually taller than one line height, so it won't make a difference there.

Here's a little modification to @Mike's special cell style.

 Cell[StyleData["MaTeX", StyleDefinitions -> StyleData["Program"]], 
  MenuSortingValue -> 1510, 
  CellEpilog :> (SelectionMove[EvaluationCell[], All, GeneratedCell];
    FrontEndTokenExecute[EvaluationCell[], "Style", 
  Evaluatable -> True, 
  CellEvaluationFunction -> (MaTeX[
      First[FrontEndExecute[ExportPacket[Cell[#1], "InputText"]]]] &),
   CellFrameLabels -> {{None, "MaTeX"}, {None, None}}, 
  CellGroupingRules -> "InputGrouping"]

Sorry about the messy code formatting. This is a special evaluatable input cell that renders LaTeX code:

enter image description here

You could probably modify this to suit your need better. One advantage is that there's no need to type \\ all the time anymore. A single \ will be fine.

Finally, I have to give a few warnings, as this is not really the use case I anticipated for MaTeX. I made MaTeX because I wanted to have more beautiful labels in publication figures. But I never meant it to be used for in-notebook display. There will be a few disadvantages if you do this.

  1. MaTeX returns the result as vector graphics. This takes up considerably more space than the text equivalent.

    expr = HoldForm[Integrate[Exp[-x], {x, 0, Infinity}]]
    ByteCount /@ {expr, MaTeX[expr]}
    (* {304, 24328} *)
  2. MaTeX's output is really meant for print or for PDF viewers, which tend to render vector graphics with quite a lot of care. Font glyphs are always rendered differently by computers than other vector drawings. Text rendering is special and it is optimized for legibility. This is going to be lost with MaTeX because it converts everything to simple curves. If you use a "retina" display, you will not be able to notice this. But many of your readers will have low resolution screens, and to them the MaTeX output will look more "blurry" than other text.

    My personal opinion about this is that if the formulas are displayed in a large enough size (and if there aren't too many of them), then LaTeX's superior typesetting outweighs the disadvantage of the blurriness. But it's something to be aware of.

  3. MaTeX takes great care to preserve the baseline. Thus mixing MaTeX output with text should be okay in theory ... it looks like this in large size.

    enter image description here

    But it turns out that the front end aggressively rounds everything to integer screen pixels early in the display pipeline, and this causes misalignments when displaying things at standard sizes:

    enter image description here

    So in practice it won't be possible to mix MaTeX output with standard text and still make it look good.

All in all, I would be very careful with using too much MaTeX in a CDF document. In figures meant for print/PDF I use it all the type. If I had to use it in CDF, I would restrict it to a few display formulas only, and I would be very careful to keep everything legible at lower resolution screens.

  • 1
    $\begingroup$ Szabolcs: Thanks so much (+1), and especially thanks for creating MaTeX. $\endgroup$ – David G. Stork Feb 29 '16 at 18:12
  • $\begingroup$ @David I hope you'll find it useful. I updated the post a little bit. $\endgroup$ – Szabolcs Feb 29 '16 at 18:34

You could set it up it manually:

SetOptions[MaTeX, "Preamble" -> {"\\usepackage{amsmath,amssymb,array}"}, 
Magnification -> 1.5];

    MaTeX["\\begin{tabular}{>{\\raggedright}m{12cm} c}
 $\\displaystyle\\mbox{}\ \\int\\limits_{x = \
-\\infty}^\\infty\\!\\!\\! |f(x)|^2 dx = \\dfrac{1}{2\\pi} \
\\int\\limits_{\\omega = -\\infty}^\\infty\\!\\!\\! |F(\\omega)|^2 \
 $\\text{This is some text.}$
 $\\displaystyle\\mbox{}\ p(\\mathcal{D}|\\theta)=\\prod\\limits_{k = \
1}^{n}\\dfrac{1}{(2\\pi)^{d/2}|\\Sigma|^{1/2}} \

enter image description here

  • 1
    $\begingroup$ Excellent. Thanks so much! I think Wolfram Research should make something like MaTeX the default within Mathematica. No reason to retain the ugly mathematical typesetting! $\endgroup$ – David G. Stork Feb 29 '16 at 18:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.