You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
106 lines
4.2 KiB
R
106 lines
4.2 KiB
R
8 years ago
|
#' Generate LaTeX subfigure code
|
||
|
#'
|
||
|
#' Generate LaTeX subfigure code for a bunch of supplied image paths,
|
||
|
#' subcaptions, label and subfigure layout.
|
||
|
#' Supports splitting the figures over several pages or using landscape layout.
|
||
|
#'
|
||
|
#' @param images vector with full paths to images (png-files or other LaTeX-compatible format)
|
||
|
#' to be put in a LaTeX subfigure environment
|
||
|
#' @param subcaptions vector with subcaptions for each subfigure
|
||
|
#' @param mainlabel string with LaTeX label for the main figure environment
|
||
|
#' should be set individually if SubfigureGenerator() is called more than once from the same document
|
||
|
#' @param perpage maximum number of images on one page, one A4 page fits six images with subcaptions
|
||
|
#' @param ncol LaTeX subfigure is setup with ncol columns
|
||
|
#' @param landscape set this to TRUE if pages are set in landscape mode
|
||
|
#'
|
||
|
#' @return a string with LaTeX code
|
||
|
#' @export
|
||
|
SubfigureGenerator <- function(images,
|
||
|
subcaptions,
|
||
13 years ago
|
mainlabel = "fig:mainfig",
|
||
8 years ago
|
perpage = 6,
|
||
|
ncol = 2,
|
||
13 years ago
|
landscape = FALSE) {
|
||
8 years ago
|
# Collect all LaTeX code in a textconnection
|
||
13 years ago
|
# that's dumped to a vector before return
|
||
|
zzstring <- ""
|
||
|
zz <- textConnection("zzstring", "w")
|
||
8 years ago
|
|
||
13 years ago
|
# If landscape is TRUE, set pagewidth to \textheight
|
||
8 years ago
|
# pagewidth <- ifelse(landscape == TRUE, "\\textheight", "\\textwidth")
|
||
13 years ago
|
pagewidth <- "\\textwidth"
|
||
8 years ago
|
|
||
13 years ago
|
# Check that the vector of images is non-empty
|
||
|
if (length(images) > 0) {
|
||
|
# keep track of the number of pages the images are split across
|
||
|
page.counter <- 1
|
||
|
# Calculate width of subfigure based on ncol-value
|
||
|
subfigure.width <- 1 / ncol - 0.02
|
||
8 years ago
|
|
||
13 years ago
|
# begin figure
|
||
|
if (landscape == TRUE) {
|
||
|
cat("\\begin{sidewaysfigure}\\centering\n", file = zz)
|
||
|
} else {
|
||
8 years ago
|
cat("\\begin{figure}[hb]\\centering\n", file = zz)
|
||
13 years ago
|
}
|
||
8 years ago
|
|
||
11 years ago
|
# display images in a X-by-Y grid
|
||
13 years ago
|
for (i in 1:length(images)) {
|
||
8 years ago
|
cat(paste("\\begin{subfigure}[b]{", round(subfigure.width, 2),
|
||
13 years ago
|
pagewidth, "}\\centering\n", sep = ""), file = zz)
|
||
|
# this includes the i-th image in a subfigure
|
||
8 years ago
|
cat(paste("\\includegraphics[width=\\linewidth]{",
|
||
13 years ago
|
images[i], "}\n", sep = ""), file = zz)
|
||
8 years ago
|
cat(paste("\\caption{", subcaptions[i],
|
||
13 years ago
|
"}\n", sep = ""), file = zz)
|
||
8 years ago
|
cat(paste("\\label{", mainlabel, ":sfig-", int2padstr(ii = i, pchr = "0", w = 3),
|
||
13 years ago
|
"}\n", sep = ""), file = zz)
|
||
|
cat("\\end{subfigure}", file = zz)
|
||
|
#
|
||
|
if (!(i %% (perpage)) && length(images) != (perpage*page.counter)) {
|
||
|
cat("\\caption{Main figure caption.}\n", file = zz)
|
||
8 years ago
|
cat(paste("\\label{", mainlabel, "-",
|
||
|
int2padstr(ii = page.counter, pchr = "0", w = 3),
|
||
13 years ago
|
"}\n", sep = ""), file = zz)
|
||
|
if (landscape == TRUE) {
|
||
|
cat("\\end{sidewaysfigure}\n", file = zz)
|
||
|
} else {
|
||
|
cat("\\end{figure}\n", file = zz)
|
||
|
}
|
||
|
cat("\n", file = zz)
|
||
|
if (landscape == TRUE) {
|
||
|
cat("\\begin{sidewaysfigure}\\centering\n", file = zz)
|
||
|
} else {
|
||
|
cat("\\begin{figure}[tb]\\centering\n", file = zz)
|
||
|
}
|
||
|
# Step-up page.counter
|
||
|
page.counter <- page.counter + 1
|
||
|
} else {
|
||
|
if (i %% ncol) {
|
||
|
# odd number -- add inter-column space
|
||
|
cat("\\,\n", file = zz)
|
||
|
}
|
||
|
if (!(i %% ncol)) {
|
||
|
# even number -- add newline and some vspace
|
||
|
cat("\\\\[6pt]\n", file = zz)
|
||
|
}
|
||
8 years ago
|
}
|
||
13 years ago
|
}
|
||
8 years ago
|
#
|
||
13 years ago
|
# end figure
|
||
|
cat("\\caption{Main caption.}\n", file = zz)
|
||
13 years ago
|
cat(paste("\\label{", mainlabel, "-",
|
||
8 years ago
|
int2padstr(ii= page.counter, pchr = "0", w = 3),
|
||
13 years ago
|
"}\n", sep = ""), file = zz)
|
||
|
if (landscape == TRUE) {
|
||
|
cat("\\end{sidewaysfigure}\n", file = zz)
|
||
|
} else {
|
||
|
cat("\\end{figure}\n", file = zz)
|
||
|
}
|
||
|
}
|
||
8 years ago
|
|
||
13 years ago
|
zzstring <- textConnectionValue(zz)
|
||
|
close(zz)
|
||
|
return(zzstring)
|
||
|
}
|