Last active
July 20, 2025 22:29
-
-
Save friendly/b2ab1e3fe0eef40bd2e3ecb499d38547 to your computer and use it in GitHub Desktop.
Render text in color for Markdown / Quarto | Format refs to packages in inline text
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' Render text in color for Markdown / Quarto documents using LaTeX or CSS styles | |
#' | |
#' This function uses `\textcolor{}{}` from the `xcolor` package for LaTeX output | |
#' or a CSS `<span>` for HTML output. | |
#' | |
#' Note that a color not defined in the `xcolor` package will trigger a latex error. | |
#' e.g., `darkgreen` is not defined but can use: | |
#' \definecolor{darkgreen}{RGB}{1,50,32} | |
#' | |
#' @param text Text to display, a character string | |
#' @param color Color to use, a valid color designation | |
#' | |
#' @return A character string with color-encoded text | |
#' @export | |
#' | |
#' @examples | |
#' # None yet | |
#' | |
colorize <- function(text, color) { | |
if (missing(color)) color <- text | |
if (knitr::is_latex_output()) { | |
sprintf("\\textcolor{%s}{%s}", color, text) | |
} else if (knitr::is_html_output()) { | |
sprintf("<span style='color: %s;'>%s</span>", color, text) | |
} else text | |
} | |
# Define some color names for use in figure captions. | |
# use as: | |
# #| fig-cap: !expr glue::glue("Some points are ", {red}, " some are ", {blue}, "some are ", {green}) | |
red <- colorize('red') | |
pink <- colorize("pink") | |
blue <- colorize('blue') | |
green <- colorize("green") | |
lightgreen <- colorize("lightgreen") | |
darkgreen <- colorize("darkgreen") | |
if (knitr::is_latex_output()) { | |
# options(crayon.enabled = FALSE) | |
# options(cli.unicode = TRUE) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# References to package inline in text | |
# Use as: `r pkg("lattice")` or `r pkg("lattice", cite=TRUE)` | |
# | |
# Assumes the bibtex key will be of the form: R-package | |
# * Produces appropriate markup for HTML and PDF | |
# * Allows package names to be printed in color and in different font styles (bold, ital, ...) | |
# | |
# | |
# See: Demonstration of how to use other fonts in an Rmarkdown document | |
# https://gist.github.com/richarddmorey/27e74bcbf28190d150d266ae141f5117 | |
# attributes for displaying the package name | |
pkgname_font = "bold" # or: plain, ital, boldital | |
pkgname_color ="brown" # uses colorize() | |
pkgname_face = "mono" # not implemented | |
pkg <- function(package, cite=FALSE) { | |
if (knitr::is_latex_output()) { | |
pkgname <- dplyr::case_when( | |
pkgname_font == "ital" ~ paste0("\\texttt{\\textit{", package, "}}"), | |
pkgname_font == "bold" ~ paste0("\\texttt{\\textbf{", package, "}}"), | |
pkgname_font == "boldital" ~ paste0("\\texttt{\\textit{\\textbf{", package, "}}}"), | |
.default = paste0("\\texttt{", package, "}}") | |
) | |
} | |
# HTML output | |
else { | |
pkgname <- dplyr::case_when( | |
pkgname_font == "ital" ~ paste0("_", package, "_"), | |
pkgname_font == "bold" ~ paste0("**", package, "**"), | |
pkgname_font == "boldital" ~ paste0("***", package, "***"), | |
.default = package | |
) | |
} | |
ref <- pkgname | |
if (!is.null(pkgname_color)) ref <- colorize(pkgname, pkgname_color) | |
if (cite) ref <- paste0(ref, " [@R-", package, "]") | |
if (knitr::is_latex_output()) { | |
ref <- paste0(ref, "\n\\index{`", pkgname, " package`}", | |
"\n\\index{packages!", pkgname, "}") | |
} | |
ref | |
} | |
# Same, but say "`pkgname` package cite" | |
package <- function(package, cite=FALSE) { | |
if (knitr::is_latex_output()) { | |
pkgname <- dplyr::case_when( | |
pkgname_font == "ital" ~ paste0("\\texttt{\\textit{", package, "}}"), | |
pkgname_font == "bold" ~ paste0("\\texttt{\\textbf{", package, "}}"), | |
pkgname_font == "boldital" ~ paste0("\\texttt{\\textit{\\textbf{", package, "}}}"), | |
.default = paste0("\\texttt{", package, "}}") | |
) | |
} | |
# HTML output | |
else { | |
pkgname <- dplyr::case_when( | |
pkgname_font == "ital" ~ paste0("_", package, "_"), | |
pkgname_font == "bold" ~ paste0("**", package, "**"), | |
pkgname_font == "boldital" ~ paste0("***", package, "***"), | |
.default = package | |
) | |
} | |
ref <- pkgname | |
if (!is.null(pkgname_color)) ref <- colorize(pkgname, pkgname_color) | |
if (cite) ref <- paste0(ref, " package [@R-", package, "]") | |
if (knitr::is_latex_output()) { | |
ref <- paste0(ref, "\n\\index{`", pkgname, " package`}", | |
"\n\\index{packages!", pkgname, "}") | |
} | |
ref | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment