1

I'd like to include LaTeX environments (e.g., algorithmic from algorithmicx, mini from optidef, dcases from mathtools, etc.) in my .Rmd bookdown file. This is no problem for pdf output. But these do not render for html or docx output.

My current hack solution:

  1. Generate the .pdf output.
  2. Screen shot, edit, save images of interest as png
  3. Include images conditional on output not being LaTeX

Downsides:

  1. Obviously doesn't scale
  2. Images are ugly in docx and html output
  3. Screws with figure cross-referencing

There has to be a better approach, right? I was thinking that there's a way to tell rmarkdown/LaTeX that, when rendering as pdf, certain code chunks should be saved in some image format. That way they could be added back into the document as images conditional on the output document being docx or html. Is that even possible?

UPDATE: An answer to Standalone diagrams with TikZ suggests an approach involving the LaTeX standalone package. But unfortunately, it's discovered over at standalone does not work with algorithms that this does not work for the algorithm environment. Any ideas?

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

enter image description here

lowndrul
  • 3,715
  • 7
  • 36
  • 54

1 Answers1

5

For math, R Markdown uses MathJax, and only a subset of LaTeX is available. This subset includes the basic math macros and environments, and allows you to define new macros, but doesn't support everything necessary to let you use arbitrary LaTeX packages. See http://docs.mathjax.org/en/latest/tex.html for details.

You might be able to create an environment that looks something like algorithm or algorithmic, but it's going to be a lot of work, and likely won't look as nice.

You should probably choose between PDF output with all of LaTeX available for formatting, or some flavour of HTML output with less style. For example, you could write your algorithm as

******
**Algorithm 1**:  My algo

******
1.  Do this.
2.  Do that.

******

and it will display as


Algorithm 1: My algo


  1. Do this.
  2. Do that.

user2554330
  • 37,248
  • 4
  • 43
  • 90
  • 1
    Unfortunately, that's not really an option in my org. I need each of pdf, html, and docx output for separate purposes. I was thinking that there's a way to tell rmarkdown/LaTeX that, when rendering as pdf, certain code chunks should be saved as .png. That way they could be added back into the document as images conditional on the output document being docx or html. I'll include this in my original question. – lowndrul May 19 '19 at 16:10
  • 3
    PNG is a bitmap format, causing your scaling problems. You might try SVG instead. I don't think LaTeX can produce SVG directly, but there's a `pdf2svg` utility that does a reasonable looking job on `pdflatex` output targetting HTML. I don't know how well Word would handle the SVG files. – user2554330 May 19 '19 at 17:45