Math mode not parsed properly if a numeral immediately follows it

I would like to type F#5, but instead of the pound sign, I'd like to use the actual musical "sharp" symbol (\sharp), which I can access with $\sharp$ in math mode. However, the numeral 5 after the second $ messes with how it's is parsed, so F$sharp$5 renders as F$$5. It only seems to work if I put a space between it, as in F$\sharp$ 5, but that renders as F# 5 and I don't want the space there.

Is there some symbol I can put between the second $ and 5, or some other trick, to ensure that they should be separated? My temporary solution is to put the 5 inside of math mode (F$sharp5$) but I don't like that the 5 is in a different font than the rest of the text.

This is a hack, but ...
If I put \newcommand{\five}{5} immediately after the header at the beginning of my document, I can later use F$\sharp$\five and get no space between the sharp symbol and the 5.

1 Like

I assume you are targetting html ouput ?

This happens because with F$\sharp$5, math mode is not recognized. This is how Pandoc sees the content

> pandoc::pandoc_convert(text = "F$\\sharp$5", to = "native")
[ Para
    [ Str "F$" , RawInline (Format "tex") "\\sharp" , Str "$5" ]
]
> pandoc::pandoc_convert(text = "F$\\sharp$ 5", to = "native")
[ Para
    [ Str "F" , Math InlineMath "\\sharp" , Space , Str "5" ]
]

As you are aiming for HTML output, the raw TeX inline is ignored, hence the two $$ you get.

I don't think Pandoc can parse such content as inline math without the space after. Though, you can do your own syntax and extend using Lua filter

Span = function(s) 
  if s.classes:includes("math") then
    return pandoc.Math('InlineMath', pandoc.utils.stringify(s.content))
  end
end

Full example

---
title: "Republishing"
output: 
  html_document:
    pandoc_args: !expr rmarkdown::pandoc_lua_filter_args("math.lua")
    keep_md: true
date: "2023-08-08"
---

```{cat, engine.opts = list(file = "math.lua")}
Span = function(s) 
  if s.classes:includes("math") then
    return pandoc.Math('InlineMath', pandoc.utils.stringify(s.content))
  end
end
```

F[\\sharp]{.math}5

You can learn more about Lua Filters at Pandoc - Pandoc Lua Filters or in Quarto documentation as it uses extensively Lua (Quarto – Lua Development)

This is just an idea, as I am not sure Pandoc can parse differently, but you could also open an issue about F$\sharp$5 not being parsed as Math with a space after last $

Why not just use the UTF-8 code. ♯ is 266F

Is that new \five command supposed to work outside of math mode? I can't seem to get it to.

I may end up doing this. I just didn't want to have to hunt down that symbol every time I want to use it.

Thank you for your detailed response! I wasn't able to get it to work, but it might be because all this Pandoc code is above my paygrade, so to speak. Maybe I should open an issue because this seems a bit complex for what I believe should be a straightforward fix.

Sorry to hear that (and seeing this just now - sorry for the delay).

The code I shared still works for me using latest versions of R Markdown and RStudio IDE. This is also using a recent Pandoc. You can check the version you are using with rmarkdown::pandoc_version()

You could open an issue in Pandoc or asked in https://groups.google.com/g/pandoc-discuss

But this could just be a parsing limitation for some reason as this is documented in Pandoc's MANUAL (Pandoc - Pandoc User’s Guide)

Anything between two $ characters will be treated as TeX math. The opening $ must have a non-space character immediately to its right, while the closing $ must have a non-space character immediately to its left, and must not be followed immediately by a digit.

Why this rule ? I don't know but when this is documented, I believe this is not an issue.

As you are targetting HTML, why not use the HTML version for the Unicode character ?

Try

F♯5

In you document. It will render as
image

Hope it helps

This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.