Skip to content

Instantly share code, notes, and snippets.

@ramnathv ramnathv/ext-env.tex
Created Feb 26, 2012

Embed
What would you like to do?
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')
@yihui

This comment has been minimized.

Copy link

yihui commented Mar 1, 2012

this looks cool!

you do not need print() -- just let the hook return character strings (print(x) actually returns x, though...)

@ramnathv

This comment has been minimized.

Copy link
Owner Author

ramnathv commented Mar 12, 2012

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

@yihui

This comment has been minimized.

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

This comment has been minimized.

Copy link
Owner Author

ramnathv commented Mar 12, 2012

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
You can’t perform that action at this time.