"Publish" to pdf

Using Matlab, I would like to create a pdf document summarizing the results of several experiments. Ideally, I'd have one page per experiment with 2 multi-panel figures and a bit of text. I thought that publish.m would be the way to go, however, publishing to pdf does not support inserting page breaks, and I can neither control the figure quality (which is rather bad), nor the figure size (which means that 2 figures take up the entire page), nor could I stuff the text into headers/footers. How else could I create a multipage pdf with some control over the layout from within Matlab?

114k 39 39 gold badges 252 252 silver badges 387 387 bronze badges asked Jan 15, 2010 at 13:07 74.9k 10 10 gold badges 138 138 silver badges 177 177 bronze badges

6 Answers 6

There exists the Matlab Report Generator Toolbox, which can do that very easy. Of course it costs some money.

You could try to write out some markup text from Matlab and then convert it to PDF using some other tools. Possible converters:

If you don't want this, have a look at the Matlab documentation. You can specify the image size/resolution for published figures and I think there exists the possibilty to insert line breaks.

answered Jan 15, 2010 at 14:01 Wolfgang Ulmer Wolfgang Ulmer 3,480 2 2 gold badges 22 22 silver badges 13 13 bronze badges

Thank you for your suggestions. Of course, the Report Generator Toolbox happens to be one of those I don't have access to. Unfortunately, it is not possible to specify image size/resolution and line breaks for publishing to pdf, as I mentioned in the question (a look at the Matlab documentation and a service request confirmed that). I guess it is going to be a toss-up between learning enough LaTeX to make a nice document, or using publishing to HTML to make something fast.

Commented Jan 15, 2010 at 18:57

What about using preformatted text (Cell > Insert Text Markup > Preformatted Text) in M-File comments ? It supports line breaks. To be able to define image sizes, first use the print -dpng command (see help print ) to create a separate image file, then include the generated file in your published M-file (<>). You can specify options to define the size of the printed image. Commented Jan 15, 2010 at 20:09 Sorry, I meant 'page breaks', not 'line breaks'. Commented Feb 17, 2010 at 3:35

If you really want control over pdf generation, take a look at the iText Java PDF library -- you can use Java libraries fairly easily from MATLAB.

answered Jan 18, 2010 at 17:14 189k 171 171 gold badges 628 628 silver badges 991 991 bronze badges
  1. Here is an example.m file with plots in a loop to be published:
x = -5 : 5 for i=1:3 plot(x, x .^ i ); snapnow; disp('\clearpage') end 
. \toolbox\matlab\codetools\private\mxdom2latex.xsl 
opts.format='latex';opts.stylesheet='custom_mxdom2latex.xsl' publish('example', opts) 

P.S. yep, not documented, it's a pity.

answered Jun 3, 2013 at 15:51 1,764 14 14 silver badges 18 18 bronze badges

damn, I was just thrown to MATLAB and its reporting has been looking so bad after my positive experience with R/Sweave.

Commented Jun 3, 2013 at 17:07

MathWorks do a Report Generator toolbox that allows you to style your output - basically it is a much fancier version of the publish function.

answered Jan 15, 2010 at 13:59 Richie Cotton Richie Cotton 121k 47 47 gold badges 250 250 silver badges 365 365 bronze badges

In the end I went with LaTeX, since it gives documents of much higher quality than if I went via HTML.

Unfortunately, publish.m is rather limited even when it comes to publishing to LaTeX. For example, if you add multiple figures inside a loop, it is not possible to set page breaks, and adjusting figure sizes to get exactly N figures onto a page is very hard.

Therefore, I wrote a function to directly write the LaTeX file (using export_fig from the file exchange to save the figures), and another one to compile to pdf. This way, I can easily generate LaTeX files and I have a lot of power over formatting.

answered Feb 17, 2010 at 3:36 74.9k 10 10 gold badges 138 138 silver badges 177 177 bronze badges

Yes to LaTeX as noted above. If you publish equations, you will find that necessary for nice output eventually anyway. Once you go that path, the following markup works well. I then use TexWorks to convert the LaTeX into a pdf. A side benefit (and the real reason I went this path) was to get nicer looking equations than I could get with the built in pdf converter. Download and install took an hour or two while I was doing other stuff and it runs a bit faster than it did when going straight to a pdf.

%% % % \clearpage % 

Side note: some error that I don't recall anymore came up with each new equation in Matlab 2012b (the equation appeared, I just had an error). When I switched to LaTeX, this stopped, but I needed a preferences file specified in Matlab with these lines inserted to get all the equation capability I wanted.

I don't know enough about LaTeX or TeXworks to know what capabilities these packages added to what TeXworks uses by default when publishing to pdfLaTeX+MakeIndex+BibTeX