Centering Image and Text in R Markdown for a PDF Report

Align text and images in a table - pdf output

I have used the workaround described by @Lyngbakr in the past kable: Vertical alignment does not work with pdf output

---
site: bookdown::bookdown_site
documentclass: book
output:
bookdown::pdf_book:
toc: false
header-includes:
- \usepackage[export]{adjustbox}
delete_merged_file: true
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, message=FALSE, cache=TRUE)
library(kableExtra)
library(knitr)
```

# Hello World
I think the world is flat

```{r}
flag = "http://flagpedia.net/data/flags/mini/gb.png"
download.file(flag,'flag.png', mode = 'wb')

tbl_img <- data.frame(
col1 = c("row1", "row2"),
col2 = c("\\includegraphics[valign=T,scale=2.5,raise=2mm]{flag.png}",
"\\includegraphics[valign=T,scale=2.5,raise=2mm]{flag.png}"),
col3 = c(
"here is a whole bunch of text to show how the images and text in the table are not properly aligned.",
"here is a whole bunch of text to show how the images and text in the table are not properly aligned."))
tbl_img %>%
kbl(booktabs = T, escape = F) %>%
kable_styling(full_width = T)
```

Sample Image

R: how to center output in R markdown

The following code worked for me.

\center Centered Text \center

And, if you need to put it in bold, as my case, you can use the underline (two at the beggining, two at the end):

\center __Centered Text__ \center

How to Centre code output (text) to the middle of a page using Knitr pdf output?

Due to the fact that your output is of class table you can use the following workaround (note to install the two libraries kableExtra and knitr):

library(kableExtra)
library(knitr)

kable_styling(kable(summary(cars)), position = "center")

Sample Image

In addition to that I highly recommend the following PDF: https://haozhu233.github.io/kableExtra/awesome_table_in_pdf.pdf

If you want to center text:

\begin{center}
centered text
\end{center}

Centering image in RMarkdown for Word export

This is not fully satisfactory, but what I did in the end was to add a macro in my styles document:

Sub Center_All_Images()
'
' Center_All_Images Macro
'
'
For Each oILShp In ActiveDocument.InlineShapes
oILShp.Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Next

End Sub

I still need to run the macro manually. I wish it was run automatically when the document is created from the template.

R Markdown - Vertical Text Alignment with Inline Images with Latex PDF

You could use LaTeX commands, like minipage to create two spaces side by side, one for the title and one for the image. In the example below, I replaced pdf_document by bookdown::pdf_document2: to show that this solution allows section numbering:

---
output:
bookdown::pdf_document2:
latex_engine: xelatex
toc: false
geometry: margin=1.5cm
papersize: a4
pagestyle: empty
fontsize: 12 pt
---

\begin{minipage}{.5\textwidth}
\section{Left Aligned Title}
\end{minipage}
\begin{minipage}{.5\textwidth}
```{r, echo=FALSE, fig.align='right', out.height='8%'}
knitr::include_graphics("logo.png")
```
\end{minipage}



Related Topics



Leave a reply



Submit