[Tex/LaTex] Require vertical centering for Header 1 in R Markdown : \vspace , \bigskip and \newline are ignored

horizontal alignmentknitrsectioningvertical alignment

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.

I would be grateful for any tip that will help me make header1 vertically centered on a single page, either 1) individually with spaces or \begin{newcommand} + \end{newcommand} or 2) defining in the preamble that any Header1 defined by one # gets vertically centered.

Thanks a lot!

Best Answer

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
header-includes:
  - \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 src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

<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}

enter image description here

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)

Related Question