# Require vertical centering for Header 1 in R Markdown : \vspace , \bigskip and \newline are ignored

First question on tex.stackexchange after lurking a while, apologies if my form is poor, comments to improve it are welcome.

I am converting a RMarkdown report to LaTeX using knitr in RStudio with a preamble.tex file.

I have three levels of header using #, ##, ###. #Header1 is the title of each study and must appear on a separate page, vertically centered (and possibly horizontally left aligned)

LaTeX commands \vspace, \bigskip and \newline as well as spaces (two spaces, \n, and html "br") are systematically ignored by knitr and #Header1 keeps appearing on top of the page. The most that happened was that a blank page was created before the title.

I failed to successfully adapt the code suggested in the remotely compatible questions I found. Attempts with packages titling and sectsty also failed because \maketitlerequired between \begin{titlingpage} and \end{titlingpage} make the document title appear, not the #Header1 that must be included in the TOC.

Attempts with \begin{nscenter} from this question gave the error "! You can't use macro parameter character #' in vertical mode. l.206 #"

I was not able to understand this other question about \hfill.

I have a hunch that something in my preamble overrides the commands, like this one asking to make a new page after each section : \let\stdsection\section \renewcommand\section{\newpage\stdsection} but removing it simply puts the document title an the TOC on the first page. You must have understood by now that my abilities in LaTeX are limited.


Thanks a lot!

• Why converting to LaTeX if it's working in RMarkdown? – David Beauchemin Jul 24 '17 at 19:32
• I am not doing the conversion, RMarkdown is doing it (Markdown to LaTeX to PDF). I don't think the "hacks" above can be done at the Markdown level: in my book, fine-tuning happens at the LaTeX level. – Pier-Eric Chamberland Aug 7 '17 at 1:46

You can included LaTeX package directly in Rmd file and event function. Witch make the work easy and as my opinion better then sweave.

Here an example of a preampule for a .Rmd file I made.

---
title: "Gabarit PDF"
author: "David Beauchemin et Samuel Lévesque"
date: "17 mars 2017"
output:
pdf_document:
fig_caption: yes
highlight: tango
number_sections: yes
toc: yes
toc_depth: 2
fontsize: 12pt
geometry: margin=1in
lang: fr
documentclass: memoir
urlcolor: blue
- \usepackage{amsmath}  %latex pakage
- \usepackage{tcolorbox}
---


And you can even add personnal command. with the usual with latex. You just have to add them right after the preampule.

Here a script for footnote I added in slide. It's in .css. The point is you can declare everything and usually the conversion is really straight forward and easy.

[//]: déclaration des footnotes
<style>
div.footnotes {
position: absolute;
bottom: 0;
margin-bottom: 10px;
width: 80%;
font-size: 0.6em;
}
</style>

<script>
$(document).ready(function() {$('slide:not(.backdrop):not(.title-slide)').append('<div class=\"footnotes\">');

$('footnote').each(function(index) { var text =$(this).html();
var fnNum = (index+1).toString().sup();
$(this).html(text + fnNum); var footnote = fnNum + ': ' +$(this).attr('content') + '<br/>';
var oldContent = $(this).parents('slide').children('div.footnotes').html(); var newContent = oldContent + footnote;$(this).parents('slide').children('div.footnotes').html(newContent);
});
});
</script>


Finally, you can even declare direct LaTeX environment in your Rmd file.

\begin{aligned}
\hat{m}' &= X_{Z,W} \\
&= \sum_{i=1}^I \left(\frac{Z_i}{Z_{\bullet}}\right) X_{i,W}
\end{aligned}


I made a presentation about RMarkdown for my university, it's in French but the Gabarit file (template in French) contains a lot of code and maybe will help you understand more what I'm saying. (in the resources section at the bottom)

• Thanks for the documents, they will be useful. I borrowed the use of the preamble.tex file in the "Bookdown" book and other threads here. I find it more tidy than having a long list packages in the YAML. – Pier-Eric Chamberland Aug 7 '17 at 1:54
• @Pier-EricChamberland Don't use $$..$$` for display math though, see tex.stackexchange.com/questions/503/why-is-preferable-to – Torbjørn T. Aug 29 '17 at 13:06
• @TorbjørnT. I didn't know that. Thanks for the intel. I will edit my respond. – David Beauchemin Jul 24 '18 at 23:21