Skip to content

Font package detection fails for fonts with spaces in names #478

@cderv

Description

@cderv

When R Markdown documents use fonts with spaces in their names (e.g., "Noto Emoji", "DejaVu Sans"), automatic package installation via tinytex::parse_install() fails.

Root Cause

The font_ext() function in R/latex.R:663-667 generates search patterns that preserve spaces:

font_ext = function(x) {
  i = !grepl('[.]', x)
  x[i] = paste0(x[i], '(-(Bold|Italic|Regular).*)?[.](tfm|afm|mf|otf|ttf)')
  x
}

When LaTeX reports Font "Noto Emoji" not found, this generates:

Noto Emoji(-(Bold|Italic|Regular).*)?[.](tfm|afm|mf|otf|ttf)

But font files never have spaces in their names:

  • NotoEmoji-Regular.ttf
  • DejaVuSans-Bold.ttf

So tlmgr search --file fails to find the package.

Solution

Replace spaces with \s* pattern to match files with or without spaces:

font_ext = function(x) {
  i = !grepl('[.]', x)
  # Replace spaces with \s* for regex matching
  x[i] = gsub('\s+', '\\s*', x[i])
  x[i] = paste0(x[i], '(-(Bold|Italic|Regular).*)?[.](tfm|afm|mf|otf|ttf)')
  x
}

This generates: Noto\s*Emoji(-(Bold|Italic|Regular).*)?[.](tfm|afm|mf|otf|ttf)

Verification

# Test that pattern works with tlmgr
system2('tlmgr', c('search', '--file', '--global', 'Noto\s*Emoji'))
# Returns: noto-emoji package ✓

system2('tlmgr', c('search', '--file', '--global', 'DejaVu\s*Sans'))
# Returns: dejavu package ✓

Related

This issue was discovered and fixed in quarto-dev/quarto-cli#13726, which uses patterns derived from tinytex.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions