Marcus Nunes
Marcus Nunes

Reputation: 861

Adjust the distance between code and figure output in knitr

I'm using knitr to write some slides about R graphics. I have the following code in my .rnw file:

\begin{frame}[fragile]

\frametitle{A Região Gráfica}

<<RegiaoGrafica01>>=
plot(0:10, 0:10)
@

\end{frame}

The output I have is

slide 02

However, I think the plot is too distant from my code. I'd like to put the plot higher on the slide, without using something like

\vspace{-10mm}

between two chunks of code (one of them with eval=FALSE and the other with echo=FALSE). I searched knitr documentation, but I couldn't find an option that helps me here.

My minimal reproducible example is below:

\documentclass[10pt, compress, usetitleprogressbar]{beamer}

\usetheme{m}

\usepackage{booktabs}
\usepackage[scale=2]{ccicons}
\usepackage{minted}
\usepackage{amssymb}
\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage[portuges]{babel}
\usepackage{bm}
\usepackage{icomma}
\usepackage[retainorgcmds]{IEEEtrantools}
\usepackage{mathtools}
\usepackage{multirow}
\usepackage{natbib}
\usepackage{shadow}
\usepackage{subfigure}
\usepackage{tikz}
\usetikzlibrary{shapes.arrows}
\usepackage{verbatim}
\usepackage{xcolor}

\usepgfplotslibrary{dateplot}

\usemintedstyle{trac}

%%% Identification

\title{O Subsistema Gráfico}
\subtitle{EST0091 - Introdução ao R}
\date{26 de março de 2015}
\author{Marcus Nunes}
\institute{Universidade Federal do Rio Grande do Norte}

%%% New colors

\definecolor{mDarkBrown}{HTML}{604c38}
\definecolor{mDarkTeal}{HTML}{23373b}

\definecolor{mLightBrown}{HTML}{EB811B}
\definecolor{mMediumBrown}{HTML}{C87A2F}

\definecolor{mLightRed}{HTML}{EB331B}
\definecolor{mLightGreen}{HTML}{14B03D}
\definecolor{mLightTeal}{HTML}{137D91}

\begin{document}


% new commands

\newcommand{\R}{\mathbb{R}}
\newcommand{\Xb}{\bm{X}}
\newcommand{\Yb}{\bm{Y}}

%%%%%%%%%%%%%%%%%%%%%%

% knitr options

<<setup, include=FALSE>>=
library(knitr)
opts_chunk$set(fig.path='figure/beamer-',fig.align='center',fig.show='hold',size='footnotesize')
@

<<options, cache=FALSE, echo=FALSE, eval=TRUE, tidy=TRUE, dpi=144>>=
options(width=60)
#par(cex=2)
par(mar=c(0, 0, 0, 0))
opts_knit$set(out.format = "latex")
opts_chunk$set(prompt=TRUE, tidy.opts=list(width.cutoff=40), tidy=FALSE, fig.width=6, fig.height=4.5)
knit_theme$set("print2")
@

%%%%%%%%%%%%%%%%%%%%%%


\maketitle

%%%%%%%%%%%%%
%%% SLIDE %%%

\plain{A Região Gráfica}

%%%%%%%%%%%%%


%%%%%%%%%%%%%
%%% SLIDE %%%

\begin{frame}[fragile]

\frametitle{A Região Gráfica}

<<RegiaoGrafica01>>=
plot(0:10, 0:10)
@

\end{frame}

%%%%%%%%%%%%%

\maketitle

\end{document}

Upvotes: 2

Views: 1400

Answers (1)

Marcus Nunes
Marcus Nunes

Reputation: 861

I couldn't make the figure closer to my code, but I added the lines

knit_hooks$set(small.mar = function(before, options, envir) {
    if (before) par(mar = c(14, 4, 0, 4)) 
})
opts_chunk$set(fig.path='figure/beamer-',fig.align='center',fig.show='hold',size='footnotesize', small.mar=TRUE)

according to the knitr manual. Hence, my problem was solved, not because the figure is closer to the commands, but because its margins became smaller.

Upvotes: 4

Related Questions