Skip to content

Commit bcd51ac

Browse files
committed
enhance the formatting of inline numbers ($ $ can be automatically added to numbers in scientific notations)
1 parent 6a9a912 commit bcd51ac

File tree

7 files changed

+301
-77
lines changed

7 files changed

+301
-77
lines changed

NEWS.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44

55
- Changed the meaning of the chunk option `order`: previously, higher values indicate earlier execution; now higher values indicate later execution. This is a breaking change, but the new meaning should feel more natural. For example, `order = i` means to execute the chunk in the i-th step, and `order = i - 1.5` means to move the chunk back 1.5 step in the queue so it will be executed earlier than its previous chunk. See https://yihui.org/litedown/#sec:option-order for details.
66

7+
- Added options `dollar`, `signif`, and `power` to format numbers from inline code. See https://yihui.org/litedown/#sec:inline-code for details.
8+
79
- Empty table headers are removed in HTML output (they may be generated from data frames or matrices without column names).
810

911
- Added support for the chunk option `collapse = TRUE` (thanks, @J-Moravec, #40).
1012

1113
- Added support for the chunk option `fig.dim`, which is a shortcut for `fig.width` and `fig.height`.
1214

13-
- Fixed a bug that cross-references to other chapters of a book could not be resolved when previewing a singe chapter.
15+
- Fixed a bug that cross-references to other chapters of a book could not be resolved when previewing a single chapter.
1416

1517
- Fixed a bug that the file navigation by line numbers on code blocks stopped working in `litedown::roam()` due to yihui/lite.js@5e06d19.
1618

R/fuse.R

+32-22
Original file line numberDiff line numberDiff line change
@@ -163,19 +163,23 @@ crack = function(input, text = NULL) {
163163
x2[N] = gsub('^`+', '', x2[N]) # leading ` of last
164164
# ` at both ends for text in the middle
165165
if (N > 2) x2[2:(N - 1)] = gsub('^`+|`+$', '', x2[2:(N - 1)])
166+
# see if the code is wrapped in $ $
167+
d1 = substring(x2, nchar(x2), nchar(x2)) == '$'
168+
d2 = substring(x2, 1, 1) == '$'
169+
dollar = d1[-N] & d2[-1]
166170
# position of code c(row1, col1, row2, col2)
167171
pos = matrix(b$pos, nrow = 4)
168-
x = head(c(rbind(x2, c(x1, ''))), -1)
169-
x = lapply(seq_along(x), function(i) {
170-
z = x[i]
171-
if (i %% 2 == 1) return(z)
172-
z = match_one(z, rx_inline)[[1]][-1]
173-
p2 = pos[, i / 2]; save_pos(p2)
174-
list(
172+
x = as.list(head(c(rbind(x2, c(x1, ''))), -1))
173+
for (i in seq_len(N - 1)) {
174+
z = match_one(x1[i], rx_inline)[[1]][-1]
175+
p2 = pos[, i]; save_pos(p2)
176+
xi = list(
175177
source = z[2], pos = p2,
176178
options = csv_options(gsub('^([^,]+)', 'engine="\\1"', z[1]))
177179
)
178-
})
180+
if (dollar[i]) xi$math = TRUE
181+
x[[2 * i]] = xi
182+
}
179183
b$source = x
180184
} else {
181185
b$source = paste(b$source, collapse = '\n')
@@ -888,22 +892,27 @@ exec_inline = function(x) {
888892
}
889893
}
890894

891-
fmt_inline = function(x) {
892-
if (is.numeric(x) && length(x) == 1 && !inherits(x, 'AsIs')) sci_num(x) else as.character(x)
895+
fmt_inline = function(x, ...) {
896+
if (is.numeric(x) && length(x) == 1 && !inherits(x, 'AsIs'))
897+
sci_num(x, ...) else as.character(x)
893898
}
894899

895900
# change scientific notation to LaTeX math
896-
sci_num = function(x) {
897-
s = getOption('litedown.inline.signif', 3)
898-
p = getOption('litedown.inline.power', 6)
901+
sci_num = function(x, math = NULL) {
902+
opts = reactor()
903+
s = x != 0 && abs(log10(abs(x))) >= opts$power
904+
x = format(signif(x, opts$signif), scientific = s)
899905
r = '^(-)?([0-9.]+)e([-+])0*([0-9]+)$'
900-
x = format(signif(x, s), scientific = x != 0 && abs(log10(abs(x))) >= p)
901-
if (!grepl(r, x)) return(x)
902-
n = match_one(x, r)[[1]]
903-
sprintf(
904-
'%s%s10^{%s%s}', n[2], if (n[3] == '1') '' else paste(n[3], '\\times '),
905-
if (n[4] == '+') '' else n[4], n[5]
906-
)
906+
s = grepl(r, x)
907+
if (s) {
908+
n = match_one(x, r)[[1]]
909+
x = sprintf(
910+
'%s%s10^{%s%s}', n[2], if (n[3] == '1') '' else paste(n[3], '\\times '),
911+
if (n[4] == '+') '' else n[4], n[5]
912+
)
913+
}
914+
if (is.na(d <- opts$dollar)) d = s && !isTRUE(math)
915+
if (d) paste0('$', x, '$') else x
907916
}
908917

909918
# similar to the base R options() interface but for litedown options / engines /
@@ -990,7 +999,8 @@ reactor(
990999
tab.cap = NULL, tab.env = '.table', cap.pos = NULL,
9911000
print = NULL, print.args = NULL, time = FALSE,
9921001
code = NULL, file = NULL, ref.label = NULL, child = NULL, purl = TRUE,
993-
wd = NULL
1002+
wd = NULL,
1003+
signif = 3, power = 6, dollar = NA
9941004
)
9951005

9961006
# the R engine
@@ -1001,7 +1011,7 @@ eng_r = function(x, inline = FALSE, ...) {
10011011
res = if (is.na(opts$error)) eval(expr, fuse_env()) else tryCatch(
10021012
eval(expr, fuse_env()), error = function(e) if (opts$error) e$message else ''
10031013
)
1004-
return(fmt_inline(res))
1014+
return(fmt_inline(res, x$math))
10051015
}
10061016
args = reactor(
10071017
'fig.path', 'fig.ext', 'dev', 'dev.args', 'message', 'warning', 'error',

0 commit comments

Comments
 (0)