The Highlightr plugin is a popular R package for displaying code snippets with syntax highlighting in R Markdown documents. With the recent release of version 1.1.2, the plugin has added a new feature that allows users to choose inline CSS classes for highlighting, making it more flexible and customizable than ever before. In this article, we will discuss the key concepts of this new feature, including context topics, subtitles, and proper formatting.
Key Concepts
Before we dive into the details of the new inline CSS class selection feature, let's first cover some essential background information about Highlightr and R Markdown. R Markdown is a document format that allows you to create dynamic, reproducible reports using R code and Markdown syntax. Highlightr is an R package that integrates with R Markdown to provide syntax highlighting for code snippets.
Context Topic
The new inline CSS class selection feature is particularly useful when working with complex R Markdown documents that require fine-grained control over the appearance of code snippets. For example, you might want to apply different styles to different parts of the same code snippet or to different code snippets in the same document.
Subtitles
Declaring Everything Inline
With the new version of Highlightr, you can now declare all CSS settings and classes inline, eliminating the need to store them in external files. This can be particularly useful for small projects or for testing different styles without having to switch between files.
Storing CSS Settings as CSS Classes
To use the new inline CSS class selection feature, you simply need to define your CSS classes as strings and pass them as arguments to the knitr::knit_chunk() function. For example, the following code snippet demonstrates how to define and apply a custom CSS class called "my-class" to a code chunk:
{r, echo=FALSE, message=FALSE, results='hide', class="my-class"}
x <- 1:10
sum(x)
{r}
In this example, the class argument is used to apply the "my-class" style to the code chunk.
Formatting Code Blocks
When using inline CSS classes, it's essential to properly format the code blocks to ensure that the classes are applied correctly. This involves enclosing the code blocks in <code> tags and using triple backticks (```) to indicate the programming language. Indentation and tabulation are also necessary for proper formatting.
{r, echo=FALSE, message=FALSE, results='hide', class="my-class"} x <- 1:10 sum(x) {r}Summary and References
- Highlightr is an R package for syntax highlighting in R Markdown documents.
- Version 1.1.2 of Highlightr adds the ability to choose inline CSS classes for highlighting.
- Declaring CSS settings and classes inline eliminates the need for external files.
- To apply a custom CSS class to a code chunk, use the
classargument in theknitr::knit_chunk()function.
For more information about Highlightr and R Markdown, check out the following resources: