RMarkdown: \cdot instead of \times for exponential

When having a RMarkdown file with something like r 1.60e9, the resulting PDF output includes this number as 1.60 × 10⁹.
This is nearly perfect - I would prefer a dot (" · " using something like LaTeX's \cdot) as the sign for multiplication in between.

I'm guessing there's a neater way to get it done, but FYI, you can include LaTeX chunks in your R Markdown.

Can you give a snippet of code where you're producing this output, it's always easier to work with something akin to a reprex (short for minimal reproducible example).

After a bit of searching, it turns out there's a toLatex function in the sfsmisc package that will take care of this. After loading the package, you can type inline text like the following to render scientific notation with \cdot.

$`r toLatex(y)`$

Or, to control significant digits and ensure scientific notation:

$`r toLatex(y, digits=3, scientific=TRUE)`$

Original Answer

There may be a more direct way to substitute a \cdot for the \times but here's a long-winded way (assuming y is the previously defined value you want to render):

$`r round(y/10^floor(log10(y)), 2)` \cdot `r 10^floor(log10(y))`$

You can also package this into a function. Here's a complete rmarkdown example.

rmarkdown document

---
output: pdf_document
---

```{r}
y = 1.60e9
```

The result is $`r sprintf("%1.2f", y/10^floor(log10(y)))` \cdot `r 10^floor(log10(y))`$.  

\vspace{0.5cm}

Package this into a function:

```{r}
cdot = function(x, digits=2) {
  
  b = floor(log10(x))

  paste0(sprintf(paste0("%1.", digits, "f"), x/10^b), " \\cdot 10^", b)
  
}
```

The result is $`r cdot(y)`$.

Output PDF

17%20AM

4 Likes

Hi joels,

Thanks a lot for your nice solution. It is a good workaround
It would be nice, if there would be a RMarkdown option which could globally switch the output for all chunks in a document as an improvement.

For future consultation of this topic, see about