library("tidyverse")
library("textools")

Introduction

The replicable research movement has highlighted the need for researchers to produce numeric and graphical displays of quantitative information directly from code. Many tools exist to output both figures and tables into LaTeX from R.

However, papers also reference many quantitative (or qualitative) pieces of information throughot the descriptive prose. This text lies outside \figure or \table environments. Recently @eubank_embrace_2016 suggested in the Political Methodologist that each individual piece of information generated directly from code be stored in a separate .tex file.

We build on this idea to provide a suite of tools to easily manage all of the quantitative pieces of information for a research paper or presentation in LaTeX. To do so we leverage \LaTeX's macro functionality. We also provided a suite of tools inspired by htmltools [@wickham_advanced_2015] to translare R in to LaTex (a Domain Specific Language). This functionality allows researchersto create easily readable and properly escaped LaTex directly from R.

To create a set of macros for \LaTex we use R's list functionality to create these macros. The simplest way to use textools is to use list_to_macros. list_to_macros requires a list object in which the researcher gives each item in the list a name and then exports this list to .tex file by providing list_to_macros with a prefix for the macro and file to export the macros.

Export data to a text file with list_to_macros

data(mtcars)

#create the file where you will store the macro
MACROS_TEX <- "./vignette_macros.tex"

#create an empty list to store the macro
MACROS <- list()

#careful not use underscores. We recommend camelCase in this situation
MACROS$meanCyl <- round(mean(mtcars$cyl), 2)
MACROS$medianCyl <- median(mtcars$cyl)
MACROS$sdCyl <- round(sd(mtcars$cyl), 2)

#cat(list_to_macros(MACROS, prefix = "carSumStat"), file = MACROS_TEX)

The contents of the .tex file will then look like the following

#print(list_to_macros(MACROS, prefix = "carSumStat"))

The researcher then includes this set of macros in their main \LaTeX file and can call each macro from within the paper. Such a method allows researchers to easily put all of the evidence

LaTeX environment

We could then write our article using our custom macros to reference our individual pieces of information. Each macro has an easily recognizable name, following good programming practice.

#doesn't work

# with_latex(
#     "We report on summary statistics from the \\texttt{mtcars} dataset The mean number of cylinders is \\carSumStatmeanCyl, the median is \\carSumStatmedianCyl and the standard deviation is \\carSumStatsdCyl")  %>% cat %>% print


jrnold/textools documentation built on May 20, 2019, 2:06 a.m.