psfrag: Inserting LaTeX Code Inside Graphics

There are many approaches for inserting LaTeX equations, symbols, tables, and other output inside postscript and pdf graphics. Insertion of multi-column tables with proper alignment is a frequently needed feature. The psfrag approach is one of the most general. It can be used more simply than what is described below, but that simple approach only works for latex, not for pdflatex. Here is a general approach.
  • Download from and save it in ~/bin and chmod a+x
  • Create a postscript graphics file such as foo_fm.eps and an accompanying LaTeX command file foo_fm
  • Run foo to create self-contained foo.eps and foo.pdf which can be inserted into LaTeX documents the usual way

Here is an example using R.

postscript('psfrag2_fm.eps', pointsize=12,
           onefile=FALSE, paper='special',
           horizontal=FALSE, height=4, width=5)
par(mar=c(3, 3.25, .25, .5), lwd=1.5, mgp = c(2, 0.45, 0), tcl = -0.4)

x <- seq(0,15,length=100)
plot(x, dchisq(x, 5), ylab='fx', type='l')   # fx will be substituted by $f(x)$
text(10, .13, 'ww', adj=0)  # LaTeX table will be put at location of ww in graph    

x <- cbind(Age=format(c(23.0,9.7)), Sex=c('Male','Female'))
tab <- latexTabular(x, align='rl')  # new function in Hmisc

cat('\\psfrag{ww}{\\small', tab, '}',
    sep='\n', file='psfrag2_fm')

system(' psfrag2')

The resulting pdf file is attached.

NOTE: If you install psfrag you may need to
  1. sudo cp -p /usr/share/texmf/tex/latex/psfrag/ /usr/share/texmf-texlive/dvips/base
  2. sudo apt-get install texlive-extra-utils
Topic attachments
I Attachment Action Size Date Who Comment
psfrag2.pdfpdf psfrag2.pdf manage 6.3 K 17 Aug 2007 - 15:07 FrankHarrell Example output of psfrag with LaTeX table inserted
Topic revision: r6 - 20 Mar 2009, FrankHarrell

This site is powered by FoswikiCopyright © 2013-2017 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Vanderbilt Biostatistics Wiki? Send feedback