ulyngs / oxforddown

Template for writing an Oxford University thesis in R Markdown; uses the OxThesis LaTeX template and was inspired by thesisdown.
https://ulyngs.github.io/oxforddown/
MIT License
221 stars 81 forks source link

Some characters appear incorrectly when using xelatex or lualatex #32

Closed matthew-law closed 2 years ago

matthew-law commented 2 years ago

Disclaimer that this issue might not be entirely one of oxforddown but it goes away when I don't specify template: templates/brief_template.tex, so I assume it's caused by something within that template.

When I use the characters ¹ / ² / ³ with the xelatex or lualatex engines, they get rendered as ź / š / ş in the pdf output.

How to reproduce:

I knit a .Rmd with the following YAML:

---
title: "superscript reprex"
output:
  bookdown::pdf_document2:
    template: templates/brief_template.tex
    latex_engine: pdflatex (or lualatex/xelatex)
---

and the following content:

unicode superscript:

1: ¹

2: ²

3: ³

Results

output when latex_engine: pdflatex: image

output when latex_engine: lualatex: image

output when latex_engine: xelatex: image

While this reprex uses the brief_template.tex template, it also occurs when I knit a whole thesis using the main template. It also occurs when I swap out the font for another one, so I don't think that's the issue (and in any case pdflatex does show the correct characters, so they're definitely there).

As a side note, I do know how to use normal superscript in markdown and have been usually using that, but haven't found a way to do so withkableExtra::add_header_above(), hence the use of the ² character as a workaround – if anyone reading this does know how to do this do let me know! Or if there is any other workaround that will allow me to use ² with lualatex or xelatex (pdflatex gives me other unrelated errors), that would be greatly appreciated!

ulyngs commented 2 years ago

So it seems the problem to solve then is how to get superscript with kableExtra::add_header_above().

Because for ordinary superscript, you will want to simply to use the normal markdown superscript notation (^1^) which gives the expected output with both pdflatex and xelatex (i.e. it inserts \textsuperscript{1} in the generated .tex file).

Can you provide a reprex of the problem with kableExtra::add_header_above() and superscript?

matthew-law commented 2 years ago

Here's an example with latex_engine: pdflatex (so it does work as intended):

islands[1:10] %>% 
  kable() %>% 
  add_header_above(c("", "Area (miles²)"))

image

and here's the same thing when latex_engine: lualatex or latex_engine: xelatex: image

Here are some of the approaches I've tried to circumvent the issue and their outputs (which are the same irrespective of latex_engine used) – some of these were long shots I didn't really expect to work but thought it was worth testing just in case:

islands[1:10] %>% 
  kable() %>% 
  add_header_above(c("", "Area (miles^2^)"))

image

islands[1:10] %>% 
  kable() %>% 
  add_header_above(c("", "Area (miles<sup>2</sup>)"))

image

islands[1:10] %>%
  kable() %>%
  add_header_above(c("", expression("Area (miles"^2*")")))

fails to knit with the error Error in as.data.frame.default(x[[i]], optional = TRUE) : cannot coerce class ‘"expression"’ to a data.frame

ulyngs commented 2 years ago

You just need to insert it using the latex syntax:

islands[1:10] %>% 
  kable() %>% 
  add_header_above(c("", "Area (miles\\\\textsuperscript{2})"), escape=FALSE)
image
matthew-law commented 2 years ago

Is there a way to do this which will work for both the latex and html outputs, or some way to specify different chunks to run for each?

ulyngs commented 2 years ago

Sure, just do something like

if (knitr::is_latex_output()) {
  islands[1:10] %>% 
    kable() %>% 
    add_header_above(c("", "Area (miles\\\\textsuperscript{2})"), escape=FALSE)
} else if (knitr::is_html_output()){
  islands[1:10] %>% 
    kable() %>% 
    add_header_above(c("", "Area (miles^2^"))
}

see e.g. https://community.rstudio.com/t/conditional-evaluation-of-pieces-within-rmarkdown/77038

matthew-law commented 2 years ago

Perfect, thanks!