Custom LaTeX file for handling multiple R Markdown files

Hello all,

I am trying to create a custom LaTeX file that can be used to create many standard documents quickly where only key parts can be exchanged and the main content for each document will be through R Markdown for specific content. (I have the basic template below that was created in word.) The .tex file that I am trying to create would make it easier to quickly change out the Logo and ownership titles and names across multiple R Markdown files using this template. The rest of the header information and document specifics would come from the specific R Markdown file. I am familiar with R Markdown but not so much with LaTeK. I have tried several different solutions but I can't get the mixture of variables (some coded in the .tex file and some in the R Markdown file) to knit together. Any help or suggestions would be appreciated!

Example Template from word

LaTeX code:

\documentclass[a4paper,12pt]{book}
\usepackage[utf8]{inputenc}
\usepackage[demo]{graphicx}
\usepackage{hyperref}
\usepackage{array}
\usepackage{lastpage}

\usepackage{fancyhdr}
\usepackage[hmargin=2cm,top=4cm,headheight=65pt,footskip=65pt]{geometry}

\setlength{\parindent}{0.95cm}

\pagestyle{fancy}

%% Header & Footer Defined

\renewcommand{\headrulewidth}{0pt}
\fancyhead[CE,CO,LE,LO,RE,RO]{} %% clear out all headers

% Header Development

\fancyhead[C]{%
\begin{tabular}{|m{3.0cm}|m{8.0cm}|m{5.0cm}|}
\hline
\includegraphics[height=1.5cm,width=2.5cm]{logo.png} &
\centering
\textbf{TITLE} &
\raggedright
\small{Number: TYP-DOC-0001 \
Revision: 01 \
Release Date: 03MAR20}\tabularnewline
\hline
\end{tabular}%
}

%% Footer Development

\fancyfoot[CE,CO,LE,LO,RE,RO]{} %% clear out all footers
\fancyfoot[C]{%
\begin{tabular}{m{0.0cm} m{16.0cm} m{0.0cm}}
&
\centering
\small{Page \thepage\ of \pageref{LastPage}} \
\tiny{Proprietary Statement} &
\tabularnewline
\end{tabular}%
}

\begin{document}
%% add this if you want the fancy style also on the first page of a chapter:
\thispagestyle{fancy}
\section{Section title}
Body of document here

\end{document}

Could you add a minimal example to show what you are trying to do and where you run into problems?

Although I write everything in LaTeX and love it, you might be better off using R Markdown and then Pandoc to process it. You can also add LaTeX code directly into Markdown if you need that.

Below is what I started with in R Markdown. I can get the header and footer to work. In this case the logo is saved in a folder as ./images/logo.png and if I substitute any image with this name in the path, I can get the header to change without having to change all of my R markdown files that use this logo. However, in the footer the "Proprietary Statement" may contain specifics that would require me to update every single document if this changed. I thought the use of a .tex template would be the solution. I haven't figured this part out or if there is a better method for doing this directly in R Markdown. The same problem exists for the "Ownership, Revision History and Effective Date" where individuals names and titles may change and would require me to update every single R Markdown when this occurs.

R Markdown Code

header-includes:

  • \usepackage[utf8]{inputenc}
  • \usepackage{graphicx}
  • \graphicspath{ {images/} }
  • \usepackage[useregional]{datetime2}
  • \usepackage{hyperref}
  • \usepackage{array}
  • \usepackage{lastpage}
  • \usepackage{fancyhdr}
  • \setlength{\parindent}{0.95cm}
  • \pagestyle{fancy}
  • \renewcommand{\headrulewidth}{0pt}
  • \fancyhead[CE,CO,LE,LO,RE,RO]{}
  • \fancyhead[C]{\begin{tabular}{|m{3.0cm}|m{9.0cm}|m{5.0cm}|}\hline\centering\includegraphics[height=1.0cm,width=2.5cm]{logo.png} & \centering\textbf{TITLE} & \raggedright\small{Number TYP-DOC-0001 \Revision 01 \Release Date \today}\tabularnewline\hline\end{tabular}}
  • \fancyfoot[CE,CO,LE,LO,RE,RO]{}
  • \fancyfoot[C]{\begin{tabular}{m{0.0cm} m{16.0cm} m{0.0cm}} & \centering\small{Page \thepage\ of \pageref{LastPage}} \ \tiny{Proprietary Statement} & \tabularnewline\end{tabular}}
    output:
    pdf_document:
    number_sections: true
    fancy: true
    documentclass: article
    lang: en-US
    geometry: "hmargin=2cm,top=4cm,headheight=65pt,footskip=65pt"
    fontsize: 12pt
    latex_engine: pdflatex

knitr::opts_chunk$set(echo = TRUE)

Purpose

Text

Scope

Text

References & Applicable Regulations

Text

Responsibilities

Text

Exemptions and Alternatives

Text

Ownership, Revision History and Effective Date

Tables

Where do you get that information from? Can you extract it from a script? What I do is create small "snippet" files of text, which are then read during the LaTeX run. I mostly use that for things like the number of observations, etc, but, in principle, it could be anything. You can then include that snippet in your template. For example, in one of my current projects, I have the following snippet in the file surveys.tex, which is generated by an R script:

The data comes from 234 Demographic and Health Surveys (DHS) from countries in East Asia, South Asia, Latin America, and Sub-Saharan Africa, collected between 1986 and 2018. The sample consists of all surveyed women aged 15 to 49 with complete information on the number of children ever born, the number of children who have died, and education. There are 1,501,771 urban women in the sample.

In the LaTeX file itself, I have the following:

% Number of surveys and regions covered
\input{../tables/surveys.tex} \unskip % https://tex.stackexchange.com/questions/18017/space-inserted-after-input-call
Table \ref{tab:surveys} shows the complete listing of 
the countries and years of the surveys.

\input{../tables/survey_table.tex}

Notice the unskip part. You might need that or you will end up with some weird line breaks.

Awesome! Thanks so much for your help.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.