Skip to content

Instantly share code, notes, and snippets.

@scrogster
Created April 20, 2018 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scrogster/ea395d3daecaf6e5a33433630fbf2d16 to your computer and use it in GitHub Desktop.
Save scrogster/ea395d3daecaf6e5a33433630fbf2d16 to your computer and use it in GitHub Desktop.
Rmarkdown numbered equation helper
#I write papers and reports using Rmarkdown, knitting to pdf.
#My co-authors use word, so I usually knit a version to docx for them to edit.
#This mostly works flawlessly, except when I use \begin{align} and \end{align} to delimit display equations.
#Using \begin{align} and \end{align} results in nicely centered and numbered equations in pdf, BUT
#the equations don't render at all when I try to make a docx.
#As a quick hack so I can make a passable docx to share with my co-authors, the following script replaces all instances
#of \begin{align} and \end{align} with "$$". The modified version of the original Rmarkdown file knits to docx just fine,
# albeit without equation numbers.
convert_align<-function(infile, outfile){
x <- readLines(infile)
x[grep("align}", x)]<-"$$"
cat(x, file=outfile, sep="\n") }
#make a version that will render as docx
convert_align("temp.Rmd", "temp_for_word.Rmd")
#render the pdf
rmarkdown::render("temp.Rmd", "pdf_document")
#render the docx
rmarkdown::render("temp_for_word.Rmd", "word_document")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment