s-news
[Top] [All Lists]

[S] LaTeX equations in S-PLUS plots

To: S-news@wubios.wustl.edu
Subject: [S] LaTeX equations in S-PLUS plots
From: Greg Snow <snow@statsci.com>
Date: Thu, 13 Jan 2000 13:55:10 -0800 (PST)
Sender: owner-s-news@wubios.wustl.edu

People on this list (and off) have asked for ways to include equations in
plots that are more complicated than can be done with the text command,
i.e. subscripts, superscripts, greek letters.  Below is a function that I
put together as one approach to this problem.

Disclaimer:  This function is NOT the MathSoft response to the problem, it
is NOT supported by MathSoft, and while I am an employee of MathSoft, this
function represents my work (play), and therefore MathSoft should not get
the blame for my mistakes. I am not one of the developers. 

All questions/comments should be sent directly to me (snow@statsci.com),
tech support is finding out about this the same way you are (and can walk
accross the hall and yell at me if you plague them to much :-).

While this is not officially supported, I will try to correct any major
blunders that get pointed out to me.  

This soulution is more of a workaround than a true solution, it actually
calls LaTeX to do the typesetting (so you have all the functionallity of
LaTeX, more than just greek letters and subscripts), then uses ImageMagick
to convert the postscript file to something that can be read in and put
into the graph by S-PLUS.  This does mean that you will need to have LaTeX
and ImageMagick installed on your computer (many Linux systems come with
them allready installed, and they are freely available for other
platforms).

A simple example of using the function:

plot(1:10, 1:10)
tmp <- latext("\\int_0^\\infty \\beta \\times x")  # click on plot 

if you want to insert the same equation/notation again (you redid the
plot), you can now do:

plot(10:1, 1:10)
latext(tmp) # click on plot to position

The equation appears in the plot on the screen and when exported or
printed (in my tests it tends to look a little better on paper than on the
screen).

There are options that allow you to: 
* specify the coordinates to insert the equation
* make the equation larger or smaller
* include a background rectangle or not
* choose which LaTeX math mode to use
* do black on white or white on black
* specify a postscript file to use instead of a LaTeX command
* pass additional information to ImageMagick to try and iprove the quality
of the included equation.

The version included below is for Unix versions, to convert it to Windows
versions: find all occurances of "unix" and replace them with "system"
(you may also want to change the default value of invert to F).

You should also set the image colors on your graphics device to shades of
grey or it may look funny.

I hope some of you find this useful,

-------------------------------------------------------------------------------
     Gregory L. Snow         |  Inertia makes the world go round,
     (Greg)                  |   Love makes the trip worth taking.
     snow@statsci.com        |
-------------------------------------------------------------------------------


Cut the following line and above and source into S-PLUS
===============================cut===================================

latext <- 
# This function allows you to add arbitrary LaTeX math formatting to an
# existing S-PLUS graph.
#
#  Note:  things will look best if the image colors on your graphics device
#         are set to greyscale rather than color.
#
# The arguments are:
#   text: The LaTeX string to include, i.e. "\int_0^\infty \beta x", or
#                 a matrix that has been returned from a previous call.
#   x,y:  The x and y coordinates to place the "text".
#   cex:  Character expansion, how big to make the "text", 1 means 10pt.
#   background:  Should an enclosing rectangle of the background color 
#                  (white) be plotted also.
#   inline:  If true uses LaTeX in-line mode (shorter output).
#   density:  Higher values give better quality but take more time and memory.
#   imparams:  Additional parameters to be passed to imagemagick.
#   invert:  White text on black background if T, black on white if F.
#   ps.file:  Postscript file to use instead of creating one from LaTeX.
#   ... :     Additional parameters to be passed to subplot function.
#
# You must have LaTeX and Imagemagick installed on the computer.
#
# the url for information and copies of LaTeX is:
# http://www.tug.org/
#
# The url for information and copies of imagemagick is:
# http://www.wizards.dupont.com/cristy/ImageMagick.html
#
# This function was written by Greg Snow (snow@statsci.com)
# This function is NOT supported by MathSoft and does not carry
# warrenties of any kind.  You are free to copy and use as you wish.
function(text=NULL, x = locator(1), y, cex = par("cex"), background = F, 
          inline = F, density = 1, imparams = "", invert=T, 
          ps.file = NULL, ...)
{
  if(is.matrix(text)) {
    text.mat <- text
  }
  else {
    if ( is.null(ps.file) ){
      # make LaTeX File
      sink("tempeq.tex")
      cat("\\documentclass{article}\n\\begin{document}\n")
      cat("\\thispagestyle{empty}\n\n")
      if(inline) {
          cat("$")
      }
      else {
          cat("\\[\n")
      }
      cat(text)
      if(inline) {
          cat("$")
      }
      else {
          cat("\n\\]")
      }
      cat("\n\n\\end{document}\n")
      sink()

      # convert to postscript
      unix("latex tempeq")
      unix("dvips tempeq -o")
      ps.file="tempeq.ps"
    }
    # convert to text file
    unix(paste("convert -density", round(cex * 72 * density), 
               imparams, "-crop 0x0+1+1", ps.file, "txt:tempeq.txt"))
    # read into S-PLUS
    lines <- scan("tempeq.txt", what = "", sep = "\n")
    # extract row info
    rows <- as.numeric(substring(lines, 1, regexpr(",", lines) - 1))
    # extract red component
    re.info <- regexpr(" [0-9]+", lines)
    red <- as.numeric(substring(lines, re.info + 1, re.info +
                                  attr(re.info, "match.length") - 1))
    nrows <- length(unique(rows))
    text.mat <- matrix(red, nrow = nrows)
    ncols <- dim(text.mat)[2]
    text.mat <- text.mat[, ncols:1]
  }
  text.mat2 <- text.mat
  if(!background) {
    text.mat2[text.mat2 > 240] <- NA
  }
  if( invert ){
    text.mat2 <- 255-text.mat2
  }
  size <- dim(text.mat2)/(72 * density)
  subplot(image(text.mat2, axes = F, xlab = "", ylab = "", 
                  zlim = c(0,255)), x, y, size, ...)
  invisible(text.mat)
}

-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu.  To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message:  unsubscribe s-news

<Prev in Thread] Current Thread [Next in Thread>
  • [S] LaTeX equations in S-PLUS plots, Greg Snow <=