This repository has been archived by the owner on Sep 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathsanitizeTexString.R
84 lines (80 loc) · 2.73 KB
/
sanitizeTexString.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#' Replace LaTeX Special Characters in a String
#' This function is used by tikzDevice when \code{sanitize=TRUE} to replace
#' special LaTeX characters (such as the comment character %) in plotting text
#' where the user does not have direct control over the generated text.
#'
#' \code{sanitizeTexString} searches character by character through a string
#' replacing each occurrence of a special character contained in
#' \code{strip[i]} with the corresponding replacement value in
#' \code{replacement[i]}. tikzDevice calls back this function for every piece
#' of text when the sanitize option is TRUE. See \code{\link{tikz}} for more
#' information on the default special characters and replacement values.
#'
#' By default, `tikzSanitizeCharacters` replaces the following characters:
#'
#' \itemize{
#' \item \verb{\%}
#' \item \verb{$}
#' \item \verb{\}}
#' \item \verb{\{}
#' \item \verb{^}
#' \item \verb{_}
#' \item \verb{#}
#' \item \verb{&}
#' \item \verb{~}
#' }
#'
#' With the contents of `tikzReplacementCharacters`:
#'
#' \itemize{
#' \item \verb{\\\%}
#' \item \verb{\\$}
#' \item \verb{\\\}}
#' \item \verb{\\\{}
#' \item \verb{\\^{}}
#' \item \verb{\\_{}}
#' \item \verb{\\#}
#' \item \verb{\\&}
#' \item \verb{\char`\~}
#' }
#'
#' These defaults may be adjusted using the \code{\link{options}} function.
#'
#' @param string A character vector of length 1 (a string).
#' @param strip A character vector of single characters to search for.
#' @param replacement A character vector of replacement values.
#'
#' @return \item{sanitizedString}{A character vector of length 1 with all
#' special characters replaced.}
#'
#' @author Cameron Bracken \email{cameron.bracken@@gmail.com}
#'
#' @seealso \code{\link{tikz}}
#' @keywords character
#'
#' @examples
#'
#' # Be careful with sanitizing, it may lead to unexpected behavior.
#' # For example, we may want -1 to be a superscript it gets
#' # sanitized away with the other default special characters.
#' # The string appears in LaTeX exactly as shown.
#' \dontrun{
#' sanitizeTexString('10\% of 10$ is 10^\{-1\}$')
#' }
#'
#' @export
sanitizeTexString <- function(string,
strip = getOption('tikzSanitizeCharacters'),
replacement = getOption('tikzReplacementCharacters')){
#separate the string into a vector of charaters
explode <- strsplit(string,'')[[1]]
if(any(is.na(explode))) stop("Unable to sanitize string, you may be trying to pass in an unsupported symbol")
# Replace each matching character with its replacement characters
for(i in 1:length(explode)){
matches <- (explode[i] == strip)
if(any(matches))
explode[i] <- paste('{',replacement[which(matches)],'}',sep='')
}
# stick the string back together
return(paste(explode,collapse=''))
}