How can I put an executive summary before the TOC using bookdown?

Viewed 477

I have a bookdown project with a custom template.tex and custom my-format.cls. I can do things like the following in my template.tex:

$if(abstract)$
\begin{abstract}
$abstract$
\end{abstract}
$endif$

\tableofcontents

\begin{body}
...

and get the abstract to take my custom formatting. This works in part because the content of the abstract is just one paragraph of text, which can be placed in the YAML front matter.

However, is it possible to include an executive summary before the table of contents?

I know I can use (in my index.Rmd)


# Executive Summary {-}

a summary here

# Introduction

this is the first numbered section

But in that case even though the "Executive Summary" section header is not numbered, it appears after the TOC. Is it possible to modify the template.tex such that using something like:

$if(abstract)$
\begin{abstract}
$abstract$
\end{abstract}
$endif$

$if(executive_summary)$
\begin{executivesummary}
% insert summary content here
\end{executivesummary}
$endif$

\tableofcontents

\begin{body}
...

I can peal off the content (including figures/tables/etc.) from an unnumbered executive summary section (even if it has to come from a different .Rmd file) and assign it to some (pandoc) variable that could reference in place of % If I insert summary content here above? I would also need this same content removed from the default assigned (pandoc) $body$ variable.

1 Answers

Thanks @tarleb! I suspect that I could get what I want via a Lua filter, but I think I found an easier way along the lines of your answer https://stackoverflow.com/a/53885034/1785752

In the index.Rmd YAML I added

executivesummary: |
  
    ```{r, echo=FALSE, results='asis'}
    res <- knitr::knit_child(quiet = TRUE, 'executive-summary.Rmd')
    cat(res, sep = '\n')
    ```

This requires a few things though:

  • That the "Shaded" environment is defined in the preamble of template.tex
$if(highlighting-macros)$
$highlighting-macros$
$endif$
  • That the executive-summary.Rmd is defined, and has no YAML and no level 1 header (the "Executive Summary" heading comes from the template/class file)

Interestingly, I could just put content in the executivesummary YAML section (i.e., skip the child document rendering), but code chunk options that include strings sometimes get parsed as YAML keys in this case, which causes the rendering to fail. Also, 2-space indented code chunks get no IDE help for syntax checking or auto-complete.

Related