R/latex.R

#' Beamer TeX Generation
#'
#' @param texName Name of TeX file to output (overwrites existing)
#' @param saveText Name of figures to include
makeTeX <- function(texName, saveText) {

    ## This will not be very clean

    fileCon <- file(texName, 'w')

    header <- "
\\documentclass[aspectratio=1610]{beamer}

\\mode<presentation> {

%%\\usetheme[RootFont]{Root}
\\usetheme{Root}

\\usepackage{graphicx} %% Allows including images
\\usepackage{media9} %% Allows the use of \\toprule, \\midrule and \\bottomrule in tables
\\usepackage{lmodern} %% Enables additional fonts to remove 2 warnings
\\usepackage{amsmath, graphicx, natbib, multirow}

%%----------------------------------------------------------------------------------------
%%	TITLE PAGE
%%----------------------------------------------------------------------------------------

\\title{RootMetrics Beamer Template}

\\author{Fast Analytics} %% Your name

\\date{\\today} %% Date, can be changed to a custom date

%%-----------------
%%	Macros
%%-----------------

\\newcommand{\\PlotFrameB}[2]{%%
\\begin{center}
\\includegraphics[scale=#1]{#2}
\\end{center}\\endgroup}

\\def\\PlotFrame{\\begingroup
\\catcode`\\_=12
\\PlotFrameB}

%%--------------
%%	Begin
%%--------------

\\begin{document}

\\begin{frame}
\\titlepage %% Print the title page as the first slide
\\end{frame}

%%----------------------------------------------------------------------------------------
%%	Internal Section Heading
%%----------------------------------------------------------------------------------------
"

    makeFrame <- function(plotName) {

        cleanName <- gsub("_", "-", plotName)
        cleanName <- gsub("&", "\\\\&", cleanName)

        frame <- sprintf("
\\begin{frame}
    \\frametitle{%s}
    \\PlotFrame{0.35}{../%s}
\\end{frame}
", cleanName, plotName)

        return(frame)

    }

    plotFrames <- vapply(saveText, makeFrame, character(1))

    footer <- "
} \\end{document}
"

    writeLines(header, fileCon)
    writeLines(plotFrames, fileCon)
    writeLines(footer, fileCon)

    close(fileCon)

    return(NULL)

}

#' Compile TeX Document
#'
#' @param texName Name of TeX file, assuemed to be in a subfolder
compile <- function(texName) {

    texName <- strsplit(texName, "/|[\\]")[[1]]
    directory <- paste(texName[1 : (length(texName) - 1)], collapse = "/")
    texName <- texName[length(texName)]

    ## Move into TeX directory
    oldWD <- setwd(directory)
    on.exit(setwd(oldWD))

    ## Compile
    system(sprintf("latexmk -pdf -quiet %s && latexmk -c -quiet %s", texName, texName))

    return(NULL)

}
mlhutchins/fast-analytics documentation built on May 23, 2019, 2:10 a.m.