Skip to content

Instantly share code, notes, and snippets.

@richarddmorey
Created May 26, 2016 00:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richarddmorey/32d405caebe1a07ea9e89bda081719ae to your computer and use it in GitHub Desktop.
Save richarddmorey/32d405caebe1a07ea9e89bda081719ae to your computer and use it in GitHub Desktop.
A test of importing gists inside a latex (really, Sweave) document compiled with knitr in Rstudio.
\documentclass{article}
\usepackage{fancyvrb, listings, xcolor}
\usepackage{hyperref}
\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{mauve}{rgb}{0.88, 0.69, 1.0}
\lstset{ %
language=R, % the language of the code
basicstyle=\footnotesize, % the size of the fonts that are used for the code
numbers=left, % where to put the line-numbers
numberstyle=\tiny\color{gray}, % the style that is used for the line-numbers
stepnumber=1, % the step between two line-numbers. If it's 1, each line
% will be numbered
numbersep=5pt, % how far the line-numbers are from the code
backgroundcolor=\color{white}, % choose the background color. You must add \usepackage{color}
showspaces=false, % show spaces adding particular underscores
showstringspaces=false, % underline spaces within strings
showtabs=false, % show tabs within strings adding particular underscores
frame=single, % adds a frame around the code
rulecolor=\color{black}, % if not set, the frame-color may be changed on line-breaks within not-black text (e.g. commens (green here))
tabsize=2, % sets default tabsize to 2 spaces
captionpos=b, % sets the caption-position to bottom
breaklines=true, % sets automatic line breaking
breakatwhitespace=false, % sets if automatic breaks should only happen at whitespace
title=\lstname, % show the filename of files included with \lstinputlisting;
% also try caption instead of title
keywordstyle=\color{blue}, % keyword style
commentstyle=\color{dkgreen}, % comment style
stringstyle=\color{mauve}, % string literal style
escapeinside={\%*}{*)}, % if you want to add a comment within your code
morekeywords={*,...} % if you want to add more keywords to the set
}
\begin{document}
<<set1,results='asis',echo=FALSE,message=FALSE>>=
# Settings for the particular gist
gist.url = "https://gist.github.com/richarddmorey/db65d83de20fbc06d5ad"
gist.file = "replication_CIs.R"
gist.raw = "https://gist.githubusercontent.com/richarddmorey/db65d83de20fbc06d5ad/raw/6477f286b3812d57252072a2b1b9674b4d19c46f/replication_CIs.R"
gist.sha1 = "448b88b74b779240695f7c70403e1041ae96de8a"
# Create the code listing
cat("\\begin{lstlisting}[caption = {This code for { \\lstinline[basicstyle=\\ttfamily]!",
gist.file,
"! } was imported from \\url{",gist.url,"} on ",
format(Sys.time(), "%b %d at %X %Z %Y")," (SHA1: ",gist.sha1,").}]",sep="")
cat(readLines(gist.raw),sep="\n")
cat("\\end{lstlisting}")
# Run the code (if we want)
devtools::source_gist(gist.url, sha1 = gist.sha1)
@
We can show that we ran the code in the listing because \verb|mean(CI.in)| is \Sexpr{mean(CI.in)}.
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment