R Markdown - Questions and Answers


I guess it is time for a markdown category. Here is my first question:

When I am creating equations, e.g. $X = coef*b$, how do I insert an object previously created as part of a coded analysis? In the example, I would like to replace coef with a value stored in coef[1]. Any suggestions?




Sorry I don’t know that package so it might be stupid but have you tried to use the fonction print(coef[1])?


Here is one solution using markdown


Cool! Thanks! Already used your example…Not sure though if it is worth it to write equations in markdown that way. It gets painfully verbose:

$MRLM =\frac{e^{r round(coef(best.model.4)[1],3)+r round(coef(best.model.4)[2],3)*log(x)+r round(coef(best.model.4)[3],3)*log(x1)+r round(coef(best.model.4)[4],3)*log(x2)+r round(coef(best.model.4)[5],3)*log(x3)}}{(1+e^{r round(coef(best.model.4)[1],3)+r round(coef(best.model.4)[2],3)*log(x4)+r round(coef(best.model.4)[3],3)*log(x5)+r round(coef(best.model.4)[4],3)*log(x6)+r round(coef(best.model.4)[5],3)*log(x7))}}$

To get:


Can you build the equation beforehand, in a chunk perhaps, then evaluate it when needed?


I am not sure if I get you right…would this make the code less verbose? As far as I understand, the code would be just somewhere else…


You could do all the rounding in 1 step. Even if it did not make the code any less verbose it would separate the building of the equation from using it. That would not interrupt the flow of the paragraph so much. But it is a long equation - you are not going to be able to get around that.


Nice idea! Now I get it…thanks!


Hello everyone,

I am rendering a docx in R markdown and I would like to learn more about aligning text and other contents. in this case I am trying to center my equations. I can’t work out how to do it. Following code:

<div id="LMMR" class="equation">


  1. log Band1/Band2=log(Band1)-log(Band2)

I definitely want to keep the numbering.

Any suggestions?

Thanks for your help,



Hi Rene. Seeing as you are embedding HTML directly into the markdown, you could try changing the DIV’s CSS style using “style”. This is untested, but try:

<div id="LMMR" class="equation" style="width:800px; margin:0 auto;">


Hi Rene,

You just need to replace one dollar sign ($) with two dollar signs ($$) to centre the equation.



(@LMMR) $$log\frac{Band1}{Band2}=log(Band1)-log(Band2)$$


Pfff…really? o.O Thanks for responding Drew and Josh! It is a bit puzzling what syntax is possible in markdown and then for what output type this is true…

I will get there…



RMarkdown is using Pandoc to do the conversion so if you look up documentation for pandoc, you will find what you need - http://rmarkdown.rstudio.com/authoring_pandoc_markdown.html#math. In short, it allows $ and $$ in most output formats but some do a terrible job of rendering it. PDF will be (by far) the best supported output for anything in $ or $$. Plus, using PDF output should (I haven’t actually tried it) support arbitrary LaTeX code - giving you total control over your output.


:slight_smile: Thanks, Robert! That’s very helpful! Ina told me about your meeting…great to have another enthusiastic peer on board. Welcome!