简体   繁体   English

带有 html/docx 输出的 R Markdown 文档,使用 LaTeX 包 bbm?

[英]R Markdown document with html/docx output, using LaTeX package bbm?

How can I use the LaTeX package bbm in an R markdown for html/docx output?如何在 html/docx 输出的 R 降价中使用 LaTeX 包bbm

Currently I'm using the hack solution below where, essentially, I just abandon using the package bbm for .docx/.html output.目前我正在使用下面的 hack 解决方案,基本上,我只是放弃使用bbm包用于 .docx/.html 输出。 Is there a hack solution in which I can still use the package?是否有一个 hack 解决方案,我仍然可以使用该软件包?

Note, this question related to my question In an R Markdown document, “includes” for docx output?请注意,这个问题与我的问题有关在 R Markdown 文档中,“包含”用于 docx 输出? where there I'm specifically asking about how to move these special <!--- For DOCX Only ---> code chunk to a preamble-word.tex file to be indcluded in the YAML header.在那里我特别询问如何将这些特殊的<!--- For DOCX Only --->代码块preamble-word.tex到要包含在 YAML 标头中的preamble-word.tex文件。 This question is also related to the question How to get \\bm{} to work in an R markdown (to HTML) file?这个问题也与How to get \\bm{} to work in a R markdown (to HTML) file? My current hack for bbm is basically an adaptation of one of the hacks proposed as an answer to that question.我目前对bbm hack 基本上是对作为该问题的答案提出的其中一个 hack 的改编。

tinytextest.Rmd小文本测试.Rmd

---
title: "TinyTeX Test"
header-includes:
  - \usepackage{bbm}
output:
  pdf_document: default
  html_document: default
  word_document: default
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

<!--- For HTML Only --->
`r if (knitr:::is_html_output()) '
$\\newcommand{\\mathbbm}[1]{\\mathbf{#1}}$
'`

<!--- For DOCX Only --->
`r if (!knitr:::is_latex_output() & !knitr:::is_html_output()) '
\\newcommand{\\mathbbm}[1]{\\mathbf{#1}}
'`

Hello

\[\mathbbm{1}_{S}(x)\]

.pdf output .pdf 输出

在此处输入图片说明

.docx/.html output .docx/.html 输出

在此处输入图片说明

Ok, another answer, I just realized, these are two slightly different questions - one by the question starter and one by the bounty starter.好的,另一个答案,我刚刚意识到,这是两个略有不同的问题 - 一个由问题发起人提出,一个由赏金发起人提出。

