rmarkdown is not indenting headers

I have a Rmd document that loop and creates multiple pages

knitr version 1.19

rmarkdown version 1.8

Here is some .rmd code that works and displays proper bookmarks:

---
author:
- name: ""
- name: ""
- affiliation: ""
date: ""
abstract: ""
title: ""
classoption: landscape
output: 
  pdf_document:
    number_sections: false
    dev: pdf
    keep_tex: true
    toc: yes
  fig.pos: H
  fig.width: 11in
  fontsize: 9pt
  geometry: margin=.01in
  fig.lp: 'Fig '
  setspace: singlespacing
  fig.height: 5in
  always_allow_html: yes
header-includes:
- \usepackage[usenames,table]{xcolor}
- \usepackage{amsmath}
- \usepackage{rotating}
- \usepackage{float}
- \usepackage{longtable}
- \usepackage{tabulary}
- \usepackage{array}
- \usepackage{colortbl}
- \usepackage{graphicx}

---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```


\newpage 

# Definitions

## defintiion 1

text for defition

## defintiion 2

text for defition

## defintiion 3

text for defition



```{r myplot, out.width='9in',fig.width=9, fig.height=6,results="asis", message= FALSE, warning = FALSE}

data = data.frame(group = c("A","A","B","B"), subgroup = c("x","y","x","y"), number=c(1,2,3,4), stringsAsFactors = FALSE)
groups = unique(data$group)

# =1
for(i in 1:length(groups)){

cat(paste0("\\newpage\n # ",as.character(groups[i]) ))

cat("\n\n")


subgroups = data[data$group == groups[i],]$subgroup

          for(j in 1:length(subgroups)){

               # j = 1
               cat(paste0("\\newpage\n ## ",groups[i]," subgroup  " ,subgroups[j]))


            cat("\n\n")


          }



}


```

Now when I add a template by putting "template: '../myfolder/mytemplate.tex'" right below the "keep_tex: true" line in the header, this causes the bookmarks to be messed up.

Here is the mytemplate code:

\documentclass[$if(fontsize)$$fontsize$,$endif$$if(lang)$$babel-lang$,$endif$$if(papersize)$$papersize$,$endif$$for(classoption)$$classoption$$sep$,$endfor$]{article}
\usepackage[left=1in,top=1in,right=1in,bottom=1in]{geometry}
\newcommand*{\authorfont}{\fontfamily{phv}\selectfont}
$if(fontfamily)$
\usepackage[$fontfamilyoptions$]{$fontfamily$}
$else$
\usepackage{lmodern}
$endif$


  \usepackage[$if(fontenc)$$fontenc$$else$T1$endif$]{fontenc}
  \usepackage[utf8]{inputenc}
\usepackage{lscape}
\usepackage[usenames,table]{xcolor}
\usepackage{amsmath}
\usepackage{rotating}
\usepackage{float}
\usepackage{longtable}
\usepackage{tabulary}
\usepackage{array}
\usepackage{colortbl}
\usepackage{graphicx}
\usepackage{caption}
\usepackage[hidelinks]{hyperref}

\usepackage{abstract}
\renewcommand{\abstractname}{}   
\renewcommand{\absnamepos}{empty} 

\renewenvironment{abstract}
 {{%
    \setlength{\leftmargin}{0mm}
    \setlength{\rightmargin}{\leftmargin}%
  }%
  \relax}
 {\endlist}

\makeatletter
\def\@maketitle{%
  \newpage
%  \null
%  \vskip 2em%
%  \begin{center}%
  \let \footnote \thanks
    {\fontsize{18}{20}\selectfont\raggedright  \setlength{\parindent}{0pt} \@title \par}%
}
%\fi
\makeatother
\def\tightlist{}

$if(appendix)$
\renewcommand*\thetable{$appendixletter$.\arabic{table}}
\renewcommand*\thefigure{$appendixletter$.\arabic{figure}}
$endif$


\setcounter{secnumdepth}{0}

$if(listings)$
\usepackage{listings}
$endif$
$if(lhs)$
\lstnewenvironment{code}{\lstset{language=r,basicstyle=\small\ttfamily}}{}
$endif$
$if(highlighting-macros)$
$highlighting-macros$
$endif$
$if(verbatim-in-note)$
\usepackage{fancyvrb}
$endif$
$if(tables)$
\usepackage{longtable,booktabs}
$endif$

$if(graphics)$
\usepackage{graphicx}


\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth
\else\Gin@nat@width\fi}
\makeatother
\let\Oldincludegraphics\includegraphics
\renewcommand{\includegraphics}[1]{\Oldincludegraphics[width=\maxwidth]{#1}}
$endif$

$if(title)$
\title{$title$$if(subtitle)$: $subtitle$$endif$ $if(anonymous)$$else$$if(thanks)$\thanks{$thanks$} $endif$$endif$ }
$endif$ 



$if(anonymous)$$else$\author{$for(author)$\Large $author.name$\vspace{0.05in} \newline\noparindent\normalsize{$author.affiliation$}  $sep$ \and $endfor$}$endif$


\date{$date$}

\usepackage{titlesec}

\titleformat*{\section}{\normalsize\bfseries}
\titleformat*{\subsection}{\normalsize\itshape}
\titleformat*{\subsubsection}{\normalsize\itshape}
\titleformat*{\paragraph}{\normalsize\itshape}
\titleformat*{\subparagraph}{\normalsize\itshape}


$if(natbib)$
\usepackage{natbib}
\bibliographystyle{$if(biblio-style)$$biblio-style$$else$plainnat$endif$}
$endif$

$if(biblatex)$
\usepackage$if(biblio-style)$[style=$biblio-style$]$endif${biblatex}
$if(biblatexoptions)$\ExecuteBibliographyOptions{$for(biblatexoptions)$$biblatexoptions$$sep$,$endfor$}$endif$
$for(bibliography)$
\addbibresource{$bibliography$}
$endfor$
$endif$
$if(listings)$
\usepackage{listings}
$endif$


\newtheorem{hypothesis}{Hypothesis}
\usepackage{setspace}

\makeatletter

\@ifpackageloaded{color}{
    \PassOptionsToPackage{usenames,dvipsnames}{color}
}{%
    \usepackage[usenames,dvipsnames]{color}
}
\makeatother

$for(header-includes)$
$header-includes$
$endfor$

\begin{document}

% \pagenumbering{arabic}% resets `page` counter to 1 
%$if(appendix)$
%\renewcommand*{\thepage}{$appendixletter$--\arabic{page}}
%$endif$    

$if(title)$
% \maketitle

{% \usefont{T1}{pnc}{m}{n}
\setlength{\parindent}{0pt}
\thispagestyle{plain}
{\fontsize{18}{20}\selectfont\raggedright 
\maketitle  % title \par  

}

{
   \vskip 13.5pt\relax \normalsize\fontsize{11}{12} 
$if(anonymous)$\hfill $else$$for(author)$\textbf{\authorfont $author.name$}\small\textbf{$author.affiliation$}  $sep$ \par $endfor$ $endif$
\hfill\date{$date$}
}

}


$endif$

$if(toc)$
{
\setcounter{tocdepth}{$toc-depth$}
\tableofcontents
}
$endif$

$if(abstract)$



\begin{abstract}

    \hbox{\vrule height .2pt width 39.14pc}

    \vskip 8.5pt % \small 

\noindent $abstract$

$if(keywords)$

\vskip 8.5pt \noindent \emph{Keywords}: $keywords$ \par

    \hbox{\vrule height .2pt width 39.14pc}


$else$

    \hbox{\vrule height .2pt width 39.14pc}

$endif$

\end{abstract}

$endif$

\setlength{\parindent}{0pt}
\setlength{\parskip}{1em}
\setlength{\intextsep}{10pt plus 1.0pt minus 2.0pt}
\newcommand{\blandscape}{\begin{landscape}}
\newcommand{\elandscape}{\end{landscape}}
%\vskip 6.5pt

\noindent $if(anonymous)$\doublespacing$else$$if(spacing)$\$spacing$spacing$endif$$endif$ $body$
\newpage
\singlespacing 
$if(natbib)$
$if(bibliography)$
$if(biblio-title)$
$if(book-class)$
\renewcommand\bibname{$biblio-title$}
$else$
\renewcommand\refname{$biblio-title$}
$endif$
$endif$
\bibliography{$for(bibliography)$$bibliography$$sep$,$endfor$}
$endif$
$endif$
$if(biblatex)$
\printbibliography$if(biblio-title)$[title=$biblio-title$]$endif$
$endif$
$for(include-after)$
$include-after$
$endfor$
\end{document}

1 answer

  • answered 2018-02-13 02:37 romles

    I think you need to remove the space character between \\newpage\n and # or ##: markdown headers must start at the beginning of the line.

    Examples:
    Instead of "\\newpage\n # start page for group i"
    use "\\newpage\n# start page for group i"

    Same thing for "\\newpage\n ## next page for group i" : use "\\newpage\n## next page for group i"