From ce30e1ac617c570564b72a6ab73b143061886b1a Mon Sep 17 00:00:00 2001 From: Benjamin Mako Hill Date: Sat, 4 Aug 2018 10:44:15 -0700 Subject: [PATCH] hand-merged knitr material - branch is now knitrified - added material to the gitignore for tex files --- paper_template/.gitignore | 2 + paper_template/Makefile | 12 ++--- paper_template/knitr_rdata/knitr_data.RData | Bin 0 -> 239 bytes paper_template/resources/preamble.R | 50 ++++++++++++++++++++ paper_template/text.Rnw | 22 ++++++++- 5 files changed, 78 insertions(+), 8 deletions(-) create mode 100644 paper_template/knitr_rdata/knitr_data.RData create mode 100644 paper_template/resources/preamble.R diff --git a/paper_template/.gitignore b/paper_template/.gitignore index 5504ab4..1ae77e6 100644 --- a/paper_template/.gitignore +++ b/paper_template/.gitignore @@ -13,3 +13,5 @@ /*.ttt /*.bcf /*.run.xml +/figures/* +/*.tex diff --git a/paper_template/Makefile b/paper_template/Makefile index 0bca574..d7e6678 100644 --- a/paper_template/Makefile +++ b/paper_template/Makefile @@ -1,11 +1,11 @@ #!/usr/bin/make -all: $(patsubst %.tex,%.pdf,$(wildcard *.tex)) +all: $(patsubst %.Rnw,%.pdf,$(wildcard *.Rnw)) # use the following section for Rnw/knitr documents # all: $(patsubst %.Rnw,%.pdf,$(wildcard *.Rnw)) -# %.tex: %.Rnw -# Rscript -e "library(knitr); knit('$<')" +%.tex: %.Rnw + Rscript -e "library(knitr); knit('$<')" %.pdf: %.tex latexmk -f -pdf $< @@ -16,8 +16,8 @@ clean: rm -f vc rm -f *.bbl # the following lines are useful for Rnw/knitr - # rm -rf cache/ figure/ - # rm -f *.tex + rm -rf cache/ figure/ + rm -f *.tex viewpdf: all evince *.pdf @@ -27,4 +27,4 @@ vc: resources/vc-git pdf: all .PHONY: clean all -# .PRECIOUS: %.tex +.PRECIOUS: %.tex diff --git a/paper_template/knitr_rdata/knitr_data.RData b/paper_template/knitr_rdata/knitr_data.RData new file mode 100644 index 0000000000000000000000000000000000000000..c6f661ff18711317039b8ace2d85522cc79fb2c8 GIT binary patch literal 239 zcmV@ORGf&BvpFtC3NMB$t8QT>@S*o zA6x!@p8cI}=c$css`d||{3jfSYA-9U+P`E|xvMX=0L?+Hd5O8H#XyDt*f9`6mgK~u zBq*CLzn~-~H4j}|a!z6~OdCv)Dsmz-Zv2~ouG4*;}zvG0xn001@KV^jbD literal 0 HcmV?d00001 diff --git a/paper_template/resources/preamble.R b/paper_template/resources/preamble.R new file mode 100644 index 0000000..dbdc279 --- /dev/null +++ b/paper_template/resources/preamble.R @@ -0,0 +1,50 @@ +bold <- function(x) {paste('{\\textbf{',x,'}}', sep ='')} +gray <- function(x) {paste('{\\textcolor{gray}{',x,'}}', sep ='')} +wrapify <- function (x) {paste("{", x, "}", sep="")} + +# load("knitr_data.RData"); now broken up into small files so we'll bring 'em all in together +r <- do.call("c", lapply(paste("knitr_rdata/", list.files("knitr_rdata"), sep=""), + function (x) {load(x); return(r)})) + +attach(r) + +f <- function (x) {formatC(x, format="d", big.mark=',')} + +format.percent <- function(x) {paste(f(x*100),"\\%",sep='')} + +format.day.ordinal <- function(x) { + day <- format(x,format="%d") + daylast <- substr(day,nchar(day),nchar(day)) + dayfirst <- substr(day,1,1) + if(dayfirst == '0') + day = daylast + + if( daylast == "1") + day <- paste0(day,"st") + else if(daylast == "2") + day <- paste0(day,"nd") + else if (daylast == "3") + day <- paste0(day,"rd") + else + day <- paste0(day,"th") + + return(day) +} + +format.month <- function(x){ + return( format(x,format='%B %Y')) +} + +format.date <- function(x) { + return(paste(format(x,format = '%B'),format.day.ordinal(x),format(x,format='%Y'),sep=' ')) +} + +format.pvalue <- function (x, digits=3) { + threshold <- 1*10^(-1*digits) + x <- round(x, digits) + if (x < threshold) { + return(paste("p<", threshold, sep="")) + } else { + return(paste("p=", x, sep="")) + } +} diff --git a/paper_template/text.Rnw b/paper_template/text.Rnw index a58ce91..1f38026 100644 --- a/paper_template/text.Rnw +++ b/paper_template/text.Rnw @@ -10,6 +10,17 @@ \usepackage[utf8]{inputenc} \usepackage{wrapfig} + +<>= +knit_hooks$set(document = function(x) { + sub('\\usepackage[]{color}', +'\\usepackage[usenames,dvipsnames]{color}', x, fixed = TRUE) +}) +opts_chunk$set(fig.path="figures/knitr-") + +source("resources/preamble.R") +@ + \usepackage[T1]{fontenc} \usepackage{textcomp} \usepackage[garamond]{mathdesign} @@ -26,9 +37,7 @@ % \usepackage{endfloat} % import and customize urls -\usepackage[usenames,dvipsnames]{color} \usepackage[breaklinks]{hyperref} - \hypersetup{colorlinks=true, linkcolor=Black, citecolor=Black, filecolor=Blue, urlcolor=Blue, unicode=true} @@ -135,6 +144,15 @@ brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog \citep{989866}. +\begin{table} + \centering +<>= +kable(form.copy, format="latex") +@ +\caption{This is a test table. There are \Sexpr{f(nrow(form.copy))} rows in the table.} +\label{tab:test} +\end{table} + The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over -- 2.39.2