Closed mingsu closed 4 years ago
The best thing is to use text like e.g. "the next word is in italic" and then set escape_contents
to FALSE
. That requires that you know the output format, of course.
The best thing is to use text like e.g. "the next word is in italic" and then set
escape_contents
toFALSE
. That requires that you know the output format, of course.
Thanks for your quick reply.
I agree, and this is a possible solution for now.
To my understanding, the Rmarkdown file has to be like this for HTML
output format,
bla bla <b>bold</b> blabla
and it has to be changed to below for LATEX
output format.
bla bla \textbf{bold} blabla
If this is true, then I think it would be a great feature if supporting it naturally. In many cases, the output format can be multiple. Like my case for now, I can write special functions to fit the HTML
and LATEX
output format, but I don't know how to make it work for DOCX
and other MS office format.
The expect content of Rmarkdown file is like below, and by setting escape_contents
to FALSE
, output can automatically fit to the target format.
bla bla **bold** blabla
Simplest option would probably be to support markdown and then write translators - or reuse existing ones. Cf. gt.
commonmark could be a friend here. It has translators for html, latex and text.
Thanks, I have looked into commonmark
, but it seems beyond my knowledge to imbed into huxtable
or in R functions.
I am writing notes to myself re implementation. If you want to do this, you could do something like:
text <- "Some **markdown** text"
formatted_text <- switch(
huxtable::guess_knitr_output_format(),
"html" = commonmark::markdown_html(text),
"latex" = commonmark::markdown_latex(text)
)
huxtable(text = formatted_text)
It would be amazing if you could implement markdown support (plus translators to different output formats). This seems to work in kable
already. In general, kable
's handling of different output formats is more consistent, also with regard to math (huxtable
only seems to work with $$...$$
when output is HTML and $...$
when output is LaTeX), see
---
title: "kable vs. huxtable"
output: pdf_document # html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(magrittr)
check_output_format <- function(output_format) {
output_format %in% knitr::opts_knit$get("rmarkdown.pandoc.to")
}
table_text <- tibble::tribble(
~syntax, ~sub, ~super, ~bold, ~italic,
"HTML", "text<sub>subscript</sub>","text<sup>superscript</sup>", "<b>text</b>", "<i>text</i>",
"LaTeX", "text\\textsubscript{subscript}", "text\\textsuperscript{superscript}", "\\textbf{text}", "\\textit{text}",
"Markdown", "text~subscript~", "text^superscript^", "**text**", "*text*"
)
table_math <- tibble::tribble(
~syntax, ~math,
"inline", "$ln\\left(\\frac{\\pi_i}{1-\\pi_i}\\right)$",
"inline with text", "The coefficient $\\beta_2$",
"equation", "$$ln\\left(\\frac{\\pi_i}{1-\\pi_i}\\right)$$"
)
table_text %>%
knitr::kable()
table_math %>%
knitr::kable()
if (check_output_format("html")) {
table_text %>%
huxtable::as_hux() %>%
huxtable::theme_article() %>%
huxtable::set_escape_contents(FALSE)
} else {
table_text %>%
dplyr::slice(-3) %>% # huxtable throws error when using ^
huxtable::as_hux() %>%
huxtable::theme_article() %>%
huxtable::set_escape_contents(FALSE)
}
if (check_output_format("html")) {
table_math %>%
huxtable::as_hux() %>%
huxtable::theme_article() %>%
huxtable::set_escape_contents(FALSE)
} else {
table_math %>%
dplyr::slice(-3) %>% # huxtable throws error when using $$
huxtable::as_hux() %>%
huxtable::theme_article() %>%
huxtable::set_escape_contents(FALSE)
}
I'm persuaded this is a good idea. It'll happen either in 5.0.0 or in the next release. Incidentally, if you'd like to try out 5.0.0, most of that code has now been merged into master.
Fixed in 901c0e1 .
Thanks for the implementation! I just tried it out and if I'm not mistaken, so far it only works for bold and italic markdown markup. Do you think that you can implement a more general solution that works for other markdown markup (like, e.g., the sub/superscripts in my example above, but hopefully all markdown)? It would be amazing if we could have a general implementation like in kable
.
Did you by any chance have the time to look into the issue with inconsistent handling of math syntax between different output formats I mentioned above? Should I add this as an independent issue? Thank you!
Thanks for your effort to make this happen. I agree with @januz, that would be great to support more markdown syntax.
It should do most things in LaTeX/HTML. Screen output will be limited by definition. It won't support built-in tables or other extensions (what kind of sicko would do that?). But it's limited by the commonmark implementation. If you have specific bugs, file them.
Great package!
To specify font style for some characters is very useful, such as:
Currently, the
set_italic
,set_bold
are at cell level, is it possible or worth to do it?