Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Centering image and text in R Markdown for a PDF report

I want to center an image and/or text using R Markdown and knit a PDF report out of it.

I have tried using:

->Text<-  ->![](image1.jpg)<- 

That does not do the trick! Any other way of getting this done?

like image 709
Next Door Engineer Avatar asked Jul 10 '14 13:07

Next Door Engineer


People also ask

How do I center an image in R Markdown?

To center an image using the knitr::include_graphics() function, include it within an R code chunk that has the fig. align='center' option (and perhaps other options to control width, etc.).

How do you markdown a PDF in R?

To transform your markdown file into an HTML, PDF, or Word document, click the “Knit” icon that appears above your file in the scripts editor. A drop down menu will let you select the type of output that you want. When you click the button, rmarkdown will duplicate your text in the new file format.

How do I indent text in R Markdown?

Text can be indented two ways depending on if the indent is within a list or not. Within a list, four spaces at the begin of the line indicates the text is to be indented one nesting level. Use four additional spaces for each additional nesting level. To indent text which is not in a list, use a block quote.


2 Answers

If you are centering the output of an R code chunk, e.g., a plot, then you can use the fig.align option in knitr.

```{r fig.align="center"} plot(mtcars) ``` 
like image 121
Lincoln Mullen Avatar answered Oct 04 '22 06:10

Lincoln Mullen


I had the same question. I have tried all solutions provided above and none of them worked... But I have found a solution that works for me, and hopefully for others too.

<center>  ![your image caption](image.png)  </center> 

This code will center both the image and the caption. It is essential that you leave lines between <center>, the image code, and </center>, otherwise the image will be centered but the caption will disappear.

If you want your image to have a clickable link, you can embed things like

[![your image caption](image.png)](www.link_to_image.com) 

However, the caption will no longer appear.

So if you want a clickable caption you will have to do it in two steps:

<center>  ![](image.png)  [your image caption](www.link_to_image.com)  </center> 

Same here, make sure there are empty lines in between each command ones. If you want both the image and the caption to be clickable, then combine the middle and the last codes above. I hope this helps a bit.

like image 37
François Birgand Avatar answered Oct 04 '22 05:10

François Birgand