1

When I run the following MWE:

\documentclass{tufte-book}

\usepackage{Sweavel}

\usepackage{amsthm}
\usepackage{mathtools}
\usepackage{soul}

%%%%%%%%%%
% R example
%%%%%%%%%%
\newtheoremstyle{rex}{}{}{}{.45\textwidth}{}{}{1 mm}{}
\theoremstyle{rex}
\newtheorem{rexample}{R example}[chapter]

%%%%%%%%%%
% boxes
%%%%%%%%%%
\usepackage[framemethod=TikZ]{mdframed}
\mdfdefinestyle{style2}{
    backgroundcolor=gray!10,
}
\usepackage[strict]{changepage}
%%%%%%%%%%

%%%%%%%%%%
% crossref
%%%%%%%%%%
\usepackage[capitalize,noabbrev]{cleveref}
    \crefname{rexample}{Example}{Examples}%
%%%%%%%%%%

%%%%%%%%%%
% layout
%%%%%%%%%%
\newcommand{\blankpage}{\newpage\hbox{}\thispagestyle{empty}\newpage}
%%%%%%%%%%


%%%%%%%%%%
% listings
%%%%%%%%%%
\newcommand{\indexfunction}[1]{\index{#1@\texttt{#1}}}
\usepackage{listings}
\lstset{language = R,
  frame = single,
}
\def\Rcolor{\color{black}}
\def\Routcolor{\color{black}}
\def\Rcommentcolor{\color{red}}
\def\Rbackground{\color[rgb]{0.992, 0.965, 0.894}}
\def\Routbackground{\color[rgb]{0.894, 0.965, 0.992}}
\definecolor{lightYellow}{rgb}{0.992, 0.965, 0.894}
\sethlcolor{lightYellow}
%%%%%%%%%%

%%%%%%%%%%
% book metadata
%%%%%%%%%%
\title[]{}
\author[]{}
\publisher{}
%%%%%%%%%%

\begin{document}

\begin{Schunk}
\begin{rexample}\label{set1}\hfill{}\begin{Sinput}
setS <- letters[1:5] %>% print()
\end{Sinput}
\begin{Soutput}
[1] "a" "b" "c" "d" "e"
\end{Soutput}
\end{rexample}\end{Schunk}

\begin{Schunk}
\begin{rexample}\label{set2}\hfill{}\begin{Sinput}
"e" %in% setS
\end{Sinput}
\begin{Soutput}
[1] TRUE
\end{Soutput}
\begin{Sinput}
"f" %in% setS
\end{Sinput}
\begin{Soutput}
[1] FALSE
\end{Soutput}
\end{rexample}\end{Schunk}

\end{document}

which contains R input and output code, the following output is produced

LaTeX output compiled using knitr

Notice that the width of the input box is different from the width of the output box. How do I make those equal in width?

This code requires Sweavel.sty available here sweavel.sty is generated when one runs knir on an .Rnw file which contains LaTeX and R code

  • The original file from which this .tex was generated (as an intermediate step before generating the pdf output) was of type .Rnw, which contains both LaTeX and R code. I have edited the original query to include a link to Sweavel.sty as it was generated when I compiled my .Rnw file with knitr, a program that "knits" the LaTeX with the R code. – mk9y Apr 28 at 17:29
1

You need to redefine the definition of the style Routstyle for listings.

In your used file Sweavel.sty it is defined as

\lstdefinestyle{Routstyle}{%
  fancyvrb=false,
  literate={~}{{$\sim$}}1{R^2}{{$R^{2}$}}2{^}{{$^{\scriptstyle\wedge}$}}1{R-squared}{{$R^{2}$}}2,%
  frame=single, 
  framerule=0.2pt,
  framesep=1pt,
  basicstyle=\Routcolor\Routsize\ttfamily,%
  backgroundcolor=\Routbackground}

You can use the following code in your preamble instead:

\lstdefinestyle{Routstyle}{%
  language=R,%
  basicstyle={\Routcolor\Sweavesize\ttfamily},
  backgroundcolor=\Routbackground,%
}

So with the following complete code

\documentclass{tufte-book}

\usepackage{Sweavel}

\usepackage{amsthm}
\usepackage{mathtools}
\usepackage{soul}

%%%%%%%%%%
% R example
%%%%%%%%%%
\newtheoremstyle{rex}{}{}{}{.45\textwidth}{}{}{1 mm}{}
\theoremstyle{rex}
\newtheorem{rexample}{R example}[chapter]

%%%%%%%%%%
% boxes
%%%%%%%%%%
\usepackage[framemethod=TikZ]{mdframed}
\mdfdefinestyle{style2}{
    backgroundcolor=gray!10,
}
\usepackage[strict]{changepage}
%%%%%%%%%%

%%%%%%%%%%
% crossref
%%%%%%%%%%
\usepackage[capitalize,noabbrev]{cleveref}
    \crefname{rexample}{Example}{Examples}%
%%%%%%%%%%

%%%%%%%%%%
% layout
%%%%%%%%%%
\newcommand{\blankpage}{\newpage\hbox{}\thispagestyle{empty}\newpage}
%%%%%%%%%%


%%%%%%%%%%
% listings
%%%%%%%%%%
\newcommand{\indexfunction}[1]{\index{#1@\texttt{#1}}}
\usepackage{listings}
\lstset{language = R,
  frame = single,
}
\def\Rcolor{\color{black}}
\def\Routcolor{\color{black}}
\def\Rcommentcolor{\color{red}}
\def\Rbackground{\color[rgb]{0.992, 0.965, 0.894}}
\def\Routbackground{\color[rgb]{0.894, 0.965, 0.992}}
\definecolor{lightYellow}{rgb}{0.992, 0.965, 0.894}
\sethlcolor{lightYellow}
%%%%%%%%%%

\lstdefinestyle{Routstyle}{% <==========================================
  language=R,%
  basicstyle={\Routcolor\Sweavesize\ttfamily},
  backgroundcolor=\Routbackground,%
} % <===================================================================
%%%%%%%%%%
% book metadata
%%%%%%%%%%
\title[]{}
\author[]{}
\publisher{}
%%%%%%%%%%


\begin{document}

\begin{Schunk}
\begin{rexample}
\label{set1}\hfill{}
\begin{Sinput}
setS <- letters[1:5] %>% print()
\end{Sinput}
\begin{Soutput}
[1] "a" "b" "c" "d" "e"
\end{Soutput}
\end{rexample}
\end{Schunk}

\begin{Schunk}
\begin{rexample}
\label{set2}\hfill{}
\begin{Sinput}
"e" %in% setS
\end{Sinput}
\begin{Soutput}
[1] TRUE
\end{Soutput}
\begin{Sinput}
"f" %in% setS
\end{Sinput}
\begin{Soutput}
[1] FALSE
\end{Soutput}
\end{rexample}
\end{Schunk}

\end{document}

with the following result:

resulting pdf

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.