Chapter starts with 0.1 (not 1.0) in pdf_book format

Hi all,

When I build the pdf_book (documentclass: krantz),

---
title: "title"
author: "KIM"
date: "`r Sys.Date()`"
knit: "bookdown::render_book"
documentclass: krantz
site: bookdown::bookdown_site
fontsize: 12pt
monofont: "Source Code Pro"
monofontoptions: "Scale=0.7"
bibliography: [book.bib, packages.bib]
biblio-style: apalike
link-citations: yes
description: "DrPH Multivariable Analysis."
---

The output (the table of contents and the chapter) starts with 0.1 instead of 1.0 (see image below).

But when I use documentclass::book (see below), bookdown::pdf_book renders the numbering of chapters correctly (FYI, in all the Rmd files I used the single # as the heading for chapter).

--- 
title: "title"
author: |
  | Kamarul Imran
  | Assc Prof (Epidemiology and Statistics)
date: "`r Sys.Date()`"
knit: "bookdown::render_book"
documentclass: book
site: bookdown::bookdown_site
fontsize: 12pt
monofont: "Source Code Pro"
monofontoptions: "Scale=0.7"
lot: yes
lof: yes
bibliography: [book.bib, packages.bib]
biblio-style: apalike
link-citations: yes
description: "DrPH Multivariable Analysis."
---

image

The latex preamble (which was taken almost entirely from bookdown: Authoring Books and Technical Documents with R Markdown preamble latext) is written as below;

\usepackage{booktabs}
\usepackage{longtable}
\usepackage[bf,singlelinecheck=off]{caption}

\usepackage{framed,color}
\definecolor{shadecolor}{RGB}{248,248,248}

\renewcommand{\textfraction}{0.05}
\renewcommand{\topfraction}{0.8}
\renewcommand{\bottomfraction}{0.8}
\renewcommand{\floatpagefraction}{0.75}

\renewenvironment{quote}{\begin{VF}}{\end{VF}}
\let\oldhref\href
\renewcommand{\href}[2]{#2\footnote{\url{#1}}}

\ifxetex
  \usepackage{letltxmacro}
  \setlength{\XeTeXLinkMargin}{1pt}
  \LetLtxMacro\SavedIncludeGraphics\includegraphics
  \def\includegraphics#1#{% #1 catches optional stuff (star/opt. arg.)
    \IncludeGraphicsAux{#1}%
  }%
  \newcommand*{\IncludeGraphicsAux}[2]{%
    \XeTeXLinkBox{%
      \SavedIncludeGraphics#1{#2}%
    }%
  }%
\fi

\makeatletter
\newenvironment{kframe}{%
\medskip{}
\setlength{\fboxsep}{.8em}
 \def\at@end@of@kframe{}%
 \ifinner\ifhmode%
  \def\at@end@of@kframe{\end{minipage}}%
  \begin{minipage}{\columnwidth}%
 \fi\fi%
 \def\FrameCommand##1{\hskip\@totalleftmargin \hskip-\fboxsep
 \colorbox{shadecolor}{##1}\hskip-\fboxsep
     % There is no \\@totalrightmargin, so:
     \hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}%
 \MakeFramed {\advance\hsize-\width
   \@totalleftmargin\z@ \linewidth\hsize
   \@setminipage}}%
 {\par\unskip\endMakeFramed%
 \at@end@of@kframe}
\makeatother

\makeatletter
\@ifundefined{Shaded}{
}{\renewenvironment{Shaded}{\begin{kframe}}{\end{kframe}}}
\makeatother

\newenvironment{rmdblock}[1]
  {
  \begin{itemize}
  \renewcommand{\labelitemi}{
    \raisebox{-.7\height}[0pt][0pt]{
      {\setkeys{Gin}{width=3em,keepaspectratio}\includegraphics{images/#1}}
    }
  }
  \setlength{\fboxsep}{1em}
  \begin{kframe}
  \item
  }
  {
  \end{kframe}
  \end{itemize}
  }
\newenvironment{rmdnote}
  {\begin{rmdblock}{note}}
  {\end{rmdblock}}
\newenvironment{rmdcaution}
  {\begin{rmdblock}{caution}}
  {\end{rmdblock}}
\newenvironment{rmdimportant}
  {\begin{rmdblock}{important}}
  {\end{rmdblock}}
\newenvironment{rmdtip}
  {\begin{rmdblock}{tip}}
  {\end{rmdblock}}
\newenvironment{rmdwarning}
  {\begin{rmdblock}{warning}}
  {\end{rmdblock}}

\usepackage{makeidx}
\makeindex

\urlstyle{tt}

\usepackage{amsthm}
\makeatletter
\def\thm@space@setup{%
  \thm@preskip=8pt plus 2pt minus 4pt
  \thm@postskip=\thm@preskip
}
\makeatother

\frontmatter

What have I done wrong? Really appreciate your time in attending this problem. Thank you.

KIM

Hi,

can you format your question correctly to help us understand better and see what your rmd file really look like ?

You can use 5 backticks instead of 3 to be able to include correctly the R code chunk

---
title:"test"
---
Text
# code chunk
```{r, eval = FALSE}
cat("test")
```

For your issue, it could be related to not having a title 1 in your Rmarkdown. If you begin with a ## for the first title in your document, and not a title with #, it will began the count to 0. However, not seing your code correctly, I am not sure... :thinking:

2 Likes

Hi Christophe,

Thanks for your advice. I have edited my post and I think it looks much better now.

I realized the chapter number starts with zero when I use documentclass: krantz in the yaml metadata. If I use documentclass: book, the chapter number starts correctly with 1. I pressume the issue lies either within the krantz.cls or preamble.tex.

I love to use krantz documentclass because it looks more beautiful than the book documentclass.

Thanks again.

KIM

Hi Christophe,

I have found the solution, that is by adding this (below) in the _output.yml

pandoc_args: --top-level-division=chapter

So, the _output.yml becomes

bookdown::gitbook:
  config:
    toc:
    download: [pdf, epub]
bookdown::html_chapters:
  css: [style.css, toc.css]
bookdown::pdf_book:
  includes:
    in_header: preamble.tex
    before_body: before_body.tex
    after_body: after_body.tex
  latex_engine: xelatex
  citation_package: natbib
  keep_tex: yes
  pandoc_args: --top-level-division=chapter
  toc_depth: 3
  toc_unnumbered: no
  toc_appendix: yes
bookdown::epub_book:
  dev: svglite
  stylesheet: style.css
3 Likes

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

1 Like

Thanks for the advice. I did that just now.

KIM

1 Like

I think that this may be then an issue with the bookdown book preface and the class book. Not using documentclass: krantz but :book I have exactly the same issue, and I have no idea how to come around it. The solution you suggest does not help.