Haribo
Haribo

Reputation: 2226

Put a table before title in rmarkdown pdf document

I would like to have a table above the title in the first page of my pdf report generated by rmarkdown :

enter image description here

---
title: "test"
output: pdf_document
date: '2023-01-03'
---

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

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r cars}
summary(cars)
```

## Including Plots

You can also embed plots, for example:

```{r pressure, echo=FALSE}
plot(pressure)
```

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

I know in Latex one could use the following :

\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{multicol,lipsum}
\title{test title }       
\author{testUser}
\date{\today}

\begin{document}
\noindent
\begin{center}
\begin{tabular}{ |c|c|c| } 
 \hline
 cell1 & cell2 & cell3 \\ 
 cell4 & cell5 & cell6 \\ 
 cell7 & cell8 & cell9 \\ 
 \hline
\end{tabular}
\end{center}


\begingroup
\let\newpage\relax
\maketitle
\endgroup
\end{document}

which then would do the trick, but in rmarkdown we do not use the \maketitle command !

Upvotes: 1

Views: 308

Answers (1)

If your tex distribution is reasonable up-to-date, you can use hooks:

---
title: "test"
output: 
  pdf_document:
    keep_tex: true
date: '2023-01-03'
header-includes:
  - \AddToHook{cmd/maketitle/before}{\begin{center}\begin{tabular}{ |c|c|c| }\hline cell1 & cell2 & cell3 \\ cell4 & cell5 & cell6 \\  cell7 & cell8 & cell9 \\  \hline \end{tabular}\end{center}\begingroup \let\newpage\relax}
  - \AddToHook{cmd/maketitle/after}{\endgroup}
---

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

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r cars}
summary(cars)
```

## Including Plots

You can also embed plots, for example:

```{r pressure, echo=FALSE}
plot(pressure)
```

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

enter image description here

Upvotes: 1

Related Questions