latex3 / fontspec

Font selection in LaTeX for XeTeX and LuaTeX
http://latex3.github.io/fontspec/
LaTeX Project Public License v1.3c
277 stars 34 forks source link

fontspec can't handle fallback fonts provided through markdown. #385

Closed markbneal closed 4 years ago

markbneal commented 5 years ago

Description

The issue is that compiling a markdown file fails if fallback fonts have been passed through with similar code as they would go to html.

From an rmarkdown perspective, ideally you want to use the same code to determine fonts, regardless of whether you knit to html or pdf.

For example, you can select a preferred font, and a fallback font, e.g. for the first row of a table (with huxtable package) like this:

font(ht)[1,]   <- paste0("Agenda, sans-serif")

Which if you knit to html results in something like this, which then uses the first available font:

html { font-family: Agenda, sans-serif}

The same code causes an error (in fontspec?) when you knit to pdf.

tlmgr search --file --global "/Agenda, san-serif[.](tfm|afm|mf|otf)"
! Package fontspec Error: The font "Agenda, san-serif" cannot be found.

! kpathsea:make_tex: Invalid filename `Agenda, san-serif', contains ','

Here's an excerpt of what is being passed on as per the markdown file.

`\multicolumn{1}{!{\huxvb{0, 0, 0}{0}}r!{\huxvb{0, 0, 0}{0}}}{\huxtpad{4pt}\raggedleft {\fontspec{Agenda, san-serif} Sepal.Length}\huxbpad{4pt}} &
\multicolumn{1}{r!{\huxvb{0, 0, 0}{0}}}{\huxtpad{4pt}\raggedleft {\fontspec{Agenda, san-serif} Sepal.Width}\huxbpad{4pt}} &`

To be fair it may not be a high priority for fontspec maintainers to handle options like this that rmarkdown wants to throw at it for fallback fonts, but could it be quite easy to handle in terms of just taking the first-named font before the comma? I can't think that rmarkdown users would knit a pdf where they didn't have the first named font very often, or if it happened, they wouldn't think that is a reasonable error that the user can easily fix, e.g by installing the named font.

davidcarlisle commented 5 years ago

Surely this should be reported as an error in whatever markdown to tex convertor you are using (you don't say which, perhaps "knit" implies you are using R?) It should generate valid latex (fontspec) syntax.

markbneal commented 5 years ago

Thanks @davidcarlisle, yes I am using R (RStudio, Rmarkdown, xelatex). If I understand you correctly, the markdown to tex convertor should be the piece of the chain that ensures later pieces (e.g. fontspec) only gets one properly formatted font name. Where would be the place to raise that issue?

davidcarlisle commented 5 years ago

@markbneal sorry I have no idea where the convertor that R uses is maintained, but naturally any something-to-latex convertor has to be responsible for generating valid latex syntax, you can not expect latex to change to accept whatever output is generated by any third party software that aims to generate latex.

markbneal commented 5 years ago

Thanks @davidcarlisle, I have raised it at the rstudio community here - will see what options are proposed.

markbneal commented 4 years ago

Link in my previous comment has a good solution. I’ll close this.