开发者

"Publish" to pdf

开发者 https://www.devze.com 2022-12-16 06:53 出处:网络
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.

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?


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:

  • Write out LaTeX, then use PdfLaTeX to generate PDF
  • Write out reStructuredText, then use Python docutils to generate PDF
  • Write out DocBook XML, then use any DocBook to PDF converter
  • Write out HTML, then print it with your browser or OpenOffice to PDF.

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.


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.


  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('<latex>\clearpage</latex>')
    end
    
  2. Take a sample xsl-stylesheet file from your MATLAB installation dir

    ...\toolbox\matlab\codetools\private\mxdom2latex.xsl
    

    and copy it next to your example.m file, say, as custom_mxdom2latex.xsl.

  3. Find the following evil lines in your custom .xsl file that destroy the order of the output:

    <xsl:apply-templates select="text"/>
    <xsl:apply-templates select="mcode"/>
    <xsl:apply-templates select="img"/>
    <xsl:apply-templates select="mcodeoutput"/>
    
  4. Replace the lines with this:

    <xsl:apply-templates select="text|mcode|img|mcodeoutput"/>
    
  5. publish:

    opts.format='latex';opts.stylesheet='custom_mxdom2latex.xsl'
    publish('example', opts)
    

    you're done :)

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


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


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.


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.

%% 
% <latex>
% \clearpage
% </latex>

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.

\usepackage{amsmath}

\usepackage{amssymb}

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

0

精彩评论

暂无评论...
验证码 换一张
取 消