在 fancyvrb 与列表集成中对多行进行着色或加粗

在 fancyvrb 与列表集成中对多行进行着色或加粗

我想在带有列表的 Verbatim 环境中对多行应用乳胶命令,尤其是对代码块进行加粗或着色。

我可以对单行执行此操作,但不能对多行执行此操作。

\documentclass{beamer}

\usepackage[T1]{fontenc}
\usepackage[scaled=0.85]{beramono}%% monotype with bold variant 
\usetheme{bars}

\usepackage{xcolor}
\usepackage{hyperref}
\usepackage{listings}
\usepackage{setspace}
\usepackage{graphicx}
\usepackage{alltt}
\usepackage{mathtools}
\usepackage{fancyvrb}
\usepackage[normalem]{ulem}

\lstdefinestyle{Java}{ %
basicstyle=\scriptsize\ttfamily, % the size of the fonts that are used for the code
language=Java,                % choose the language of the code
numbers=left,                   % where to put the line-numbers
numberstyle=\tiny,      % the size of the fonts that are used for the line-numbers
stepnumber=1,                   % the step between two line-numbers. If it is 1 each line will be numbered
numbersep=5pt,                  % how far the line-numbers are from the code
backgroundcolor=\color{white},  % choose the background color. You must add \usepackage{color}
showspaces=false,               % show spaces adding particular underscores
showstringspaces=false,         % underline spaces within strings
showtabs=false,                 % show tabs within strings adding particular underscores
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,    % sets if automatic breaks should only happen at whitespace
keywordstyle=\color{red}, %keywordstyle=\color{red}\bf
commentstyle=\color{green},
variablestyle=\color{blue},
fancyvrb=true,
}


\newcommand\Red[1]{\textcolor{red}{#1}}
\newcommand\Blue[1]{\textcolor{blue}{#1}}
\newcommand\Green[1]{\textcolor{green}{#1}}

\newcommand\RB[1]{\textcolor{red}{\textbf{#1}}}
\newcommand\BB[1]{\textcolor{blue}{\textbf{#1}}}
\newcommand\GB[1]{\textcolor{green}{\textbf{#1}}}

\newenvironment{JavaCode}[1][]
  { \VerbatimEnvironment%
    \lstset{style=Java}
    \begin{Verbatim}[#1]}
  { \end{Verbatim}  }



\begin{document}

\begin{frame}[fragile]
 \frametitle{MyActionListener}
 \begin{JavaCode}[commandchars=\\!|,frame=single,numbers=left,numbersep=2pt] 

  public void perform() {
    //Doesn't work for multiple lines
    \textbf!
    class MyActionListener implements ActionListener {
      public void actionPerformed(ActionEvent event)
      {
        System.out.print("Action Performed");
      }
    }
    |

   //works for individual lines
   \textbf! ActionListener listener = new MyActionListener();|
    button.addActionListener(listener);
  }

 \end{JavaCode}
\end{frame}

\end{document}

答案1

您的代码存在几个问题:

  1. 您使用了一个在您的代码中variablestyle既未定义也未定义的键。listings
  2. 您尝试使用 定义具有逐字内容的环境\newenvironment,但这是不可能的。不过fancyvrb(参见文档中的 4.2.4.) 和listings(参见文档中的 4.16) 都具有使用逐字内容创建自定义环境的机制。
  3. 您的代码不是最少的(这里不需要hyperrefmathtools等)

此外,我不确定你为什么不简单地使用环境。你可以使用'键lstlisting轻松定义触发代码中粗体字样的分隔符,如下所示:listingsmoredelim

moredelim=**[is][\bfseries]{<openingdelimiter>}{<closingdelimiter>}

见下文。

在此处输入图片描述

\documentclass{beamer}

\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[scaled=0.85]{beramono}

\usetheme{bars}

\usepackage{xcolor}
\usepackage{fancyvrb}
\usepackage{listings}

\lstdefinestyle{Java}%
{%
    basicstyle          = \scriptsize\ttfamily,
    language            = Java,
    numbers             = left,
    numberstyle         = \tiny,
    stepnumber          = 1,
    numbersep           = 5pt,
    backgroundcolor     = \color{white},
    showspaces          = false,
    showstringspaces    = false,
    showtabs            = false,
    frame               = single,
    tabsize             = 2,
    captionpos          = b,
    breaklines          = true,
    breakatwhitespace   = false,
    morestring          = [b]",
    stringstyle         = \color{magenta},
    keywordstyle        = \color{red},
    commentstyle        = \color{green},
    identifierstyle     = \color{blue},
    moredelim           = **[is][\bfseries]{`}{`},
    fancyvrb            = true,
}

\begin{document}

\begin{frame}[fragile]
\frametitle{MyActionListener}
\begin{lstlisting}[style=Java]
public void perform() {
  //Doesn't work for multiple lines    
  `
  class MyActionListener implements ActionListener {
    public void actionPerformed(ActionEvent event)
    {
      System.out.print("Action Performed");
    }
  }
  `

  `ActionListener listener = new MyActionListener();`
  button.addActionListener(listener);
}
 \end{lstlisting}
\end{frame}

\end{document}

相关内容