问题
I wrote an .Rmd
file with some inline codes quoted by "`", and I used the knit html
function in Rstudio to convert the .Rmd
file to .html
. However, the inline codes were not highlighted as on stackoverflow, and I took a look at the source codes of the .html
file and found that the setting was like
tt, code, pre {
font-family: 'DejaVu Sans Mono', 'Droid Sans Mono', 'Lucida Console', Consolas, Monaco, monospace;
}
And I tried altering this block:
tt, code, pre {
font-family: 'DejaVu Sans Mono', 'Droid Sans Mono', 'Lucida Console', Consolas, Monaco, monospace;
background-color: #F8F8F8;
}
And it worked.
However, I do not want to do such work every time after I knit the .Rmd
file to .html
. I have read the help files of knit2html
and markdownHTMLOptions
in R but found no solution. Is there any solution to this issue?
回答1:
With the latest version of the markdown
package on CRAN, you can use the header
argument to add additional CSS code to the HTML output, e.g.
library(knitr)
knit2html(...,
header = c('<style type="text/css">', 'code{background-color: #F8F8F8;}', '</style>'))
Or just set this as a global option in your ~/.Rprofile
:
options(markdown.HTML.header = c('<style type="text/css">', 'code{background-color: #F8F8F8;}', '</style>'))
And the markdown
package will use it every time when you simply run knit2html()
without explicitly specifying the header
argument.
With RStudio, it is a little trickier. See the documentation Customizing Markdown Rendering.
来源:https://stackoverflow.com/questions/18744985/inline-codes-are-not-highlighted-after-using-knit-html-function-in-rstudio