|
I would like to use child documents in a Quarto project, similar to how child documents can be used in Bookdown. For example, suppose I have a report with one section per species, and I want each species section to be maintained as a separate .qmd file. Ideally, these child documents should be generated automatically from a template and included in the main Quarto document during rendering. What is the recommended way to achieve this in Quarto? |
Replies: 1 comment
|
Quarto does not handle child documents in exactly the same way as Bookdown. In particular, it is better not to think of this as a loop inside a The flandersqmd development version provides remotes::install_github("inbo/flandersqmd#7")We are still fixing some small bugs and documentation files, but the main functions seem to work well. The basic idea is:
This means that the generated child documents only exist temporarily during the rendering process. The source project can therefore remain clean and the generated files do not need to be manually maintained. 1. Create a templateFor example, ### {{species}}
The results for *{{species}}* are shown below.
```{r}
# Analysis for this species
```
The template can contain ordinary Quarto content, code chunks, figures and cross-references. It is recommended to prefix the template with 2. Add markers to the parent documentIn the parent document, add markers indicating where the generated child documents should be inserted: ## Results
<!-- AUTOGENERATED-START -->
> Content between these markers is automatically generated.
> Do not edit manually.
<!-- AUTOGENERATED-END -->The 3. Generate and insert the child documentsThe simplest approach is to use library(flandersqmd)
my_species <- paste("Iris", levels(iris$Species))
autoqmd_prepare(
species = my_species,
label = gsub("\\s", ".", tolower(my_species)),
template = "_species_template.qmd",
child_dir = "child_qmd",
target_file = "iris_chapter.qmd"
)This generates one After the function runs, the relevant part of the parent document will look roughly like: <!-- AUTOGENERATED-START -->
{{< include child_qmd/_qmd_02282edd.qmd >}}
{{< include child_qmd/_qmd_08a15729.qmd >}}
{{< include child_qmd/_qmd_08758dcd.qmd >}}
<!-- AUTOGENERATED-END -->Quarto then renders these included 4. Clean up after renderingBecause the child documents are generated files, they can be removed automatically after rendering using library(flandersqmd)
autoqmd_finalise(
target_files = "iris_chapter.qmd",
child_dirs_rm = "child_qmd"
)This removes the generated child documents and restores the parent document to its original state, with only the autogenerated markers remaining. 5. Automate this with Quarto pre- and post-render hooksThe generation and cleanup can be connected to Quarto's render lifecycle using a project:
type: book
pre-render: pre_render.R
post-render: post_render.RThe pre-render script calls This means that running quarto renderis sufficient to generate, include and subsequently clean up all child documents. |
Quarto does not handle child documents in exactly the same way as Bookdown. In particular, it is better not to think of this as a loop inside a
.qmdfile. Instead, Quarto can compose multiple.qmdfiles using its{{< include >}}shortcode.The flandersqmd development version provides
autoqmd_*()helper functions that automate this workflow.We are still fixing some small bugs and documentation files, but the main functions seem to work well.
We hope to merge this soon with the main package version.
The basic idea is:
.qmdfile containing the structure of one child document..qmdfile per entity from this t…