There is a package called pgfsweave for R. It can sweave R code into a LaTeX document. I found we can gain some advantage from this method.

  1. The report document can reproduce.
  2. The font in figure keeps consistent with main font of document.

I think "sweave" code is the best method for data-oriented report (especially for LaTeX). By sweave, we can separate data (or figure) from the document, through LaTeX, we can separate content from document style.

Unfortunately, I didn't find a corresponding package or some equivalent method for matlab and mathematica.

I know some packages, like psfrag or matlab2tikz, that work well. But we need a more powerful tool to do this that can change our workflow and that can make our life easier.

Is that possible? How do you solve this problem? Does it meet the philosophy of matlab or mathematica?

  • Perhaps with StatWeave. (This indicates that it could work with Matlab at least, but I do not know if it is actually possible, therefore I'm posting this as a comment.) – Torbjørn T. Nov 17 '11 at 18:09
  • @TorbjørnT. No, it could not work with Matlab yet. native support may be better. – pythonee Nov 17 '11 at 19:00
  • What about dexy? dexy.it – Roman Luštrik Nov 17 '11 at 22:26
  • You might be interested in the matlab-prettifier package; see this answer. – jub0bs Apr 28 '14 at 15:36

There are three ways of doing this:

  1. Do the weaving at the application (Matlab, Mathematica, R) end. That is, the application should be aware of the TeX format and ignore everything other than \begin{code} ... \end{code} snippets. This is how Sweave and literate haskell work.

  2. Do the weaving at the TeX end, that is, let TeX call the external application (with appropriate switches) and then display the result. This the approach that the R and gnuplot modules in ConTeXt follow.

  3. Use a general purpose literate programming tool, like noweb (or those targeted to a specific language like Ocamweb).

For the second approach, I have written a ConTeXt module, filter, that allows you to pass the content of a program to an external program and read back the results. For example, you can replicate the functionality of sweave using:


  [filtercommand={R CMD BATCH -q --save --restore \externalfilterinputfile\space \externalfilteroutputfile},

Then, using


will execute the resultant code using R and show the output generated by R. Using


will execute the code using R but not show the output. The same approach will work for Matlab or Mathematica by replacing the filtercommand by the appropriate call to Matlab/Mathematica. This approach can be used for other purposes as well

  • @pythonee: The first approach means that you have to write appropriate programs in Matlab/Mathematica to generate TeX output. You cannot reuse code...additional effort is required to take care of each format. That is why I prefer the second approach. The solution can also be implemented in LaTeX (it is just that I use ConTeXt, so I implemented it in ConTeXt). Using an intermediate data format is a step in the right direction (you can easily change output formats), but requires more work. – Aditya Nov 17 '11 at 20:11
  • Thank you for your answer. I prefer the first approach and I not familiar with ConTeX. I come up one idea. I think we can generate the compute result as certain intermediate format(xml or others) and then extract the information contain in intermediate data format to LaTeX. But figure can't keep consistent with main document.How do think about this or someone have implemented this? – pythonee Nov 17 '11 at 20:14
  • @pythonee: It depends on how the figures are produced. As an experiment, I had implemented an interface to interact with gnuplot here, where I label each plot manually (passing name=....) and then using the figure by an appropriately defined macro (see the definition of \usegnuplotfigure). This relies on the fact that, in gnuplot, the name of the figure is the same as that of the filename. This is certainly not the case in R (and I think Matlab), where the R code has to specify the figure name. In those cases, you can directly use the figure name in TeX. – Aditya Nov 17 '11 at 20:48
  • Have you heart about Tpx or Ipe. they both work with latex or tex system well. I think matlab or mathematica can learn something from them. Through this, we can gain power of matlab(mathematica,R,SAS) and the power of TeX – pythonee Nov 17 '11 at 23:42
  • @pythonee I know a couple of toolbox developers in person for MATLAB and I can pretty much surely say that they couldn't care less. The thing is they do whatever their gazillion dollar worth customers want. If they need it you will get it otherwise no way. I mean ever! You can expect in the future that it communicates better with MS Office but not LaTeX. I see that you are aware of Ipe, then you can dive into nuts and bolts of Lua and create your own wonders with Ipe. I think there is also something called LuaX but I don't know if it is still up to date. – percusse Nov 18 '11 at 0:45

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.