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!

  • I came up with the following "hack" but an explanation of my initial problem (why was \vspaceignored) would still be appreciated. I replaced line #2 in the following: \let\stdsection\section \renewcommand\section{\newpage\stdsection} by: \renewcommand\section{\newpage\vspace*{105pt}\stdsection} \vspace was not ignored, but my table of contents was also lowered, so I removed the code altogether. \newpage \clearpage \vspace*{105pt} before #heading1. To then separate my TOC from the title page, I disabled it from the YAML and requested it separately. Jul 24 '17 at 19:15
  • Why converting to LaTeX if it's working in RMarkdown? 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. 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"
    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
div.footnotes {
  position: absolute;
  bottom: 0;
  margin-bottom: 10px;
  width: 80%;
  font-size: 0.6em;

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

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

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

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)

  • 1
    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. Aug 7 '17 at 1:54
  • 2
    @Pier-EricChamberland Don't use $$ .. $$ for display math though, see tex.stackexchange.com/questions/503/why-is-preferable-to Aug 29 '17 at 13:06
  • @TorbjørnT. I didn't know that. Thanks for the intel. I will edit my respond. Jul 24 '18 at 23:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.