function to integrate R results into LaTeX-documents

Description

ff reads well constructed LaTeX files, evaluates the expressions found in formfill environments and saves the results in a way that these will be integrated by LaTeX.

Usage

ff(in.file, path="", digits=4, escape.symbol="#")

Arguments

in.file file with text and expressions to be analyzed
path path of the input file
digits digits for rounding numerical values
escape.symbol symbol used as delimiter

Details

ff is a function to integrate R-results into LaTeX-documents.

The first step is to write down the raw text as a LaTeX file. In the (preamble of the) LaTeX-File you have to define a new LaTeX environment by: \newenvironment{formfill}[2]{\input{#1}}

Now you can use this environment for paragraphs that should be filled by the results of evaluated R expressions. For this you write something like: \begin{formfill}{outfilename}{ text ... text ... }\end{formfill}

The second parameter of the environment contains the raw text to be filled by R results. The R function ff scans the lines of the raw text, looks for expressions that are surrounded by two escape.symbols and substitutes the expressions by the results of the evaluations. The modified text of each environment is stored in a file. The name of this file will be defined by the first parameter of the environment. The integration of the new file(s) is (are) performed by the LaTeX process.

Additionally you are allowed to include further statements to define some variables. These statements have to be placed in the second parameter of formfill between a line containing <escape.symbol>: and a line containing :<escape.symbol> and will be evaluated before the code in the text is substituted.

Assuming the default escape.symbol is used the function ff

  • 1) reads the input file
  • 2) extracts the formfill environments of the file,
  • 3) evaluates the lines between #: and :# of the environments,
  • 4) replaces expressions (escaped by two delimiters #...#), by their results,
  • 5) writes the modified text a file for each of the formfill environments with a file name specified by the first parameter of the environment.

    Value

    some messages as well some files (see DETAILS)

    Note

    version: 23.5.2000

    Author(s)

    H.P. Wolf

    References

    available from: http://www.wiwi.uni-bielefeld.de/~wolf/software/R-wtools/formfill/ff.rd

    See Also

    ~~~

    Examples

    ##---- Should be DIRECTLY executable !! ----
    # generate a raw report file:
    cat(file="report.tex",sep="\n",
            "\\documentclass{article}\\parskip2ex",
            "\\begin{document}\\begin{center}",
            "{\\large\\bf Example for using {\\tt formfill}-environment and R-function {\\tt ff}}",
            "\\end{center}",
            "At first we have to define the environment",
            "\\verb+formfill+ (here or in the preamble):",
            "\\newenvironment{formfill}[2]{\\input{#1}}{}",
            "Now we can use it for generating reports automatically.","",
            "\\fbox{\\parbox{10cm}{",
            "\\begin{formfill}{resultA}{",
            "\\sf What is the probability $P$ to get $x=$ &x& heads by tossing a",
            "coin $n=$ &n& times with probability $p=$ &prob& getting a head",
            "in one throw?\\\\",
            "The answer is: $P=$ & dbinom(x,n,prob) & !",
            "&:",
            " x<-3 ",
            " n<-10; prob<-0.5",
            ":&",
            "}\\end{formfill}",
            "}}\\\\ % end of fbox and parbox","",
            "\\rm That's it. We have to start R and call the function {\\tt ff}:\\\\",
            "\\verb+ > ff('report.tex',escape.symbol='&') +\\\\",
            "Then we can format this file by LaTeX:\\\\",
            "\\verb+ latex report.tex + \\\\",
            "and proceed as usual.\\\\","",
            "Now have a look at {\\tt report.tex}!",
            "\\end{document}"
    )
    # substitute expressions
    ff("report.tex",escape.symbol="&")
    # format report with evaluated expressions
    system("latex report.tex")
    
    # end of example
    
    ## The function is currently defined as
    ff <- function (in.file, path = "", digits = 4, escape.symbol = "#")
    {
        cat("processing of in.file ", in.file, " : starts\n")
        if (0 == nchar(path)) {
            ff.h <- unlist(strsplit(in.file, "/"))
            path <- paste(ff.h[-length(ff.h)], collapse = "/")
        }
        else {
            in.file <- paste(path, in.file, sep = "/")
        }
        ff.local <- function(tz, out.file = "tmpout.tex", path = "",
            digits = 4, escape.symbol = "#") {
            cat("processing for   ", out.file, ": starts   \n")
            ff.begin <- grep(paste(escape.symbol, ":", sep = ""),
                tz)
            ff.end <- grep(paste(":", escape.symbol, sep = ""), tz)
            ff.defs.pos <- cbind(ff.begin, ff.end)
            cat("  definition(s) to be evaluated:\n")
            for (ff.i in 1:length(ff.begin)) {
                cat("  from ", ff.begin, "to", ff.end, "\n")
                cat(paste("  ", tz[ff.begin[ff.i]:ff.end[ff.i]],
                    "\n"))
            }
            repeat {
                if (length(ff.begin) == 0)
                    break
                ff.defs <- tz[ff.begin[1]:ff.end[1]]
                ff.begin <- ff.begin[-1]
                ff.end <- ff.end[-1]
                ff.chars <- substring(ff.defs[1], 1:nchar(ff.defs[1]),
                    1:nchar(ff.defs[1]))
                ff.von <- seq(ff.chars)[ff.chars == escape.symbol &
                    c(ff.chars[-1], " ") == ":"]
                ff.defs[1] <- substring(ff.defs[1], ff.von + 2)
                ff.h <- 1:nchar(ff.defs[ff.length <- length(ff.defs)])
                ff.chars <- substring(ff.defs[ff.length], ff.h, ff.h)
                ff.bis <- seq(ff.chars)[c(ff.chars[-1], " ") == escape.symbol &
                    ff.chars == ":"]
                ff.defs[ff.length] <- substring(ff.defs[ff.length],
                    1, ff.bis - 1)
                for (ff.d in ff.defs) {
                    cat("  definition(s) to be evaluated: ", ff.d,
                      "\n")
                    eval(parse(text = ff.d))
                    cat("  evaluation(s) done\n")
                }
            }
            for (ff.h in 1:length(ff.defs.pos[, 1]))
               tz <- tz[-(ff.defs.pos[ff.h,1]:ff.defs.pos[ff.h, 2])]
            cat("definitions for  ", out.file, ": evaluated\n")
            ff.begin.end <- grep(escape.symbol, tz)
            cat("  lines with replacements: \n")
            cat(paste("  ", tz[ff.begin.end], "\n"))
            repeat {
                if (length(ff.begin.end) == 0)
                    break
                ff.repl <- tz[ff.begin.end[1]]
                ff.chars <- substring(ff.repl, 1:nchar(ff.repl),
                    1:nchar(ff.repl))
                ff.found <- T
                ff.positions <- seq(ff.chars)[ff.chars == escape.symbol]
                if (length(ff.positions) < 2)
                    ff.found <- F
                if (ff.chars[ff.positions[1] + 1] == ":")
                    ff.found <- F
                if (0 != length(ff.positions)%%2)
                    ff.found <- F
                if (ff.found) {
                    ff.positions <- matrix(ff.positions, 2)
                    ff.eval <- substring(ff.repl, ff.positions[1,
                      ] + 1, ff.positions[2, ] - 1)
                    ff.out <- NULL
                    for (ff.e in ff.eval) {
                      cat("  expression to be evaluated / replaced:",
                        ff.e, "\n")
                      ff.out <- c(ff.out, eval(parse(text = ff.e)))
                      cat("  evaluation done, result: ", ff.out[length(ff.out)],
                        " \n")
                    }
                    ff.out <- as.character(signif(ff.out, digits))
                    ff.chars <- c("a", ff.chars[0 == cumsum(ff.chars ==
                      escape.symbol)%%2], "z")
                    ff.chars <- paste(ff.chars, collapse = "")
                    ff.chars <- strsplit(ff.chars, escape.symbol)[[1]]
                    ff.chars <- rbind(c(" ", ff.out), ff.chars)[-1]
                    ff.chars <- paste(ff.chars, collapse = "")
                    ff.repl <- substring(ff.chars, 2, nchar(ff.chars) -
                      1)
                    tz[ff.begin.end[1]] <- ff.repl
                    cat("  REPLACED line:", ff.repl, "\n")
                }
                ff.begin.end <- ff.begin.end[-1]
            }
            cat("replacements for ", out.file, ": finished\n")
            if (0 < nchar(path))
                out.file <- paste(path, "/", out.file, sep = "")
            cat(paste(tz, "\n"), file = out.file, sep = "")
            cat("output file      ", out.file, " generated\n")
            return(tz)
        }
        ff.tz <- scan(in.file, "", sep = "\n")
        ff.pos <- grep("\\begin{formfill}", ff.tz)
        if (length(ff.pos) == 0) {
            ff.pos <- matrix(c(1, length(ff.tz)), 1, 2)
            ff.names <- "tmpout.tex"
        }
        else {
            ff.names <- sub("\\begin\{formfill\}", "", ff.tz[ff.pos])
            ff.names <- gsub("{", "", ff.names)
            ff.names <- unlist(strsplit(ff.names, "}"))
            ff.pos <- cbind(ff.pos, grep("\\end{formfill}", ff.tz))
        }
        cat("new file(s):", paste(ff.names, collapse = " "), "\n")
        cat("environments extracted\n")
        for (ff.n in seq(along = ff.names)) {
            ff.local(ff.tz[(ff.pos[ff.n, 1] + 1):(ff.pos[ff.n, 2] -
                1)], out.file = ff.names[ff.n], path = path, digits = digits,
                escape.symbol = escape.symbol)
        }
      }
    

    [Package Contents]