I would need a solution to this issue as well.我也需要解决这个问题。 Here is my minimum example expected to work:这是我希望工作的最小示例:

 --- title: "Testing LaTeX packages in Docx format" output: word_document header-includes: - \\usepackage{xfrac} --- $$ \\sfrac{1}{2} $$ ```

In this case the outlined answer with MathJax will not work so well.在这种情况下,MathJax 的概述答案将无法正常工作。 sfrac is not supported by MathJax ( http://docs.mathjax.org/en/latest/input/tex/macros/index.html ) MathJax 不支持 sfrac ( http://docs.mathjax.org/en/latest/input/tex/macros/index.html )

I don't know if you need \\sfrac specifically, but \\frac or \\tfrac would be supported.我不知道您是否特别需要 \\sfrac,但会支持 \\frac 或 \\tfrac。 Maybe you can use these instead.也许你可以用这些来代替。

Here an example with \\frac这里有一个 \\frac 的例子

 ---
 title: "Frac Test"
 header-includes: 
  - \usepackage{xfrac}
 output:
  pdf_document: default
  html_document: default
  word_document: default
---

$$
   \frac{1}{2}+1
$$

Output of \\frac example html: \\frac 示例 html 的输出:

在此处输入图片说明

This will work for .pdf / word and html.这适用于 .pdf/word 和 html。

Here an example with \\tfrac这里有一个 \\tfrac 的例子

---
title: "TFrac Test"
header-includes: 
 - \usepackage{xfrac}
output:
  pdf_document: default
  html_document: default
  word_document: default
---

$$
   \tfrac{1}{2}+1
$$

Output \\tfrac example html:输出 \\tfrac 示例 html:

在此处输入图片说明

This will run for html, pdf - for word it will write 1/2 instead of a formula, since there is no \\tfrac equivalent in word.这将为 html、pdf 运行 - 对于 word 它将写入 1/2 而不是公式,因为 word 中没有 \\tfrac 等效项。

\\cfrac and \\dfrac are also supported by MathJax. \\cfrac 和 \\dfrac 也受 MathJax 支持。

You can somehow solve this for most of the important symbols/characters via Mathjax.您可以通过 Mathjax 以某种方式解决大多数重要符号/字符的问题。 Here is a list of supported symbols/characters: http://docs.mathjax.org/en/latest/input/tex/macros/index.html Just look there for the symbol you need.以下是支持的符号/字符列表: http://docs.mathjax.org/en/latest/input/tex/macros/index.html只需在此处查找所需的符号即可。

Then you use it like this:然后你像这样使用它:

$$
\require{mhchem}
\begin{equation}
  [C] + [R] 
  \xrightleftharpoons[k_{-1}]{k_1}
  [CR] + [C] 
  \xrightleftharpoons[k_{-2}]{k_2}
  [C2R]
(\#eq:multiplebinding)
\end{equation}
$$

So you need to enclose your command in $$.因此,您需要将命令括在 $$ 中。 In this example \\xrightleftharpoons was used.在这个例子中使用了 \\xrightleftharpoons。 From the table I linked you above you can see that you need to add \\require{mhchem} in this case.从我上面链接的表格中,您可以看到在这种情况下您需要添加 \\require{mhchem}。 For your case this isn't needed, you just need to write \\mathbb.对于您的情况,这不是必需的,您只需要编写 \\mathbb。

Example:例子:

  ---
title: "TinyTeX Test"
header-includes:
  - \usepackage{mathbbol}
output:
  pdf_document: default
  html_document: default
  word_document: default
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

Hello Mori, this should work


$$
    \mathbb{1}_{S}(x)
$$

Greetings, Steffen

HTML OUTPUT: HTML 输出: 在此处输入图片说明

WORD OUTPUT:字输出: 在此处输入图片说明

Also note, that I replaced bbm with \\usepackage{mathbbol} - otherwise it had errors rendering the pdf for me.另请注意,我用 \\usepackage{mathbbol} 替换了 bbm - 否则它在为我呈现 pdf 时出错。 Here a discussion about bbm on tex.stackexchange ... most people advise against using bbm, since there are better packages to get the symbols https://tex.stackexchange.com/questions/26637/how-do-you-get-mathbb1-to-work-characteristic-function-of-a-set这里讨论了 tex.stackexchange 上的 bbm ......大多数人建议不要使用 bbm,因为有更好的包来获取符号https://tex.stackexchange.com/questions/26637/how-do-you-get- mathbb1-to-work-characteristic-function-of-a-set

Ok, so after Steffen's answers, I've come to the conclusion that the proper answer would be: Including LaTeX packages in docx documents cannot be done (at least up to now).好的,在 Steffen 的回答之后,我得出的结论是,正确的答案是:无法在docx文档中包含 LaTeX 包(至少到目前为止)。

The only solution is to use the equivalent MathJax-supported command/symbol.唯一的解决方案是使用等效的 MathJax 支持的命令/符号。 Nevertheless, Steffen's answer pointed me in the correct direction of the commands supported by MathJax (many thanks!), so I awarded him the bounty.尽管如此,Steffen 的回答为我指明了 MathJax 支持的命令的正确方向(非常感谢!),所以我给了他赏金。

Also, as my specific problem was with diagonal fractions, I tried all the frac variants allegedly supported by MathJax.此外,由于我的具体问题是对角分数,我尝试了据称由 MathJax 支持的所有frac变体。 There are six of them, out of which the following worked: \\dfrac , \\frac , and \\dfrac , and the following did not: \\cfrac , \\flatfrac , and \\genfrac .其中有六个,其中以下有效: \\dfrac\\frac\\dfrac ,而以下无效: \\cfrac\\flatfrac\\genfrac

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM