使用 fancyvrb 集成列表

使用 fancyvrb 集成列表

我正在尝试使用fancyvrblistings但收到错误消息

l.2 ...{逐字}[formatcom={\lstset{style=Java}}]

包列表错误:语言 java 未定义。

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

答案1

定义您自己的环境或明确使用\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}

在此处输入图片描述

答案2

请参阅第 4.15 节接口fancyvrblistings文档中。你应该listings使用\lstset 使用的 verbatim 环境的开始,不是在后者的可选参数中。

提示:您或许应该使用不同的样式名称Java,以避免与language同名的预定义名称混淆。

在此处输入图片描述

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

相关内容