formatting in-line code
This SO question doesn't appear to work anymore. I tried this but errors were produced:
hook_inline <- knitr::knit_hooks$get('inline')
knitr::knit_hooks$set(
inline = function(x) {
if (is.character(x) & knitr::is_html_output()) {
highr::hi_html()
} else {
hook_inline
}
}
)
From this SO post:
I think this problem is possible to solve. Which output formats do you want the syntax highlighting to work with? I haven't tested it, but I guess HTML might be a little easier. LaTeX might be trickier, but shouldn't be terribly hard. – Yihui Xie Jun 29 at 4:58
Sorry for the delay on this. It was always through my list so going back to it now as doing some cleaning in relation rmarkdown and bookdown
but errors were produced:
I am not sure about the error you had.
Trying your demo repo (thanks) seems to work with these adjusments:
- The hook would be this one
```{r inline, include = FALSE}
local({
hook_inline <- knitr::knit_hooks$get('inline')
knitr::knit_hooks$set(
inline = function(x) {
if (is.character(x) & knitr::is_html_output()) {
paste0(
"<code>",
highr::hi_html(x),
"</code>"
)
} else {
hook_inline(x)
}
}
)
})
```
adding <code> is not needed if you write in the Rmd something like `` `r 'lm(y ~ x, data = dat)'` ``
-
highris using thehighlighttool so you need the right CSS as gitbook won't insert it. Gitbook will use Pandoc's highlighting, so you would need to create a CSS based on this highlighting to get the same as in code chunk. For testing I added this in your document (found in https://github.com/andre-simon/highlight/tree/master/extras/css-themes)
```{css, echo = FALSE}
.hl.num { color: #fd971f; }
.hl.esc { color: #fd971f; }
.hl.ipl { color: #cc6633; }
.hl.str { color: #a6e22e; }
.hl.ppc { color: #ae81ff; }
.hl.pps { color: #a1efe4; }
.hl.com { color: #a59f85; font-style: italic; }
.hl.slc { color: #a59f85; font-style: italic; }
.hl.lin { color: #75715e; }
.hl.opt { color: #f8f8f2; }
.hl.kwa { color: #f92672; }
.hl.kwb { color: #f4bf75; }
.hl.kwc { color: #66d9ef; }
.hl.kwd { color: #ae81ff; }
```
I get this in the output

So there would be some adjustment to make to get that working.
Also using inline hook would mean it would apply to any output, including those that are not supposed to be show as R code chunk in the output if I am correct. Ex:
There is `r nrow(data)` line in the table
I believe nowadays downlit would simplify this - or could if it can't already.
is this still something you want to do ?
downlit support for gitbook() is still a thing to do : https://github.com/rstudio/bookdown/issues/844
At last, regarding bookdown project, the hook being applied may depend on where it is set according to the settings used (new_session: false specifically). This part could be related directly to #3
Anyway, it was a long overdue digging through. Sorry for the wait.