Since I really like the SIMD-way of manipulating data in Octave, all my data processing/visualization is done in it. In order to obtain high quality plots (for conference papers), I generate all plots in PDF format. Unfortunately, the default way of saving plots in PDF has a few caveats, which make them hard to be used in a paper directly. Therefore, I am sharing my experience on how to save plots in PDF format that can be used in a conference paper easily. There are mainly two problems, plot dimension and embedding fonts.

Plot Dimension

The following two commands fit the paper to the plot instead of the default A4 size, which makes it hard to include in a paper due to the surrounding excessive blank space.

1
2
3
set(gcf, 'PaperPosition', [0 0 8 6]);
set(gcf, 'PaperSize', [8 6]);
% or one can specify -dpdfcairo in print, but this one might not be available in old octave releases

Embedding Fonts

Conferences often require all fonts used to be embedded in the final submission. octave uses Helvetica for plotting by default, but it’s usually not available on Linux. One can use pdffonts to check what fonts are used in a pdf and whether a certain font is embedded or not.

1
2
3
4
5
$ pdffonts test.pdf
name type encoding emb sub uni object ID
------------------------------------ ----------------- ---------------- --- --- --- ---------
Helvetica Type 1 Standard no no no 7 0
LDKUJD+:Bold Type 1C WinAnsi yes yes no 8 0

When a font is not embedded, and it’s missing on the platform it’s viewed, a substitute font will be used. On my box, “Nimbus Sans” is used.

1
2
3
name                                 object ID substitute font                      substitute font file
------------------------------------ --------- ------------------------------------ ------------------------------------
Helvetica 7 0 NimbusSans-Regular /usr/share/fonts/opentype/urw-base35/NimbusSans-Regular.otf

Instead of relying on the substitution, I can change the default font to something that’s available on my box so that all fonts in the pdf are embedded. One can set the default font as shown below, but in order for it to take effect, the Cairo renderer must be used. IOW, use -dpdfcairo instead of -dpdf. Another good font is “FreeSans”.

1
2
set (0, "defaultaxesfontname", "NimbusSans")
set (0, "defaulttextfontname", "NimbusSans")

Complete program

1
2
3
4
5
6
7
8
9
10
11
12
set (0, "defaultaxesfontsize", 10)
set (0, "defaulttextfontsize", 10)

set (0, "defaultaxesfontname", "NimbusSans")
set (0, "defaulttextfontname", "NimbusSans")

plot(1:10)
title('test')
xlabel('x')
ylabel('y')

print(1, "test.pdf", '-dpdfcairo')

Bonus: normalize the PDF

If the generated PDFs are checked into the repo (for archive/sharing purpose), it’s better to strip the meta data firstly. The following commands are inspired from https://gist.github.com/hubgit/6078384

1
2
3
4
exiftool -overwrite_original -all:all="" test.pdf &> /dev/null
qpdf --linearize --replace-input test.pdf
perl -i -pe 's!trailer <<.*!!' test.pdf # this is for removing the trailing ID; seems working fine for PDFs from Octave, but not sure about others
md5sum test.pdf