2
votes

When I run the R script below in RStudio, the theta is shown correctly.

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

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

# Greek letters
```{r ggraph}
nodes<-data.frame(label=c("θ","S"))
edges<-data.frame(from=c(2),to=c(1))
graph<-tbl_graph(nodes=nodes,edges=edges)
ggraph::ggraph(graph,layout="fr")+
    ggraph::geom_edge_link()+
    ggraph::geom_node_label(mapping = aes(label=label))     
```

But when I knit it, I get this error message (partly in Danish):

! Package inputenc Error: Unicode character θ (U+03B8) (inputenc) not set up for use with LaTeX.

Try other LaTeX engines instead (e.g., xelatex) if you are using pdflatex. For R Markdown users, see https://bookdown.org/yihui/rmarkdown/pdf-document.html Fejl: LaTeX failed to compile test.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See test.log for more info. In addition: Advarselsbeskeder: 1: I grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : konverteringsfejl på 'θ' i 'mbcsToSbcs': punktum erstattet for 2: I grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : konverteringsfejl på 'θ' i 'mbcsToSbcs': punktum erstattet for 3: I grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : konverteringsfejl på 'θ' i 'mbcsToSbcs': punktum erstattet for 4: I grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : konverteringsfejl på 'θ' i 'mbcsToSbcs': punktum erstattet for 5: I grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
konverteringsfejl på 'θ' i 'mbcsToSbcs': punktum erstattet for 6: I grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
konverteringsfejl på 'θ' i 'mbcsToSbcs': punktum erstattet for 7: I grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
konverteringsfejl på 'θ' i 'mbcsToSbcs': punktum erstattet for 8: I grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
konverteringsfejl på 'θ' i 'mbcsToSbcs': punktum erstattet for Kørsel stoppet

I have tried using expression(theta) instead of the character itself, but that doesn't work. Any ideas?

1

1 Answers

1
votes

There might be a better way to solve this, but one way is to overcome your issue is to the following:

  1. Use xelatex and the fontenc package;
  2. force dev to "jpeg", "png" or "cairo_pdf" to generate figures (θ can cause issue with other devices).

Below in the RMarkdown document I've used

    ---
    title: "test"
    header_includes:
        - \usepackage[T1]{fontenc}
    output:
        pdf_document:
          latex_engine: xelatex
    ---


    ```{r setup, include=FALSE}
     knitr::opts_chunk$set(echo = TRUE, dev = "png", dpi = 300)
     library(tidygraph)
     library(ggraph)
     ```

    # Greek letters

    ```{r ggraph}
    library(ggraph)
    library(ggplot2)
    nodes<-data.frame(label=c("θ","S"))
    edges<-data.frame(from=c(2),to=c(1))
    graph<-tbl_graph(nodes=nodes,edges=edges)
    ggraph::ggraph(graph,layout="fr")+
        ggraph::geom_edge_link()+
        ggraph::geom_node_label(mapping = aes(label=label))
    ```