Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I would like to use minted to redefine the Sweave environments Sinput and Soutput (see the example below). This way, I can have minted format my R code. There are two problems left:

1) Is it possible to pass other commands (such as \par or \vspace) to Sinput and Soutput via minted? As you can see, I tried it for Soutput but it failed. It would be great to be able to pass commands/arguments, since that would allow one to adjust the spacing between the Sinput and Soutput environments. Note: this works with listings's lstnewenvironment.

2) Is it possible to use different minted highlighting styles (e.g., emacs and bw) for Sinput and Soutput defined below? This way, the input and output could be colorized differently which would be very nice. Note: this works with listings's \lstset command which can be used to adjust style-related settings.

\documentclass{scrartcl}

\usepackage[T1]{fontenc}
\usepackage[american]{babel}
\usepackage{xcolor}
\usepackage{fancyvrb}
\usepackage{Sweave}
\usepackage{minted}

\xdefinecolor{lightgray}{RGB}{247, 247, 247}
\xdefinecolor{semilightgray}{RGB}{235, 235, 235}

\renewenvironment{Schunk}{\par\vspace{0.3\baselineskip}}{\\[-0.5\baselineskip]}

\expandafter\let\csname Sinput\endcsname\relax
\expandafter\let\csname endSinput\endcsname\relax
\expandafter\let\csname Soutput\endcsname\relax
\expandafter\let\csname endSoutput\endcsname\relax
\expandafter\let\csname Sinput*\endcsname\relax
\expandafter\let\csname endSinput*\endcsname\relax
\expandafter\let\csname Soutput*\endcsname\relax
\expandafter\let\csname endSoutput*\endcsname\relax

\usemintedstyle{emacs}

% redefine Sweave's Sinput
\newminted[Sinput]{r}{%
  linenos,% line numbers
  bgcolor=semilightgray% background color
}
% redefine Sweave's Soutput
\newminted[Soutput]{r}{%
  bgcolor=lightgray% background color
%  \protect\vspace{\baselineskip}
}

\begin{document}
<<label=preliminaries, echo=FALSE, results=hide>>=
options(width=74, useFancyQuotes="UTF-8", prompt="> ", continue="  ")
options(SweaveHooks=list(fig=function() par(mar=c(4, 4, 0.4, 0.7))))
@
Just before the Schunk, to see the spacing.
<<foo>>=
(x <- rnorm(20))
3!=4
@
Just after the Schunk, to see the spacing.
\end{document}
share|improve this question
add comment

2 Answers 2

up vote 1 down vote accepted

Here's a solution using my PythonTeX package, which uses the same Pygments highlighting library that minted does. I don't use Sweave, so this is just an example of how to get the Sinput and Soutput to do what you want; it's not a real Sweave document.

I've set Sinput to use the rconsole lexer, with emacs style, and Soutput to use the text lexer, with bw style. All of this is easy to change. You should be able to get whatever spacing you want by changing the Schunk, Sinput, and Soutput environments, either by hard-coding spaces, or by adding optional arguments that will produce spaces.

enter image description here

\documentclass{scrartcl}

\usepackage{xcolor}
\usepackage{pythontex}
\usepackage{mdframed}

\xdefinecolor{lightgray}{RGB}{247, 247, 247}
\xdefinecolor{semilightgray}{RGB}{235, 235, 235}

\newenvironment{Schunk}{\vspace{10pt}}{\vspace{10pt}}

\newenvironment{Sinput}{%
    \VerbatimEnvironment
    \begin{mdframed}[backgroundcolor=semilightgray]%
    \begin{pygments}{rconsole}}{\end{pygments}\end{mdframed}\vspace{10pt}}
\setpygmentspygopt{rconsole}{style=emacs}
\newenvironment{Soutput}{%
    \VerbatimEnvironment
    \begin{mdframed}[backgroundcolor=lightgray]%
    \begin{pygments}{text}}{\end{pygments}\end{mdframed}}
\setpygmentspygopt{text}{style=bw}


\begin{document}
Just before the Schunk, to see the spacing.  
\begin{Schunk}
\begin{Sinput}
> 2 + 2
\end{Sinput}

\begin{Soutput}
[1] 4
\end{Soutput}
\end{Schunk}
Just after the Schunk, to see the spacing.
\end{document}
share|improve this answer
    
Thanks. On the one hand, that solves the problem. On the other hand, one has to add line numbers etc. by hand. More importantly, PythonTeX is not included in TeXLive (as far as I know), so will not be available to users by default, which is a disadvantage if you distribute the code. –  Marius Hofert Nov 9 '12 at 6:29
    
The Pygments commands take fancyvrb optional arguments, so getting line numbers is just a matter of using \begin{pygments}[numbers=left,firstnumber=last]{rconsole}, etc. Regarding TeX Live, right now PythonTeX is only on GitHub since it's been in beta, but I will add it to CTAN as soon as that's over. That should be by the end of December. –  G. Poore Nov 9 '12 at 12:44
    
Thanks a lot. Great to hear that PythonTeX will be on CTAN then. Keep up the good work. –  Marius Hofert Nov 11 '12 at 8:39
    
Hmmm... I just realized that this is similar to Sweave in that it complicates matters quite a bit (concerning compilation, compatibility etc.). Then knitr would also be an option. –  Marius Hofert Nov 13 '12 at 7:40
    
@MariusHofert I just got a few minutes to look at your problem, and a quick answer by knitr is provided below. –  Yihui Jan 9 '13 at 4:26
show 1 more comment

As you mentioned in the comments, this problem can be easily solved by knitr, and you do not have to use Sweave.sty -- feel free to redefine Schunk, Sinput and Soutput.

\documentclass{scrartcl}
\usepackage[T1]{fontenc}
\usepackage[american]{babel}
\usepackage{xcolor}
\usepackage{minted}

\xdefinecolor{lightgray}{RGB}{247, 247, 247}
\xdefinecolor{semilightgray}{RGB}{235, 235, 235}

\usemintedstyle{emacs}

\newenvironment{Schunk}{\par\vspace{0.3\baselineskip}}{\\[-0.5\baselineskip]}
% define Sinput
\newminted[Sinput]{r}{%
  linenos,% line numbers
  bgcolor=semilightgray% background color
}
% define Soutput
\newminted[Soutput]{r}{%
  bgcolor=lightgray% background color
%  \protect\vspace{\baselineskip}
}

\begin{document}
<<label=preliminaries, echo=FALSE, results='hide'>>=
options(width=74, useFancyQuotes="UTF-8")
knit_hooks$set(fig.keep=function(before, options, envir) {
  if (before) par(mar=c(4, 4, 0.4, 0.7))
})
render_sweave()
set_header(highlight = '')
@
Just before the Schunk, to see the spacing.
<<foo>>=
(x <- rnorm(20))
3!=4
@
Just after the Schunk, to see the spacing.
\end{document}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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