Closed
Description
I encountered a kerning issue when using ggsave()
with cairo_pdf
in R. While searching for a solution using base Cairo utilities, I ran into a problem with rendering R expression
.
What I expected was a simple and reliable way to eliminate kerning issues while still being able to use Cairo's PDF rendering capabilities, especially for mathematical expressions in plots (by using latex2exp
package).
Here is a minimal reproducible example illustrating the issue:
library(ggplot2)
plot <- ggplot() + theme_void() +
annotate("text",
x = 0.5, y = 0.5, size = 1.5,
label = expression('example formula: '*alpha^{2} + beta)) +
scale_x_continuous(limits = c(0, 1), expand = c(0, 0)) +
scale_y_continuous(limits = c(0, 1), expand = c(0, 0))
# Expression renders correctly, but with kerning issues
ggsave("ggsave_cairo.pdf", plot = plot, device = cairo_pdf)
# No kerning issues, but expression rendering fails
Cairo::CairoPDF(file = "base_cairo.pdf")
grid::grid.draw(plot)
dev.off()
Note: Rasterizing fonts using the showtext
package is not a suitable solution in my view, as I need to keep text fully vectorized in the output PDF.