Skip to content

Instantly share code, notes, and snippets.

@ramnathv
Created February 26, 2012 03:39
Show Gist options
  • Save ramnathv/1912800 to your computer and use it in GitHub Desktop.
Save ramnathv/1912800 to your computer and use it in GitHub Desktop.
Collection of knitr Hooks
% Full Screen Environment for Beamer Slides
\newenvironment{changemargin}[2]{%
\begin{list}{}{%
\setlength{\topsep}{0pt}%
\setlength{\leftmargin}{#1}%
\setlength{\rightmargin}{#2}%
\setlength{\listparindent}{\parindent}%
\setlength{\itemindent}{\parindent}%
\setlength{\parsep}{\parskip}%
}%
\item[]}{\end{list}}
# Wraps chunk in a fragile frame environment for beamer
fragile = function(before, options, envir){
if (before) {
return('\\begin{frame}[fragile]\n')
} else {
return('\\end{frame}')
}
}
# Wraps figures inside a full figure environment for beamer
fullfig = function(before, options, envir) {
if (before) {
return('\\begin{changemargin}{-1cm}{-1cm}\n')
} else {
return('\n\\end{changemargin}')
}
}
# Higher Order Functions for Latex Hooks
wrap_in_env <- function(begin, end = begin){
function(x, options){
str_c("\n\\begin{", begin, "}\n", x, "\n\\end{", end, "}")
}
}
# Example Usage
frame_hook <- wrap_in_env('frame')
@ramnathv
Copy link
Author

So instead of print(x), I could just do return(x) ? Is there any advantage to doing it this way?

@yihui
Copy link

yihui commented Mar 12, 2012

yes you can return(x); in this case, print(x) is equivalent to return(print(x)) and the function both prints x and returns x, because print(x) returns x

in short, print(x) brings the side effect of printing, which is not necessary here

@ramnathv
Copy link
Author

Got it. Makes sense. I have updated the gist based on your comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment