save_tikz: Save a ggplot Figure as a TikZ File

View source: R/save_tikz.R

save_tikzR Documentation

Save a ggplot Figure as a TikZ File

Description

This function saves a ggplot figure as a TikZ file for inclusion in LaTeX documents. It checks for a valid ggplot object, ensures the file path is specified, and appends the .tex extension if not provided. The function also allows sanitization of special characters in the output file.

Usage

save_tikz(fig = NULL, path = NULL, sanitize = TRUE, width = 6.4, height = 3.54)

Arguments

fig

A ggplot object. This is the figure to be saved as a TikZ file.

path

A character string specifying the file path where the TikZ file will be saved. If no extension is provided, .tex will be appended. If the directory does not exist, an error will be raised.

sanitize

Logical, whether to sanitize special characters in the LaTeX output. Defaults to TRUE.

width

Numeric, specifying the width of the figure in inches. Defaults to 6.4.

height

Numeric, specifying the height of the figure in inches. Defaults to 3.54.

Details

This function uses the tikzDevice package to generate TikZ code from a ggplot object. It ensures that the file path provided is valid and appends the .tex extension if missing. If the specified directory does not exist, the function will abort with an error message. Additionally, the function provides useful information to the user, such as warnings when no extension is provided and confirmation once the file is saved successfully.

Value

The path to the saved TikZ file is returned invisibly.

Note

The tikzDevice package must be installed to use this function.

Examples

## Not run: 
library(ggplot2)

# Create a sample plot
p <- ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point() +
  theme_minimal()

# Save the plot as a TikZ file
save_tikz(fig = p, path = "my_plot")

## End(Not run)


AndreSjuve/texaid documentation built on Oct. 20, 2024, 6:17 a.m.