[Tex/LaTex] Integrating listings with fancyvrb

beamerfancyvrblistings

I am trying to use fancyvrb with listings and I get an error saying

l.2 …{Verbatim}[formatcom={\lstset{style=Java}}]

Package Listings Error: language java undefined.

\documentclass{beamer}
\usetheme{bars}

\usepackage{xcolor}
\usepackage[T1]{fontenc}
\usepackage{hyperref}
\usepackage{listings}
\usepackage{fancyvrb}
\usepackage{graphicx}
\usepackage{mathtools}

\lstdefinestyle{Java}{ %
language=Java,                % choose the language of the code
basicstyle=\scriptsize\ttfamily, % the size of the fonts that are used
numbers=left,                   % where to put the line-numbers
numberstyle=\tiny,      % the size of the fonts that are used for
stepnumber=1,                   % the step between two line-numbers. 
numbersep=5pt,                  % how far the line-numbers are from the code
showspaces=false,               % show spaces adding particular underscores
showstringspaces=false,         % underline spaces within strings
showtabs=false,                 % show tabs within strings adding particular
frame=single,           % adds a frame around the code
tabsize=2,          % sets default tabsize to 2 spaces
captionpos=b,           % sets the caption-position to bottom
breaklines=true,        % sets automatic line breaking
breakatwhitespace=false,   
%escapeinside={\%*}{*)},     % if you want to add a comment within your code
fancyvrb=true,
}




\begin{document}

\begin{frame}[fragile]
  \frametitle{Listings with Fancyvrb}
  \begin{Verbatim}[formatcom={\lstset{style=Java}}]
    new Date(); 
  \end{Verbatim}
\end{frame}

\end{document}

Best Answer

define your own environment or use explicitely \lstset:

\documentclass{beamer}
\usetheme{bars}   
\usepackage{xcolor}
\usepackage[T1]{fontenc}
\usepackage[scaled=0.85]{beramono}
\usepackage{hyperref}
\usepackage{fancyvrb}
\usepackage{listings}
\usepackage{graphicx}
\usepackage{mathtools}

\lstdefinestyle{Java}{%
language=Java,                % choose the language of the code
basicstyle=\scriptsize\ttfamily, % the size of the fonts that are used
keywordstyle=\bfseries,
numbers=left,                   % where to put the line-numbers
numberstyle=\tiny,      % the size of the fonts that are used for
stepnumber=1,                   % the step between two line-numbers. 
numbersep=5pt,                  % how far the line-numbers are from the code
showspaces=false,               % show spaces adding particular underscores
showstringspaces=false,         % underline spaces within strings
showtabs=false,                 % show tabs within strings adding particular
%frame=single,           % adds a frame around the code
tabsize=2,          % sets default tabsize to 2 spaces
captionpos=b,           % sets the caption-position to bottom
breaklines=true,        % sets automatic line breaking
breakatwhitespace=false,   
%escapeinside={\%*}{*)},     % if you want to add a comment within your code
fancyvrb=true,
}

\newenvironment{JavaCode}
  { \VerbatimEnvironment%
    \lstset{style=Java}
    \begin{Verbatim} }
  { \end{Verbatim}  }  

\begin{document}

\begin{JavaCode}
import java.util.Vector;

abstract class Figur{
  abstract double getFlaeche();
}

class Rechteck extends Figur{
...
\end{JavaCode}



\lstset{style=Java}
\begin{Verbatim}
import java.util.Vector;

abstract class Figur{
  abstract double getFlaeche();
}

class Rechteck extends Figur{
...
\end{Verbatim}

\end{document}

enter image description here

Related Question