【问题标题】:Hack in R Markdown or Bookdown for including LaTeX environments which appear in html or docx output?破解 R Markdown 或 Bookdown 以包含出现在 html 或 docx 输出中的 LaTeX 环境?
【发布时间】:2019-05-17 16:26:18
【问题描述】:

我想将 LaTeX 环境(例如,algorithmic 来自 algorithmicxmini 来自 optidefdcases 来自 mathtools 等)包含在我的 .Rmd 预订文件中。这对于 pdf 输出没有问题。但这些不会呈现为 html 或 docx 输出。

我目前的破解解决方案:

  1. 生成 .pdf 输出。
  2. 截屏、编辑、将感兴趣的图像保存为 png
  3. 包含以输出不是 LaTeX 为条件的图像

缺点:

  1. 显然无法扩展
  2. docx 和 html 输出中的图像很难看
  3. 带有图形交叉引用的螺钉

必须有更好的方法,对吗?我在想有一种方法可以告诉 rmarkdown/LaTeX,当呈现为 pdf 时,某些代码块应该以某种图像格式保存。这样,它们可以作为图像添加回文档,条件是输出文档是 docx 或 html。这可能吗?

更新:An answer to Standalone diagrams with TikZ 提出了一种涉及 LaTeX standalone 包的方法。但不幸的是,在standalone does not work with algorithms 发现这不适用于algorithm 环境。有什么想法吗?

index.Rmd

---
title: "Bookdown"
header-includes:
  - \usepackage{float}
  - \floatplacement{figure}{!htb}
  - \usepackage{algorithm}
  - \usepackage{algpseudocode}
output: 
  bookdown::gitbook:
    split_by: none
  bookdown::pdf_book:
    fig_caption: yes
    keep_tex: yes
    toc: no
  bookdown::word_document2: default
site: bookdown::bookdown_site
---

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

Hello zero

# First Chapter 

Hello one

\begin{algorithm}
    \caption{My Algo}
    \begin{algorithmic}[1]
        \State Do this.
        \State Do that.
    \end{algorithmic}
\end{algorithm}

```{r myalgo, echo=FALSE, eval = !knitr:::is_latex_output(), fig.cap="Must have text here. For cross-referencing to work."}
knitr::include_graphics("myalgo.png")
```

Hello two. 

Check out this picture: \@ref(fig:myalgo)

myalgo.png

【问题讨论】:

    标签: r latex r-markdown bookdown


    【解决方案1】:

    对于数学,R Markdown 使用 MathJax,并且只有 LaTeX 的一个子集可用。这个子集包括基本的数学宏和环境,并允许您定义新的宏,但不支持让您使用任意 LaTeX 包所需的一切。详情请见http://docs.mathjax.org/en/latest/tex.html

    您也许可以创建一个类似于 algorithmalgorithmic 的环境,但这需要大量工作,而且看起来可能不会那么好。

    您可能应该在 PDF 输出与所有 LaTeX 可用于格式化,或某种风格的 HTML 输出与较少样式之间进行选择。例如,您可以将算法编写为

    ******
    **Algorithm 1**:  My algo
    
    ******
    1.  Do this.
    2.  Do that.
    
    ******
    

    它会显示为


    算法 1:我的算法


    1. 这样做。
    2. 这样做。

    【讨论】:

    • 不幸的是,这在我的组织中并不是一个真正的选择。我需要每个 pdf、html 和 docx 输出用于不同的目的。我在想有一种方法可以告诉 rmarkdown/LaTeX,当呈现为 pdf 时,某些代码块应该保存为 .png。这样,它们可以作为图像添加回文档,条件是输出文档是 docx 或 html。我将把它包含在我原来的问题中。
    • PNG 是一种位图格式,会导致缩放问题。您可以尝试使用 SVG。我不认为 LaTeX 可以直接生成 SVG,但是有一个 pdf2svg 实用程序可以在 pdflatex 输出目标 HTML 上执行合理的工作。我不知道 Word 将如何处理 SVG 文件。
    猜你喜欢
    • 2019-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多