How Can I Set a Pandoc Option to True by Default in a LaTeX Template

I want to set some options to true by default in my Pandoc LaTeX template. Here is the source code for the colorlinks option:

$if(colorlinks)$
  colorlinks=true,
  linkcolor=$if(linkcolor)$$linkcolor$$else$default-linkcolor$endif$,
  filecolor=$if(filecolor)$$filecolor$$else$default-filecolor$endif$,
  citecolor=$if(citecolor)$$citecolor$$else$default-citecolor$endif$,
  urlcolor=$if(urlcolor)$$urlcolor$$else$default-urlcolor$endif$,
$else$
  hidelinks,
$endif$

I want colorlinks to automatically be set to true unless I set it to false in the YAML metadata. How can I modify this code to do that? Also, I want to do the same for the link-citations option. But link-citations is not present anywhere in my Pandoc LaTeX template, so I'm assuming it is defined by default Pandoc or R Markdown.

Pandoc does not allow that in a template. You have to use the metadata to set value for a call to pandoc. Either using metadata (yaml header, yaml file) or variable at command line.

You could probably do that within your output format. That is what most of R Markdown formats are doing: We have an argument in the format function, and based on its value we are modifying pandoc behavior setting metadata or activating command line flag. You could set this way the value of colorlinks to true by default unless set otherwise by a user.

One advantage is that you don't have to to modify and maintain a custom template.

That would work exactly the same.

This metadata that you set in YAML is probably used within Pandoc citeproc, and not used directly by the template. But with the approach I described above you could also set that by default.

If you can share your project output format function, I could show you how to modify, but basically the steps are :

  • Add argument in your format function
  • If set by user, then use value otherwise use your default
  • Pass the variable value to Pandoc using command line arguments, rmarkdown::pandoc_variable_arg() can help within R to add the argument within the format function.

Hope it helps

Sorry, I don't understand. Should I add the pandoc_args option? I can give you what my Rmd file looks like and can you show me how I can do those steps?

---
author: Amar Al-Zubaidi
colorlinks: true
link-citations: true
output: amaryaml::eisvogel
---

https://www.gnu.org

The amaryaml::eisvogel output format is from my R package. The R code looks like this:

pkg_file <- function(...) system.file(..., package = "amaryaml")

eisvogel <- function(..., number_sections = TRUE, toc = FALSE, toc_depth = 3, template = "eisvogel") {
  tablist <- pkg_file("includes", "tablist.tex")
  bookdown::pdf_document2(number_sections = number_sections, toc = toc, toc_depth = toc_depth,
    template = template, includes = includes(in_header = tablist), ...)
}

Setting value inside the format function would be

eisvogel <- function(..., colorlinks = TRUE, link_citations = TRUE, number_sections = TRUE,
                     toc = FALSE, toc_depth = 3, template = "eisvogel", pandoc_args = NULL) {
  tablist <- pkg_file("includes", "tablist.tex")
  pandoc_args <- c(
    pandoc_args,
    if (colorlinks) rmarkdown::pandoc_variable_arg("colorlinks"),
    if (link_citations) rmarkdown::pandoc_variable_arg("link-citations")
  )
  bookdown::pdf_document2(
    number_sections = number_sections, toc = toc, toc_depth = toc_depth,
    template = template, includes = includes(in_header = tablist), 
    pandoc_args = pandoc_args, ...
  )
}

This way, you have set a default to TRUE, and your user can change it easily to set to FALSE and deactivate.

Variable should not be set in YAML top level but in format

author: Amar Al-Zubaidi
output: 
    amaryaml::eisvogel:
         colorlinks: false
         link_citations: false

Does it make it clearer ?

That makes it much clearer I updated my R file as you showed and it works as intended.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.