Package 'snippet'

Title: Create Nice Code Images
Description: Render typeset code snippets using 'Typst'. Supports syntax highlighting for many languages, multiple output formats, and theming via 'tmTheme' files. Designed for documentation, teaching materials, and reproducible code presentation.
Authors: Christopher T. Kenny [aut, cre] (ORCID: <https://orcid.org/0000-0002-9386-6860>)
Maintainer: Christopher T. Kenny <[email protected]>
License: MIT + file LICENSE
Version: 0.0.1
Built: 2026-05-09 21:42:38 UTC
Source: https://github.com/christopherkenny/snippet

Help Index


Render a code snippet using Typst

Description

This function renders a static code snippet using a Typst template and the typr package. It is purely visual and does not evaluate code. Styling, syntax highlighting, and window style are all configurable.

Usage

snippet(
  code,
  lang = NULL,
  title = "",
  style = c("windows", "mac", "none"),
  background,
  theme = "auto",
  width = 5,
  line_numbers = FALSE,
  format = c("png", "pdf", "svg"),
  output_file = NULL
)

Arguments

code

Character vector, string, or file path. Code to render. If nothing is supplied, it defaults to reading your current clipboard contents.

lang

Language name for syntax highlighting. Inferred from file extension if code is a path, otherwise defaults to 'r'.

title

Optional title for the code snippet.

style

Window style: one of 'mac', 'windows', or 'none'.

background

Background color as a hex string. If not provided, defaults to the background color of the selected theme, or '#CCCCCC'.

theme

Theme name from snippet_themes() or path to a .tmTheme file. Use 'auto' or 'none' for Typst's built-in options.

width

Width of the rendered output in inches. Defaults to 5.

line_numbers

Whether to show line numbers. Defaults to FALSE.

format

Output format, one of 'pdf', 'png', or 'svg'.

output_file

File path to write the rendered result. If NULL, a temporary file is used.

Value

Invisibly, the path to the rendered file.

Examples

## Not run: 
snippet('x <- 1:10\nmean(x)')

## End(Not run)

Install a theme for use with snippet

Description

Downloads or copies a .tmTheme file to the user's snippet cache directory, making it available to snippet() and snippet_themes().

Usage

snippet_install_theme(theme, overwrite = FALSE)

Arguments

theme

One of:

  • A theme name from snippet_known_themes() to download a curated theme

  • A URL pointing to a .tmTheme file to download

  • A file path to a local .tmTheme file to copy

overwrite

Whether to overwrite an existing installed theme. Defaults to FALSE.

Value

Invisibly, the path to the installed theme file.

Examples

## Not run: 
snippet_install_theme('Dracula')
snippet_install_theme('https://example.com/my-theme.tmTheme')
snippet_install_theme('/path/to/my-theme.tmTheme')

## End(Not run)

List curated themes available to install by name

Description

Returns a named character vector of URLs for themes that can be installed by name via snippet_install_theme().

Usage

snippet_known_themes()

Value

A named character vector where names are theme names and values are download URLs.

Examples

snippet_known_themes()

Return paths to themes available in snippet

Description

Returns paths to both bundled themes and any themes previously installed with snippet_install_theme().

Usage

snippet_themes()

Value

A named character vector of file paths to available themes. Names are human-readable theme names derived from file names.

Examples

snippet_themes()