Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R latex pattern #1701

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Authors@R: c(
person("Lorenz", "Walthert", role = "ctb"),
person("Lucas", "Gallindo", role = "ctb"),
person("Martin", "Modrák", role = "ctb"),
person("Michael", "Braun", role = "ctb"),
person("Michael", "Chirico", role = "ctb"),
person("Michael", "Friendly", role = "ctb"),
person("Michal", "Bojanowski", role = "ctb"),
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## NEW FEATURES

- Added a pattern and file type (.RLatex), so R code chunks in a LaTeX document can be defined within an Rcode environment.

- It is possible to customize the sign `\times` used for the scientific notation of inline numeric output via a global option, e.g., `options(knitr.inline.times = '\\cdot ')` (thanks, @wuffi @trentks, #1563).

## BUG FIXES
Expand Down
8 changes: 5 additions & 3 deletions R/output.R
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,11 @@ knit = function(input, output = NULL, tangle = FALSE, text = NULL, quiet = FALSE
ext, "' in built-in pattern lists; ",
'see ?knit_patterns on how to set up customized patterns')
set_pattern(pattern)
if (pattern == 'rnw' && length(sweave_lines <- which_sweave(text)) > 0)
if (pattern %in% c('rnw','rlatex') && length(sweave_lines <- which_sweave(text)) > 0)
remind_sweave(if (in.file) input, sweave_lines)
opts_knit$set(out.format = switch(
pattern, rnw = 'latex', tex = 'latex', html = 'html', md = 'markdown',
pattern, rnw = 'latex', tex = 'latex', html = 'html', md = 'markdown',
rlatex = 'latex',
rst = 'rst', brew = 'brew', asciidoc = 'asciidoc', textile = 'textile'
))
}
Expand Down Expand Up @@ -344,7 +345,7 @@ process_file = function(text, output) {
auto_out_name = function(input, ext = tolower(file_ext(input))) {
base = sans_ext(input)
name = if (opts_knit$get('tangle')) c(base, '.R') else
if (ext %in% c('rnw', 'snw')) c(base, '.tex') else
if (ext %in% c('rnw', 'snw', 'rlatex')) c(base, '.tex') else
if (ext %in% c('rmd', 'rmarkdown', 'rhtml', 'rhtm', 'rtex', 'stex', 'rrst', 'rtextile'))
c(base, '.', substring(ext, 2L)) else
if (grepl('_knit_', input)) sub('_knit_', '', input) else
Expand All @@ -355,6 +356,7 @@ auto_out_name = function(input, ext = tolower(file_ext(input))) {
# determine output format based on file extension
ext2fmt = c(
rnw = 'latex', snw = 'latex', tex = 'latex', rtex = 'latex', stex = 'latex',
rlatex = 'latex',
htm = 'html', html = 'html', rhtml = 'html', rhtm = 'html',
md = 'markdown', markdown = 'markdown', rmd = 'markdown', rmarkdown = 'markdown',
brew = 'brew', rst = 'rst', rrst = 'rst'
Expand Down
15 changes: 14 additions & 1 deletion R/pattern.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,16 @@ all_patterns = list(
chunk.end = '^###[.]\\s+end[.]rcode\\s*$',
ref.chunk = '^\\s*<<(.+)>>\\s*$',
inline.code = '@r +([^@]+)\\s*@',
inline.comment = '^###[.].*')
inline.comment = '^###[.].*'),

`rlatex` = list(
chunk.begin = "^\\s*\\\\begin\\{Rcode\\}(?:\\[(.*)?\\])?\\s*(%.*)?$",
chunk.end = "^\\s*\\\\end\\{Rcode\\}\\s*(%+.*|)$",
inline.code = "\\\\Sexpr\\{([^}]+)\\}",
inline.comment = "^\\s*%.*",
header.begin = "(^|\n)\\s*\\\\documentclass[^}]+\\}",
document.begin = "\\s*\\\\begin\\{document\\}",
ref.chunk = "^\\s*\\\\begin\\{Rcode\\}\\[(.*)\\].*$")
)

.sep.label = '^(#|--)+\\s*(@knitr|----+)(.*?)-*\\s*$' # pattern for code chunks in an R script
Expand Down Expand Up @@ -128,6 +137,9 @@ pat_rst = function() set_pattern('rst')
pat_asciidoc = function() set_pattern('asciidoc')
#' @rdname pat_fun
pat_textile = function() set_pattern('textile')
#' @rdname pat_fun
pat_rlatex = function() set_pattern('rlatex')



# helper functions
Expand All @@ -146,6 +158,7 @@ detect_pattern = function(text, ext) {
if (ext %in% c('rmd', 'rmarkdown', 'markdown', 'md')) return('md')
if (ext %in% c('rst', 'rrst')) return('rst')
if (ext %in% c('asciidoc', 'rasciidoc', 'adoc', 'radoc')) return('asciidoc')
if (ext %in% c('rlatex', 'Rlatex', 'RLatex')) return('rlatex')
}
for (p in names(all_patterns)) {
for (i in c('chunk.begin', 'inline.code')) {
Expand Down
22 changes: 22 additions & 0 deletions tests/testit/test-rlatex.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
library(testit)

assert(
'detect_pattern() automatically detects syntax patterns',
identical(detect_pattern('<<>>='), 'rnw'),
identical(detect_pattern('<<foo, bar=TRUE>>='), 'rnw'),
identical(detect_pattern('\\begin{Rcode}'), 'rlatex'),
identical(detect_pattern('\\begin{Rcode}[foo, bar=TRUE]'), 'rlatex'),
identical(detect_pattern('asdf', 'rlatex'), 'rlatex'),
detect_pattern('foo') %==% NULL
)


ce = all_patterns$rlatex$chunk.end
assert(
'patterns for rlatex',
identical(grep(ce, ' \\end{Rcode}'), 1L), # spaces before @
identical(grep(ce, '\\end{Rcode} '), 1L), # spaces after @
identical(grep(ce, '\\end{Rcode} %asdf'), 1L), # comments after %
identical(grep(ce, '\\end{Rcode} asdf'), integer()), # only spaces/comments allowed
identical(grep(ce, ' \\end{Rcode} a% sdf'), integer())
)