3

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}
3
  • Is the listings java language setting spelled with a capital J?
    – daleif
    Commented Dec 12, 2013 at 19:50
  • the first error I get from fancyvrb with the above, if \FancyVerbGetLine does not find a ^^M, then we are at the end of the file,and \FV@EOF attempts to terminate the document. Otherwise, \FV@EOF is gobbled by \FancyVerbGetLine, according to the manual.
    – doed
    Commented Dec 12, 2013 at 22:08
  • Thanks Andrew! dalief, Java is spelled with a capital J. The answer provided by Herbert and Jubobs of using /lstset{style=Java} before the \begin{lstlistings} works. I did not the first solution provided by Herbert, which is to define a new environment yet. But I will try it and post if it works. Commented Dec 12, 2013 at 22:31

2 Answers 2

1

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

2

Have a look at subsection 4.15 Interface to fancyvrb in the listings documentation. You're supposed to set all the listings option with \lstset before the beginning of the verbatim environment used, not in an optional argument of the latter.

A tip: you should perhaps use a different style name than Java, to avoid confusion with the predefined language of the same name.

enter image description here

\documentclass{beamer}

\usetheme{bars}

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

\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}
  \lstset{style=Java}
  \begin{Verbatim}
    new Date(); 
  \end{Verbatim}
\end{frame}

\end{document}
5
  • I'm wondering a bit here, what is the advantage to use fancyverb and listings inside fancyverb?
    – daleif
    Commented Dec 12, 2013 at 20:00
  • According to the subsection I quote from the doc, fancyvrb provides macros for reading, writing and typesetting verbatim code. It has some remarkable features the listings package doesn’t have. I haven't looked into it further.
    – jub0bs
    Commented Dec 12, 2013 at 20:04
  • I'd like to know what those features were...
    – daleif
    Commented Dec 12, 2013 at 20:13
  • Daleif, I believe you can use latex commands inside Verbatim environment and not inside lstlistings environment. I did not have much success with them though. Commented Dec 12, 2013 at 22:39
  • If you define escape sequences, then you can escape just fine I listings. I typically use a pair if X... X for that
    – daleif
    Commented Dec 13, 2013 at 7:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .