/
latex.R
296 lines (278 loc) · 10.5 KB
/
latex.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#' Convert R Markdown to a PDF book
#'
#' Convert R Markdown files to PDF after resolving the special tokens of
#' \pkg{bookdown} (e.g., the tokens for references and labels) to native LaTeX
#' commands.
#'
#' This function is based on \code{rmarkdown::\link{pdf_document}} (by default)
#' with better default arguments. You can also change the default format to
#' other LaTeX/PDF format functions using the \code{base_format} argument.
#'
#' The global R option \code{bookdown.post.latex} can be set to a function to
#' post-process the LaTeX output. This function takes the character vector of
#' the LaTeX output as its input argument, and should return a character vector
#' to be written to the \file{.tex} output file. This gives you full power to
#' post-process the LaTeX output.
#' @param toc,number_sections,fig_caption,pandoc_args See
#' \code{rmarkdown::\link{pdf_document}}, or the documentation of the
#' \code{base_format} function.
#' @param ... Other arguments to be passed to \code{base_format}.
#' @param base_format An output format function to be used as the base format.
#' @param toc_unnumbered Whether to add unnumberred headers to the table of
#' contents.
#' @param toc_appendix Whether to add the appendix to the table of contents.
#' @param toc_bib Whether to add the bibliography section to the table of
#' contents.
#' @param quote_footer If a character vector of length 2 and the quote footer
#' starts with three dashes (\samp{---}), \code{quote_footer[1]} will be
#' prepended to the footer, and \code{quote_footer[2]} will be appended; if
#' \code{NULL}, the quote footer will not be processed.
#' @param highlight_bw Whether to convert colors for syntax highlighting to
#' black-and-white (grayscale).
#' @note This output format can only be used with \code{\link{render_book}()}.
#' @export
pdf_book = function(
toc = TRUE, number_sections = TRUE, fig_caption = TRUE, pandoc_args = NULL, ...,
base_format = rmarkdown::pdf_document, toc_unnumbered = TRUE,
toc_appendix = FALSE, toc_bib = FALSE, quote_footer = NULL, highlight_bw = FALSE
) {
base_format = get_base_format(base_format)
config = base_format(
toc = toc, number_sections = number_sections, fig_caption = fig_caption,
pandoc_args = pandoc_args2(pandoc_args), ...
)
config$pandoc$ext = '.tex'
post = config$post_processor # in case a post processor have been defined
config$post_processor = function(metadata, input, output, clean, verbose) {
if (is.function(post)) output = post(metadata, input, output, clean, verbose)
f = with_ext(output, '.tex')
x = resolve_refs_latex(read_utf8(f))
x = resolve_ref_links_latex(x)
x = restore_part_latex(x)
x = restore_appendix_latex(x, toc_appendix)
if (!toc_unnumbered) x = remove_toc_items(x)
if (toc_bib) x = add_toc_bib(x)
x = restore_block2(x, !number_sections)
if (!is.null(quote_footer)) {
if (length(quote_footer) != 2 || !is.character(quote_footer)) warning(
"The 'quote_footer' argument should be a character vector of length 2"
) else x = process_quote_latex(x, quote_footer)
}
if (highlight_bw) x = highlight_grayscale_latex(x)
post = getOption('bookdown.post.latex')
if (is.function(post)) x = post(x)
write_utf8(x, f)
tinytex::latexmk(
f, config$pandoc$latex_engine,
if ('--biblatex' %in% config$pandoc$args) 'biber' else 'bibtex'
)
output = with_ext(output, '.pdf')
o = opts$get('output_dir')
keep_tex = isTRUE(config$pandoc$keep_tex)
if (!keep_tex) file.remove(f)
if (is.null(o)) return(output)
output2 = file.path(o, output)
file.rename(output, output2)
if (keep_tex) file.rename(f, file.path(o, f))
output2
}
# always enable tables (use packages booktabs, longtable, ...)
pre = config$pre_processor
config$pre_processor = function(...) {
c(if (is.function(pre)) pre(...), '--variable', 'tables=yes', '--standalone')
}
config$bookdown_output_format = 'latex'
config = set_opts_knit(config)
config
}
#' @rdname html_document2
#' @export
pdf_document2 = function(...) {
pdf_book(..., base_format = rmarkdown::pdf_document)
}
#' @rdname html_document2
#' @export
tufte_handout2 = function(...) {
pdf_book(..., base_format = tufte::tufte_handout)
}
#' @rdname html_document2
#' @export
tufte_book2 = function(...) {
pdf_book(..., base_format = tufte::tufte_book)
}
resolve_refs_latex = function(x) {
# equation references \eqref{}
x = gsub(
'(?<!\\\\textbackslash{})@ref\\((eq:[-/:[:alnum:]]+)\\)', '\\\\eqref{\\1}', x,
perl = TRUE
)
# normal references \ref{}
x = gsub(
'(?<!\\\\textbackslash{})@ref\\(([-/:[:alnum:]]+)\\)', '\\\\ref{\\1}', x,
perl = TRUE
)
x = gsub(sprintf('\\(\\\\#((%s):[-/[:alnum:]]+)\\)', reg_label_types), '\\\\label{\\1}', x)
x
}
resolve_ref_links_latex = function(x) {
res = parse_ref_links(x, '^%s (.+)$')
if (is.null(res)) return(x)
x = res$content; txts = res$txts; i = res$matches
# text for a tag may be wrapped into multiple lines; collect them until the
# empty line
for (j in seq_along(i)) {
k = 1
while (x[i[j] + k] != '') {
txts[j] = paste(txts[j], x[i[j] + k], sep = '\n')
x[i[j] + k] = ''
k = k + 1
}
}
restore_ref_links(x, '(?<!\\\\texttt{)%s', res$tags, txts, FALSE)
}
restore_part_latex = function(x) {
r = '^\\\\(chapter|section)\\*\\{\\(PART(\\*)?\\)( |$)'
i = grep(r, x)
if (length(i) == 0) return(x)
x[i] = gsub(r, '\\\\part\\2{', x[i])
# remove (PART*) from the TOC lines for unnumbered parts
r = '^(\\\\addcontentsline\\{toc\\}\\{)(chapter|section)(\\}\\{)\\(PART\\*\\)( |$)'
x = gsub(r, '\\1part\\3', x)
# for numbered parts, remove the line \addcontentsline since it is not really
# a chapter title and should not be added to TOC
j = grep('^\\\\addcontentsline\\{toc\\}\\{(chapter|section)\\}\\{\\(PART\\)( |$)', x)
k = j; n = length(x)
for (i in seq_along(j)) {
# figure out how many lines \addcontentsline{toc} spans over (search until
# it finds an empty line)
l = 1
while (j[i] + l <= n && x[j[i] + l] != '') {
k = c(k, j[i] + l)
l = l + 1
}
}
if (length(k)) x = x[-k]
x
}
restore_appendix_latex = function(x, toc = FALSE) {
r = '^\\\\(chapter|section)\\*\\{\\(APPENDIX\\) .*'
i = find_appendix_line(r, x)
if (length(i) == 0) return(x)
level = gsub(r, '\\1', x[i])
brace = grepl('}}$', x[i])
x[i] = '\\appendix'
if (toc) x[i] = paste(
x[i], sprintf('\\addcontentsline{toc}{%s}{\\appendixname}', level)
)
if (brace) x[i] = paste0(x[i], '}') # pandoc 2.0
if (grepl('^\\\\addcontentsline', x[i + 1])) x[i + 1] = ''
x
}
find_appendix_line = function(r, x) {
i = grep(r, x)
if (length(i) > 1) stop('You must not have more than one appendix title')
i
}
remove_toc_items = function(x) {
r = '^\\\\addcontentsline\\{toc\\}\\{(part|chapter|section|subsection|subsubsection)\\}\\{.+\\}$'
x[grep(r, x)] = ''
x
}
add_toc_bib = function(x) {
r = '^\\\\bibliography\\{.+\\}$'
i = grep(r, x)
if (length(i) == 0) return(x)
i = i[1]
level = if (length(grep('^\\\\chapter\\*?\\{', x))) 'chapter' else 'section'
x[i] = sprintf('%s\n\\addcontentsline{toc}{%s}{\\bibname}', x[i], level)
x
}
restore_block2 = function(x, global = FALSE) {
i = grep('^\\\\begin\\{document\\}', x)[1]
if (is.na(i)) return(x)
if (length(grep('\\\\(Begin|End)KnitrBlock', tail(x, -i))))
x = append(x, '\\let\\BeginKnitrBlock\\begin \\let\\EndKnitrBlock\\end', i - 1)
if (length(grep(sprintf('^\\\\BeginKnitrBlock\\{(%s)\\}', paste(all_math_env, collapse = '|')), x)) &&
length(grep('^\\s*\\\\newtheorem\\{theorem\\}', head(x, i))) == 0) {
theorem_defs = sprintf(
'%s\\newtheorem{%s}{%s}%s', theorem_style(names(theorem_abbr)), names(theorem_abbr),
str_trim(vapply(theorem_abbr, label_prefix, character(1), USE.NAMES = FALSE)),
if (global) '' else {
if (length(grep('^\\\\chapter[*]?', x))) '[chapter]' else '[section]'
}
)
# the proof environment has already been defined by amsthm
proof_envs = setdiff(names(label_names_math2), 'proof')
proof_defs = sprintf(
'%s\\newtheorem*{%s}{%s}', theorem_style(proof_envs), proof_envs,
gsub('^\\s+|[.]\\s*$', '', vapply(proof_envs, label_prefix, character(1), label_names_math2))
)
x = append(x, c('\\usepackage{amsthm}', theorem_defs, proof_defs), i - 1)
}
# remove the empty lines around the block2 environments
i3 = if (length(i1 <- grep('^\\\\BeginKnitrBlock\\{', x))) (i1 + 1)[x[i1 + 1] == '']
i3 = c(i3, if (length(i2 <- grep('^\\\\EndKnitrBlock\\{', x))) (i2 - 1)[x[i2 - 1] == ''])
if (length(i3)) x = x[-i3]
r = '^(.*\\\\BeginKnitrBlock\\{[^}]+\\})(\\\\iffalse\\{-)([-0-9]+)(-\\}\\\\fi\\{\\})(.*)$'
if (length(i <- grep(r, x)) == 0) return(x)
opts = sapply(strsplit(gsub(r, '\\3', x[i]), '-'), function(z) {
intToUtf8(as.integer(z))
}, USE.NAMES = FALSE)
x[i] = paste0(gsub(r, '\\1', x[i]), opts, gsub(r, '\\5', x[i]))
x
}
style_definition = c('definition', 'example', 'exercise')
style_remark = c('remark')
# which styles of theorem environments to use
theorem_style = function(env) {
styles = character(length(env))
styles[env %in% style_definition] = '\\theoremstyle{definition}\n'
styles[env %in% style_remark] = '\\theoremstyle{remark}\n'
styles
}
process_quote_latex = function(x, commands) {
for (i in grep('^\\\\end\\{quote\\}$', x)) {
i1 = NULL; i2 = i - 1
k = 1
while (k < i) {
xk = x[i - k]
if (grepl('^---.+', xk)) {
i1 = i - k
break
}
if (xk == '' || grepl('^\\\\begin', xk)) break
k = k + 1
}
if (is.null(i1)) next
x[i1] = paste0(commands[1], x[i1])
x[i2] = paste0(x[i2], commands[2])
}
x
}
# \newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}
# \newcommand{\KeywordTok}[1]{\textcolor[rgb]{x.xx,x.xx,x.xx}{\textbf{{#1}}}}
# \newcommand{\DataTypeTok}[1]{\textcolor[rgb]{x.xx,x.xx,x.xx}{{#1}}}
# ...
highlight_grayscale_latex = function(x) {
i1 = grep('^\\\\newenvironment\\{Shaded\\}', x)
if (length(i1) == 0) return(x)
i1 = i1[1]
r1 = '^\\\\newcommand\\{\\\\[a-zA-Z]+\\}\\[1]\\{.*\\{#1\\}.*\\}$'
r2 = '^(.*?)([.0-9]+,[.0-9]+,[.0-9]+)(.*)$'
i = i1 + 1
while (grepl('^\\\\newcommand\\{.+\\}$', x[i])) {
if (grepl(r1, x[i]) && grepl(r2, x[i])) {
col = as.numeric(strsplit(gsub(r2, '\\2', x[i]), ',')[[1]])
x[i] = gsub(
r2, paste0('\\1', paste(round(rgb2gray(col), 2), collapse = ','), '\\3'),
x[i]
)
}
i = i + 1
}
x
}
# https://en.wikipedia.org/wiki/Grayscale
rgb2gray = function(x, maxColorValue = 1) {
rep(sum(c(.2126, .7152, .0722) * x/maxColorValue), 3)
}