#' Loop through figure files (e.g. pdf) in a folder and generate LaTeX code to make a beamer slide for each figure.
#' This function is developed based on the folder structure generated by the fishgraph package
#'
#' @param dir1 directory containing subdirectories (folders) of figures (e.g. spp directory produced by fishgraph)
#' @param folders names of folders of interest within dir1
#' @param dir2 optional second directory, analogous to dir1. If provided, the function will generate text code to produce two column slides for comparing analogous figures (e.g. from two runs of the same model)
#' @param ext file extension of figues in subdirectories
#' @param label1 text label for slides associated with dir1
#' @param label2 text label for slides associated with dir1
#' @param height figure height in cm, passed to the height argument of the latex command includegraphics
#' @param dir_prefix annoying prefix on folder names to be removed with gsub (e.g. figs)
#' @param file_root root names (i.e. without ext) of figures to plot
#' @param hypertargetText Character vector of names for creating hyperlinks in slides (I'll be more specific when I remember what this does!)
#' @keywords bam stock assessment fisheries
#' @author Nikolai Klibansky
#' @export
#' @examples
#' \dontrun{
#' }
figs2tex <- function(dir1,
folders,
dir2=NULL,
ext="pdf",
label1=NULL,
label2=NULL,
height=NULL,
dir_prefix=NULL,
file_root=NULL,
hypertargetText=""
)
{
folderPath <- file.path(dir1,folders)
filenames <- list.files(folderPath) # file names should be the same for folderPath and analogous folder for dir2
filenames <- filenames[substr(filenames,start=nchar(filenames)-3,stop=nchar(filenames))==".pdf"] # Only include pdf files
if(is.null(file_root)){
file_root <- gsub(x=filenames,pattern=paste0(".",ext),replacement = "",fixed=TRUE)
}
dir1Label <- dir1
if(!is.null(dir_prefix)){
dir1Label <- gsub(x=dir1,pattern=dir_prefix,replacement="")
}
if(is.null(label1)){
label1 <- dir1Label
}
# Print one column slides
for(filename.i in file_root){
# page <- page+1
if(is.null(dir2)){
if(is.null(height)){
height <- "8"
}
cat(c(paste("## ",folders,": ", filename.i ,"\n",sep=""),
hypertargetText,
"\\begin{center}\n",
paste("\\includegraphics[height=",height,"cm]{{",file.path(dir1,folders,filename.i),"}.",
ext,"}\n",sep=""),
"\\end{center}\n")
)
# Print two column slides
}else{
dir2Label <- dir2
if(!is.null(dir_prefix)){
dir2Label <- gsub(x=dir2,pattern=dir_prefix,replacement="")
}
if(is.null(label2)){
label2 <- dir2Label
}
if(is.null(height)){
height <- "4.25"
}
if(file.exists(file.path(dir1,folders,paste0(filename.i,".",ext)))){
txt_ig_1 <- paste("\\includegraphics[height=",height,"cm]{{",file.path(dir1,folders,filename.i),"}.",ext,"}\n",sep="")
}else{
txt_ig_1 <- "\nfigure not found\n"#paste0("{",file.path(dir1,folders,paste0(filename.i,".",ext)),"not found}")
}
if(file.exists(file.path(dir2,folders,paste0(filename.i,".",ext)))){
txt_ig_2 <- paste("\\includegraphics[height=",height,"cm]{{",file.path(dir2,folders,filename.i),"}.",ext,"}\n",sep="")
}else{
txt_ig_2 <- "\nfigure not found\n"#paste0("{",file.path(dir2,folders,paste0(filename.i,".",ext)),"not found}")
}
cat(c(paste("## ",folders,": ", filename.i ,"\n",sep=""),
hypertargetText,
"\\begin{columns}\n
\\column{0.5\\textwidth}\n
\\begin{center}\n",
label1,"\n",
txt_ig_1,
"\\end{center}\n
\\column{0.5\\textwidth}\n
\\begin{center}\n",
label2,"\n",
txt_ig_2,
"\\end{center}\n
\\end{columns}\n")
)
}
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.