LaTeX Tips and Tricks

Mahesh Mahesh Follow Aug 17, 2012 · 8 mins read
Share this

There seem to be quite a few techniques to embed the fonts while generating PDFs from LaTeX sources, and I just wanted to collate a few in one post. Further, I wanted to share my complicated workflow in creating vector graphics. Let me know if you have better solutions (in the past I have use inkscape with ‘limited’ success: some dots simply disappeared in Mac’s preview).

Embedding Fonts

If you are using pdflatex:

If you have all your figures in PDF and you prefer using pdflatex, then you may want to follow this link.

Update: Most of the time I just use pdf images and so I end up using pdflatex. Pdflatex is not only convenient, I think its recommended. I recently stumbled upon this eternal problem of running into non embedded fonts. This time I could quickly point my fingers towards a few pdf plots generated using Matlab. Thanks to Kimo, I was able to fix the problem quickly. All I had to do was to run the following

ps2pdf14 -dPDFSETTINGS=/prepress fig_non_embedded.pdf fig_embedded.pdf

I believe ps2pdf14 is equivalent to ps2pdf -dCompatibilityLevel=1.4 (just a guess), and this should also help prevent the error “Acrobat version is less than 5.0” that you might get on IEEE PDF eXpress. [Note that earlier I had ps2pdf13 used, and I had this issue].

I made a handy script to fix all the figures.

ps2pdf13 -dPDFSETTINGS=/prepress "$1" "$1.emb"
rm "$1"
mv "$1.emb" "$1"
pdffonts "$1"

If all your images now have embedded fonts, then you should be able to generate an impeccable pdf with all fonts embedded, unless the universe decides against you (which happens quite often with me).

If you are prefer the long way:

If you have eps figs or may be pstricks based code for image, then you may want to do latex -> dvi -> ps -> pdf. In that case, I do this:

latex filename.tex
dvips filename.dvi
ps2pdf -dEmbedAllFonts=true -dSubsetFonts=true -dEPSCrop=true -dPDFSETTINGS=/prepress

You will end up with filename.pdf which should have all the fonts embedded.

If you have an EPS image which you want to convert to PDF while making sure the fonts are embedded, you can try

epstopdf --outfile='filename-temp.pdf' filename.eps
gs -dSAFER -dNOPLATFONTS -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sPAPERSIZE=letter -dCompatibilityLevel=1.4 -dPDFSETTINGS=/printer -dCompatibilityLevel=1.4 -dMaxSubsetPct=100 -dSubsetFonts=true -dEmbedAllFonts=true -sOutputFile=filename.pdf -f filename-temp.pdf
rm filename-temp.pdf

Credit: Joey

Yet another way would be to embed the EPS figure in an article tex file and then compile the above way to generate a PDF. Now you will be stuck with a letter size PDF image, which you can crop (see below).

Generating Vector Graphics

I generally use this awesome program called LatexDraw which is great except that it gave me a little bit of a headache for about 4 hours because the PDFs I was creating did not have all fonts embedded. Its not always like that, so I do recommend it highly. I tried xfig and it looked it got teleported from 1995.

Install it, and start drawing the block diagram you need. Its quite straight forward. Its cool to see the code getting updated as you draw. In fact you if you can type formulae between $ and $. They will get compiled to the correct formulae, but you may have to try several times to get the position right.

Once you are happy and smiling, copy the generated code (they even have a button to do that!), and get ready for some hard work.

Again, you have two options. I like covering these images to PDFs and then embed it in my paper and run pdflatex. Instead, if you are going the long way of running latex->dvi->ps->pdf, then you all you may need to do is to copy paste the code. If you want it as a figure and want to add captions, just surround by figure:

% your generated code.
\caption{The case when $\beta > 1$ or whatever caption you like}

There may be a number of reasons you may not want to do this. May be your mac is complaining that pstricks is not found, in which case I am not sure what to do. I have access to a linux machine which doesn’t complain. But since my pdflatex (in both mac/linux), I use the linux machine to generate the PDF the long way (also an EPS will be nice to have right?).

So fire up your latex editor and create a fresh file, like below.

\scalebox{0.9} % Change this value to rescale the drawing.

If for any reason minimal does not work, try article class.

Do the following again:

latex figname.tex
dvips figname.dvi
ps2pdf -dEmbedAllFonts=true -dSubsetFonts=true -dEPSCrop=true -dPDFSETTINGS=/prepress

Keep making sure that the generated PDFs have all the fonts embedded.


The output will look like this: Output figure using latexdraw

but of course in a pdf file. Again make sure it has all fonts embedded.

Next course of action is to crop this pdf. There is a handy tool called pdfcrop by Heiko Oberdiek, which might be already installed in your system, otherwise get it from here

I generally create a folder called helper and put the file and also the following file

# usage:
# generatePDF <filename without ext>

latex "$1.tex"
dvips "$1.dvi"
ps2pdf -dEmbedAllFonts=true -dSubsetFonts=true -dEPSCrop=true -dPDFSETTINGS=/prepress "$"
pdfcrop "$1.pdf"

You can just run

./generatePDF filename

Make sure not to give the extension (.tex).

Now you will end up with a PDF that you can just embed as usual.

Also, if you want to get EPS file, you can do the following:

pdftops -eps filename.pdf 

If you directly want EPS files, save and use the following script

# usage:
# generatePDF <filename without ext>

latex "$1.tex"
dvips "$1.dvi"
ps2pdf -dEmbedAllFonts=true -dSubsetFonts=true -dEPSCrop=true -dPDFSETTINGS=/prepress "$"
pdfcrop "$1.pdf"
pdftops -eps "$1-crop.pdf"
rm "$1-crop.pdf"

You should be all set to go.

Force letter dimensions.

I was writing a one page document in latex and found out that it wasn’t really generating 8.5in by 11in document. So I ended up using:

\documentclass[letterpaper, 10pt]{article}

% \setlength{\textwidth}{7.0in}
% \setlength{\textheight}{9.0in}
% \setlength{\parskip}{0.0in}
% \topmargin -0.5in

You can play with the numbers and see what uncommenting other parts does.

Thats all folks. Happy latexing!

Join Newsletter
Subscribe to receive updates.
Written by Mahesh Follow
I think and read about Technology and the Human Mind. I am currently an ML engineer at Google, but the opinions here are my own and do not reflect that of my employer. Read More »