[Tex/LaTex] LaTeX commands in Verbatim using fancyvrb and listings

beamerfancyvrbfontslistings

I am trying to use LaTeX commands like \textbf, \textit, \textcolor in fancyvrb combined with listings. Especially I want to bold and color any text or entire blocks of Java code (not just keywords) at the same time.

So my \lstset options do not have keywordstyle set, so that I can bold, color any part of the code I want. I have the sample LaTeX code I am using below, but only the \textit command works. The \textbf command does not work. If I try to combine \textbf and \textcolor like

\textcolor!red|!\textbf!package com.test3;||

I get a whole bunch of errors. I am pasting my sample code below…

\documentclass{beamer}

\usetheme{bars}

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

\setcounter{tocdepth}{1}

\lstdefinestyle{JavaPlain}{ %
basicstyle=\scriptsize\ttfamily, % the size of the fonts 
numbers=left,                   % where to put the line-numbers
numberstyle=\tiny,      % the size of the fonts that are used for th
stepnumber=1,                   % the step between two line-numbers
numbersep=5pt,                  % how far the line-numbers are from the code
backgroundcolor=\color{white},  % choose the background color
showspaces=false,               % show spaces adding particular underscores
showstringspaces=false,         % underline spaces within strings
showtabs=false,                 % show tabs within strings adding 
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
fancyvrb=true,
fvcmdparams=textbf 1 textit 1,
}


\newenvironment{JavaCodePlain}
  { \VerbatimEnvironment%
    \lstset{style=JavaPlain}
    \begin{Verbatim} }
  { \end{Verbatim}  } 

\begin{document}

\begin{frame}[fragile]
 \frametitle{Latex commands in Verbatim}
   \fvset{commandchars=\\\!\|}
   \begin{JavaCodePlain}
   \textbf!package com.test;|  // This does not become bold
   \textit!package com.test2;| //This is italicized
   \textcolor!red|!\textbf!package com.test3;|| //Whole bunch of errors

    public class Employee {
      ...
    }
  \end{JavaCodePlain}
\end{frame}

\end{document}

Best Answer

define an optional argument for the Verbatim environment:

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

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

\setcounter{tocdepth}{1}

\lstdefinestyle{JavaPlain}{ %
basicstyle=\scriptsize\ttfamily, % the size of the fonts 
numbers=left,                   % where to put the line-numbers
numberstyle=\tiny,      % the size of the fonts that are used for th
stepnumber=1,                   % the step between two line-numbers
numbersep=5pt,                  % how far the line-numbers are from the code
backgroundcolor=\color{white},  % choose the background color
showspaces=false,               % show spaces adding particular underscores
showstringspaces=false,         % underline spaces within strings
showtabs=false,                 % show tabs within strings adding 
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
fancyvrb=true,
}

\newcommand\Red[1]{\textcolor{red}{#1}}
\newenvironment{JavaCodePlain}[1][]
  { \VerbatimEnvironment%
    \begin{Verbatim}[#1]}
  { \end{Verbatim}  } 

\begin{document}

\begin{frame}[fragile]
 \frametitle{Latex commands in Verbatim}
   \begin{JavaCodePlain}[commandchars=\\!|]
   \textbf!package com.test;|  // This does not become bold
   \textit!package com.test2;| //This is italicized
   \Red!\textbf!package com.test3;|| //Whole bunch of errors

    public class Employee {
      ...
    }
  \end{JavaCodePlain}
\end{frame}

\end{document}

However, you should use a monotype font which has a bold variant like beramono:

enter image description here