在 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環境。您可以使用listings'鍵輕鬆定義在程式碼中觸發粗體的分隔符moredelim,如下所示:

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}

相關內容