-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlatex_utilities.R
61 lines (52 loc) · 1.29 KB
/
latex_utilities.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
# Trim specified number of lines from a string (application to generated LaTeX)
LaTeX_trim_lines = function(x, drop_top = 0L, drop_bottom = 0L){
xlines = x |>
str_split("\n")|>
unlist()
if(drop_bottom > 0L){
tlines = length(xlines)
xlines = xlines |>
extract(-((tlines - drop_bottom + 1L):tlines))
}
if(drop_top > 0L){
xlines = xlines |>
extract(-(1:drop_top))
}
xlines |>
paste0(collapse = "\n")
}
# Add rows to a LaTeX table
LaTeX_pad_rows = function(x, n, m){
# Detect how many rows in x
current_rows = str_count(x, pattern = "\\\\\\\\\\n\\\\hline")
if(current_rows == 0L){
# Blank table
paste(c(
rep(
paste(c(
rep(
" &",
times = m - 1L),
"\\\\\n\\hline\n"),
collapse = ""),
times = n)),
collapse = "")
} else if(current_rows |> between(1L, n - 1L)){
# Add empty rows to achieve n rows
# where m is number of columns
paste(c(
x,
rep(
paste(c(
rep(
" &",
times = m - 1L),
"\\\\\n\\hline\n"),
collapse = ""),
times = n - current_rows)),
collapse = "")
} else if(current_rows >= n){
# Return x if already meets minimum row requirement
x
}
}