Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201894

How to use same R markdown source to go to HTML, Word, github markdown, and PDF

$
0
0

I am trying to render the same R markdown source document to MS word, git-hub markdown, HTML, and PDF. Options for the compilation are in the header of the .Rmd file, and the choice of output is determined at the command line through the render() function. I would like this document to include figure- and equation numbering in all versions, and have cross-references.

I have an equation in my .Rmd file:

(\#eq:eq)
A=\frac{\pi}{27d^2}

(let's ignore the wrapper for now.)

This is crossreferenced from the text as (e.g. Equation \@ref(eq:eq)).

My header in main.rmd looks like this:

output:
  bookdown::pdf_document2:
    latex_engine: pdflatex
    toc: true
    number_sections: true
    fig_caption: true
    keep_tex: true
    citation_package: natbib
  bookdown::html_document2:
    number_sections: true
    toc: true
    toc_float:
      collapsed: false
      smooth_scroll: false
  md_document:
    variant: gfm
    fig_caption: yes
    keep_md: yes
    number_sections: true
  bookdown::word_document2:
    number_sections: true
    reference_docx: defaulto

When I render with word...

rmarkdown::render("main.rmd",
  output_format=c('word_document2'),
  run_pandoc = TRUE,
  clean=TRUE)

If I use $$ as my wrapper I see the equation label rendered in the Word document. The equation number is given but it is not a cross-reference. I have to use \begin{equation}... \end{equation} to get that to work as a cross reference.

When I render to HTML, either wrapper option gives me reasonable output.

rmarkdown::render("main.rmd",
  output_format=c('bookdown::html_document2'),
  run_pandoc = TRUE,
  clean=TRUE)

When I render to markdown,

rmarkdown::render("main.rmd",
    rmarkdown::md_document(variant = "gfm"),
    run_pandoc = TRUE,
    clean=TRUE,
    knit_root_dir= getwd())

I don't get any output when I use ´´´\begin{equation} ... \end{equation}. There I have use $$ .. $$ to get any output, and I see Equation @ref(eq:eq) in the text, i.e. the cross reference does not render at all.

The PDF is rendered using

rmarkdown::render("main.rmd",
  output_format=c('bookdown::pdf_document2'),
  run_pandoc = TRUE,
  knit_root_dir= getwd())

... and seems to work fine in both cases.

So... problem: I cannot use the same equation format to go to markdown, HTML, and word from .Rmd. Is there a solution that can be implemented either in the .Rm source, or when calling render()?

MWE

title: "MWE"
author: "Me, myself, and I"
date: '`r Sys.Date()`'
output:
  bookdown::pdf_document2:
    latex_engine: pdflatex
    toc: true
    number_sections: true
    fig_caption: true
    keep_tex: true
    citation_package: natbib
  bookdown::html_document2:
    number_sections: true
    toc: true
    toc_float:
      collapsed: false
      smooth_scroll: false
  md_document:
    variant: gfm
    fig_caption: yes
    keep_md: yes
    number_sections: true
  bookdown::word_document2:
    number_sections: true
    reference_docx: default
fontsize: 11pt
geometry: margin=1in
graphics: yes
bibliography: main.bib
linkcolor: blue
urlcolor: red
citecolor: cyan
link_citations: true

# Test{sec:test}

This should be a cross-reference to Equation \@ref(eq:eq).

$$
(\#eq:eq)
A=\frac{\pi}{27d^2}
$$


Viewing all articles
Browse latest Browse all 201894

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>