목록과 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. 코드가 최소가 아닙니다( 여기서는 , 등이 필요하지 않음) hyperref.mathtools

게다가 왜 단순히 환경을 사용하지 않는지 잘 모르겠습니다 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}

관련 정보