[Tex/LaTex] minted: How to pass LaTeX commands via \newminted and how to use different styles

mintedsweave

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 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}