Closed matthew-law closed 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?
Here's an example with latex_engine: pdflatex
(so it does work as intended):
islands[1:10] %>%
kable() %>%
add_header_above(c("", "Area (miles²)"))
and here's the same thing when latex_engine: lualatex
or latex_engine: xelatex
:
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^)"))
islands[1:10] %>%
kable() %>%
add_header_above(c("", "Area (miles<sup>2</sup>)"))
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
You just need to insert it using the latex syntax:
islands[1:10] %>%
kable() %>%
add_header_above(c("", "Area (miles\\\\textsuperscript{2})"), escape=FALSE)
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?
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
Perfect, thanks!
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:
and the following content:
Results
output when
latex_engine: pdflatex
:output when
latex_engine: lualatex
:output when
latex_engine: xelatex
: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 with
kableExtra::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!