# Quarto custom template - extra commands at start of $body$...

Hello.

I'm customizing template.tex to generate a PDF using scrbook and have run into a problem. The following lines are added to my output TEX file just before the $body$ content:

  \bookmarksetup{startatroot}

\hypertarget{section}{%
\chapter{}\label{section}}


The second command is problematic because it adds an empty numbered chapter at the start of my PDF, which also messes up my chapter numbering, ToC, etc.

This code is not coming from any of the provided partials, or the main template, so I'm unable to edit them.

Is there any way for me to address this?

Can you show us the yaml you use?
"Following lines are added" : you mean they appear when you use the template.tex otherwise they are not there? If that is the case show us also the template.tex file.
Or do you want to create a template.tex file to avoid the problem?

I'm using a modified version of the template.tex provided on the Quarto github page. I do not include toc.tex but have moved some of its code into the template. I've not needed to modify any of the other partials. These changes were made so that I can use QMDs for the front matter, which are imported in the required index.qmd. This also allows me to avoid setting \frontmatter, so I can have roman page numbers throughout, as required by the style guide.

I'm saying that the lines quoted above appear in my output.tex, but I cannot account for them in the provided template or partials. I've not added them.

Here's the YAML:

project:
title: "Project Title"
type: book
output-dir: manuscript

book:
title: "Book Title"
subtitle: "Book Subtitle"
author: "My Name"
date: today
chapters:
- index.qmd
- 21-intro.qmd
- 22-lit_review.qmd
- 23-prob_statement.qmd
- 24-methods.qmd
- 25-results.qmd
- 26-conclusions.qmd
- 27-references.qmd
appendices:
- 31-firstapp.qmd

bibliography: references.bib

format:
pdf:
toc: true
toc-depth: 3
lof: true
lot: true
# Numbering
number-sections: true
# Fonts
mainfont: Georgia
sansfont: Verdana
monofont: Source Code Pro
fontsize: 12pt
microtypeoptions: ["final"]
# Colors
# Layout
documentclass: scrbook
classoption:
- oneside
- open=any
geometry:
- margin=1in
- bindingoffset=0.5in
- heightrounded
- footskip=0.475in
- showframe
pagestyle: plain
# Rendering
template: tex/template.tex
keep-tex: true


And the relevant, annotated portion of my modified template.tex:

% above here template is mostly unchanged
$title.tex()$

\begin{document}
\mainmatter

$if(toc)$
% snip - as in toc.tex, except tableofcontents made in index.qmd
$endif$

$if(linestretch)$
\setstretch{$linestretch$}
$endif$

%%% Include chapters and appendices, starting with index.qmd
$body$


My index.qmd, which should appear at the start of $body$ in the template:

<!-- Redefine KOMA Chapter styling in prelim material -->
\RedeclareSectionCommand[beforeskip=0pt, afterskip=24pt]{chapter}
\setkomafont{chapter}{\large\sffamily}
\renewcommand\raggedchapter{\centering}

{{< include 11-titlepage.qmd >}}
{{< include 12-abstract.qmd >}}
{{< include 13-acknowledgements.qmd >}}

<!-- Moved here from toc.tex -->
\RedeclareSectionCommand[beforeskip=0pt, afterskip=48pt]{chapter}

\tableofcontents

\listoftables

\listoffigures

\RedeclareSectionCommand[beforeskip=0pt, afterskip=24pt]{chapter}


And the relevant portion of the resulting output.tex file, with comments added to explain:

% from my version of the template
\begin{document}
\mainmatter

% from ToC setup in my version of the template
{
\setcounter{tocdepth}{2}
}

% these lines are not from my template or index.qmd
\bookmarksetup{startatroot}

\hypertarget{section}{%
\chapter{}\label{section}}

% the next lines come from index.qmd
\RedeclareSectionCommand[beforeskip=0pt, afterskip=24pt]{chapter}
\setkomafont{chapter}{\large\sffamily}
\renewcommand\raggedchapter{\centering}


Unless I'm missing something (entirely possible - my first real foray into Quarto, LaTeX, and Pandoc), these lines are being added by a part of the build process that I don't have access to.

Hope that makes sense.

Hello @olearydj ,
You are right that it is a (for me complicated) mix of Quarto, LaTeX, and Pandoc.
That said, it is still not clear to me which template you actually changed :

Is that https://github.com/quarto-dev/quarto-cli/blob/main/src/resources/formats/pdf/pandoc/template.tex ?
When I tried to create a scrbook with the default template (whatever it is) I saw the same bookmarksetup statement that you report, but that is not in the template I mentioned in the previous sentence. My partial tex output verses yours:

%% mine:
\bookmarksetup{startatroot}

