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}
Best Answer
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 theSinput
andSoutput
to do what you want; it's not a real Sweave document.I've set
Sinput
to use therconsole
lexer, withemacs
style, andSoutput
to use thetext
lexer, withbw
style. All of this is easy to change. You should be able to get whatever spacing you want by changing theSchunk
,Sinput
, andSoutput
environments, either by hard-coding spaces, or by adding optional arguments that will produce spaces.