我正在尝试将诸如\textbf
、、\textit
之类的 LaTeX 命令\textcolor
与fancyvrb
结合使用listings
。特别是我想同时对任何文本或整个 Java 代码块(不仅仅是关键字)进行加粗和着色。
所以我的\lstset
选项没有设置 keywordstyle,这样我就可以对代码的任何部分进行加粗、着色。我有下面使用的示例 LaTeX 代码,但只有命令\textit
有效。该\textbf
命令不起作用。如果我尝试组合\textbf
并\textcolor
像
\textcolor!red|!\textbf!package com.test3;||
我遇到了一大堆错误。我在下面粘贴了我的示例代码...
\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}
答案1
为 Verbatim 环境定义一个可选参数:
\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}
但是,您应该使用具有粗体变体的 monotype 字体,例如beramono
: