kubaPod / M2MD

Simple converter of Mathematica notebooks to markdown.
45 stars 13 forks source link

Add an option to render LaTeX as images #13

Open vsht opened 4 years ago

vsht commented 4 years ago

Since GitHub doesn't support LaTeX equations in its markdown, it might be useful to have all equations rendered as images. This way the resulting files can be easily added to a GitHub project wiki or a github.io site.

At the moment I can achieve the desired effect by simply commenting out

M2MD[style_, cell:_[BoxData @ FormBox[_, TraditionalForm], ___], OptionsPattern[]
] := MDElement["LaTeXBlock", BoxesToTeXString @ cell ];

Of course, this can be done in a more elegant way via a separate option of MDExport.

vsht commented 4 years ago

Actually, I think that in this mode it would be even better to render images not as PNGs but as SVGs, so that people with lowres HD and highres 4K screens can equally enjoy high quality typesetting of equations.

I would also appreciate an ability to apply some replacement rules to the cell before converting it to an image. Like in

Export["/home/vs/Downloads/out.svg", 
 Cell[BoxData[
    RowBox[{RowBox[{"(", RowBox[{"a", "+", "b"}], ")"}], "/", 
      RowBox[{"(", RowBox[{"c", "+", "d"}], ")"}]}]], "Code"] /. 
  "Code" -> "Input"]

where "Code" -> "Input" helps to avoid the ugly grayish background of a code cell from a .m file.

kubaPod commented 4 years ago

@vsht yes, LaTeX elements are kind of legacy because I wrote a prototype for a quick export to StackExchange. They should probably be off by default.

About a big picture you've posted: In a free time I will try to release an update that will allow you to do:

MTM2[
 _
, "CellStyleRules" -> <|
      {cellStyleAny_, BoxData @ FormBox[_, TraditionalForm]} -> {"Image",ToImageElement[#, ToImageFunction -> whateverExport]& }
  |>
vsht commented 4 years ago

Sounds great! As of now I'm going to use a tweaked version of M2MD to export our FeynCalc examples into a bunch of .md files and add them to feyncalc.github.io. I already did some tests and the results look way better as compared to what one can achieve with

Export[output, nb, "HTML",
   "CSS"-> "HTMLFiles/stylesheet.css","MathOutput"->"PNG","GraphicsOutput"->"PNG"];

Using NotebookPauseForEvaluation from here one can conveniently automatize the evaluation of .m files via SelectionEvaluate[].

IMHO notebooks rendered as markdown files on GitHub is a much more convenient solution as compared to uploading them to https://www.notebookarchive.org. I suppose other Mma package projects might then soon follow the idea to create such showcases.

Many thanks for this very useful package!