Using gsub (or stringr) to replace text with valid LaTeX in Rmd

I'd like to replace the degree symbol (°) with the latex equivalent (\textdegree) in an Rmarkdown document that I can then compile to pdf.

Here's an example:

test <- "The temperature is 20 °C"
gsub("\\x{00B0}", "\\textdegree", test)

The output of this is The temperature is 20 textdegreeC.

How can I use gsub to replace the symbol with a functioning LaTeX command?

1 Like

This is the notorious regex horde of backslash escape problem.

Try

test <- "The temperature is 20 °C"
gsub("\\x{00B0}", "\\\\textdegree", test)
2 Likes

@technocrat Thanks. How could I take the output of gsub and use it inline in an Rmd? For example, the output of the gsub function, "The temperature is 20 \\textdegreeC" breaks as an "undefined control sequence" when dropped inline within the following Rmarkdown document:

---
title: "test"
output:
  pdf_document: default
---


```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

test <- "The temperature is 20 °C"
output <- gsub("\\x{00B0}", "\\\\textdegree", test)
```

`r output`

1 Like

Use r print(output) inline

2 Likes

Thank you, but unfortunately this doesn't solve my problem - all I get is a blank document using XeLaTeX as my compiler.

Session info R version 3.6.0 (2019-04-26) Platform: x86_64-apple-darwin15.6.0 (64-bit) Running under: macOS Mojave 10.14.5

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] googledrive_0.1.3 gdtools_0.1.8 ggiraph_0.6.1 kableExtra_1.1.0 gt_0.1.0
[6] zoo_1.8-5 here_0.1 stars_0.3-1 sf_0.7-4 abind_1.4-5
[11] raster_2.9-5 sp_1.3-1 magrittr_1.5 ecotrend_0.0.0.9000 AICcmodavg_2.2-2
[16] ggthemes_4.2.0 patchwork_0.0.1 jpeg_0.1-8 DT_0.6 plotly_4.9.0
[21] forcats_0.4.0 stringr_1.4.0 dplyr_0.8.1 purrr_0.3.2 readr_1.3.1
[26] tidyr_0.8.3 tibble_2.1.3 ggplot2_3.1.1 tidyverse_1.2.1

loaded via a namespace (and not attached):
[1] VGAM_1.1-1 colorspace_1.4-1 class_7.3-15 rgdal_1.4-4
[5] rprojroot_1.3-2 rstudioapi_0.10 lubridate_1.7.4 xml2_1.2.0
[9] codetools_0.2-16 splines_3.6.0 ncdf4_1.16.1 knitr_1.22
[13] jsonlite_1.6 broom_0.5.2 rgeos_0.4-3 shiny_1.3.2
[17] compiler_3.6.0 httr_1.4.0 backports_1.1.4 assertthat_0.2.1
[21] Matrix_1.2-17 lazyeval_0.2.2 cli_1.1.0 later_0.8.0
[25] htmltools_0.3.6 tools_3.6.0 gtable_0.3.0 glue_1.3.1
[29] reshape2_1.4.3 tinytex_0.12 Rcpp_1.0.1 cellranger_1.1.0
[33] nlme_3.1-139 crosstalk_1.0.0 xfun_0.6 rvest_0.3.3
[37] mime_0.7 rnaturalearthhires_0.2.0 MASS_7.3-51.4 scales_1.0.0
[41] promises_1.0.1 hms_0.4.2 parallel_3.6.0 curl_3.3
[45] yaml_2.2.0 gridExtra_2.3 sass_0.1.1 stringi_1.4.3
[49] highr_0.8 e1071_1.7-2 checkmate_1.9.3 rlang_0.4.0
[53] pkgconfig_2.0.2 commonmark_1.7 bitops_1.0-6 evaluate_0.13
[57] lattice_0.20-38 htmlwidgets_1.3 labeling_0.3 cowplot_0.9.4
[61] tidyselect_0.2.5 plyr_1.8.4 bookdown_0.9 R6_2.4.0
[65] generics_0.0.2 DBI_1.0.0 pillar_1.4.2 haven_2.1.0
[69] withr_2.1.2 units_0.6-3 survival_2.44-1.1 RCurl_1.95-4.12
[73] modelr_0.1.4 crayon_1.3.4 KernSmooth_2.23-15 rmarkdown_1.12
[77] viridis_0.5.1 rnaturalearth_0.1.0 grid_3.6.0 readxl_1.3.1
[81] data.table_1.12.2 digest_0.6.20 classInt_0.3-3 webshot_0.5.1
[85] xtable_1.8-4 httpuv_1.5.1 stats4_3.6.0 munsell_0.5.0
[89] unmarked_0.12-3 viridisLite_0.3.0

1 Like

The default Rmarkdown engine

/usr/local/bin/pandoc +RTS -K512m -RTS radix.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output radix.tex --template /Library/Frameworks/R.framework/Versions/3.6/Resources/library/rmarkdown/rmd/latex/default-1.17.0.2.tex --highlight-style tango --pdf-engine pdflatex --variable graphics=yes --variable 'geometry:margin=1in' --variable 'compact-title:yes'

is pdflatex.

30\textdegree

will work in a tex file using the XeLaTeX engine (provided your system font defaults contain the degree character). So, the question is how are you getting from the Rmd file to the tex file and what do you have in the preface to your tex file. Please post a reproducible example, called a reprex

2 Likes

@technocrat I think I figured it out. The above example:

---
title: "test"
output:
  pdf_document: default
---


```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

test <- "The temperature is 20 °C"
output <- gsub("\\x{00B0}", "\\\\textdegree", test)
```

`r output`

was breaking because \textdegreeC isn't valid latex. Changing "\\\\textdegree" to "\\\\textdegree " allowed for the correct encoding of the degree symbol. That was frustrating. Thanks for sticking with me.

Here's the working example that will compile:

---
title: "test"
output:
  pdf_document: default
---


```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

test <- "The temperature is 20 °C"
output <- gsub("\\x{00B0}", "\\\\textdegree ", test)
```

`r output`

3 Likes

Great catch! Cockroaches never travel solo and I should have spotted it in my answer!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.