\hypertarget{preface}{%
\chapter*{Preface}\label{preface}}

%%yours:

\bookmarksetup{startatroot}

\hypertarget{section}{%
\chapter{}\label{section}}


Finally I assume you are aware of https://quarto.org/docs/journals/templates.html ?
By the way a strange location (name) for this information.

Thank you for having a look.
Before I forget, please clarify your last statement - "strange location (name) for this information". Are you referring to my post? Is there a better community for Quarto discussions? I couldn't find one. If just the post name and content, I concede it is a bit opaque. Often understanding and concisely communicating a problem is the hardest part of solving it.

I could have just said this:

Create a default quarto book project and render the PDF with debug on.
Note that the following lines, which are included in the output TEX, are not found in any of the provided templates.
Is there any way to work around this behavior and/or avoid the initial Preface chapter?

Anyway...
Yes - my template.tex is based on the one in the link you provided.

The difference between my output and yours is only that the new section is called Preface in yours and has no name in mine. That is because I eliminated the Preface header and content from intro.qmd.

The creation of this code is not handled by the provided templates. Your findings support this: "not in the template I mentioned". It is also not in my changes. It is added somewhere else in the build process, hard coded to the first H1 in intro.qmd. As such, this additional section cannot be removed.

For now, thanks in part to this discussion, I've worked around the problem by making the following changes:

1. Added # Preface {.unnumbered .unlisted} to the top of my intro.qmd, removing it from the ToC and correcting the numbering of subsequent sections.
2. Added \setcounter{page}{1} to the top of my titlepage QMD document, which makes it the first page of the document.

The resulting PDF includes a Preface page (header only), which I can delete until a real fix is possible.

Yes, I'm aware of the provided templates, thank you. Unfortunately, the dissertation style guide at my university has some unconventional requirements (i.e. prelim pages numbered with arabic) that forced some changes.

Also, I'm trying to make a Quarto project that can be easily used by anyone that follows me here, without mucking about in TEX files and such. I'm close to having it where anyone can simply fill out the prelim pages and chapters and press render.

Thanks again for your consideration and assistance. Happy holidays!

Sorry for being not clear.
I meant that I would have expected the Quarto information about templates (the link) to be in a place that is easier to find in the Quarto documentation.

Good luck with the project!

1 Like

I agree that the Quarto documentation could be better organized.

Yes they are being added as part of the build process. Specifically Lua filter for example, to power some of the feature of Quarto

What is the content of this file ? Do you mean index.qmd ?
Does index.qmd has a title chapter ?

Either heading is expected by Quarto, or an empty title is seen and is numbered somehow...

Possibly a bug report to make in Quarto-cli repo GitHub - quarto-dev/quarto-cli: Open-source scientific and technical publishing system built on Pandoc.

Please do send us feedback in our Discussion channel: Discussions · quarto-dev/quarto-cli · GitHub

Hello, again, and thanks for the detailed feedback.

Yes, I meant index.qmd, the contents of which are below. Note that my version of template.tex does not include the toc.tex partial.

The main goals of this approach are:

• control numbering style in preliminary pages: arabic where the title page is 1 but unnumbered
• control type and ordering of preliminary pages: title page, abstract, acknow., ToC/T/F
• make it easier for other users to create preliminary pages as QMDs - I believe this is essential to get average users to initially adopt Quarto... title pages are almost always custom, which creates an immediate pain point for anyone that thinks "let's try the title page first, it will be easy"

Now that I've done all this, and learned a lot along the way, I'm sure I could do it in a more straightforward fashion (e.g., use \pagenumbering{}), but this works for now.

I do wish there was a way to avoid creating this Preface chapter. Maybe it is best to use it as an optional cover page with my method.

Do you feel "make index.qmd optional for pdf output" or "move creation of Preface chapter into templates" are reasonable bugs / feature requests to report?

re: documentation - once I better know my way around all this the docs may seem better organized, or at least I'll be able to provide better feedback

<!-- Work-around for hard-coded preface in Quarto -->
# Delete Me {.unnumbered .unlisted}
This could be used for a cover page.

<!-- Redefine KOMA Chapter styling in prelim material -->
\RedeclareSectionCommand[beforeskip=0pt, afterskip=24pt]{chapter}
\setkomafont{chapter}{\large\sffamily}
\renewcommand\raggedchapter{\centering}

{{< include 11-titlepage.qmd >}}
{{< include 12-abstract.qmd >}}
{{< include 13-acknowledgements.qmd >}}

<!-- Moved here from toc.tex -->
\RedeclareSectionCommand[beforeskip=0pt, afterskip=48pt]{chapter}

\tableofcontents

\listoftables