awenborn
awenborn

Reputation: 417

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

I'm trying to create an R Markdown script to routinely export PDF reports of data and I'm struggling with the most basic formatting tasks here!

First of all I just want to create a page header with a left-aligned title and a right-aligned organisation logo on the same line; the logo is approximately 2-line heights, so I also want to top-align the title. In HTML/CSS this would be like a 10 second task, but I've been struggling with this all morning!

So this is my current effort:

---
output:
  pdf_document:
    latex_engine: xelatex
geometry: margin=1.5cm
papersize: a4
pagestyle: empty
fontsize: 12 pt 
---

<!-- Inline But No Scaling or Alignment   -->
# Left Aligned Title ![](C:/logo.png){ height=65px }

<!-- Aligned but Not Compatible with Inline Title -->
```{r, echo=FALSE, fig.align='right', out.height='8%'}
knitr::include_graphics("C:/logo.png")
```

I even tried separating into columns but couldn't get it to work. Just wondering if there's any easy way to achieve this format, with a left & top aligned title and a right aligned logo in R Markdown.

enter image description here

Upvotes: 0

Views: 875

Answers (1)

bretauv
bretauv

Reputation: 8567

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}

Upvotes: 1

Related Questions