Search code examples
rknitrpandoc

Using a section as abstract in latex template


I want to create a markdown report with an abstract that can include figures generated by the report itself. The usual way of defining the abstract as a string in the yaml header doesn't work for me.

Essentially, I want to have some part of the document with something like this:

## Abstract

Lorem impsum this is the abstract

```{r, fig.cap = "figure in the abstract"}
plot(pressure)
```

And then have something like this in the latex document

\begin{abstract}
Lorem impsum this is the abstract 
\begin{figure}
\centering
\includegraphics{unnamed-chunk-1-1.pdf}
\caption{figure in the abstract}
\end{figure}
\end{abstract}

Ideally, this would be done by having something like

\begin{abstract}
$body.abstract$   
\end{abstract}

In the latex template. But the problem I'm facing is that for Pandoc, everything that is rendered by knitr is part of the $body$ variable, so I don't know how to access the content of a specific section.

As a workaround, creating a separate abstract.Rmd file that compiles into a latex fragment and then using

\begin{abstract}
\input{abstract.tex}
\end{abstract}

works. But it involves remembering to compile that file first, and pollutes the directory with an extra LaTeX file. Since that will certainly will be a source of errors and confusion for the expected users, I'd like to avoid it.

Is what I'm asking for possible?


Solution

  • Sounds like the abstract-to-meta Lua filter should do exactly what you need.

    This moves a document's abstract from the main text into the metadata.

    Download the abstract-to-meta.lua file and place it into your project directory next to the R Markdown file. Then make sure that it is called by adding a pandoc_args field to your metadata:

    ---
    output: 
      pdf_document:
        pandoc_args: ["--lua-filter=abstract-to-meta.lua"]
    ---