我正在使用该包listings
,在 Java 环境中显示 SQL 语句时遇到问题。这是 MWE。
\documentclass[]{report}
\usepackage{listings}
\definecolor{listinggray}{gray}{0.9}
\definecolor{lbcolor}{rgb}{0.9,0.9,0.9}
\lstset{
backgroundcolor=\color{lbcolor},
tabsize=4,
rulecolor=,
language=matlab,
basicstyle=\scriptsize,
upquote=true,
aboveskip={1.5\baselineskip},
columns=fixed,
showstringspaces=false,
extendedchars=true,
breaklines=true,
prebreak = \raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}},
frame=single,
showtabs=false,
showspaces=false,
showstringspaces=false,
identifierstyle=\ttfamily,
keywordstyle=\color[rgb]{0,0,1},
commentstyle=\color[rgb]{0.133,0.545,0.133},
stringstyle=\color[rgb]{0.627,0.126,0.941},
}
\begin{document}
\begin{lstlisting}[language=java]
Connection db = DriverManager.getConnection("jdbc:h2:rel.db", "user", "pass");
Statement stmt = db.createStatement();
stmt.execupdate("INSERT INTO CAMPAIGN VALUES ('C1R1R2R3R4R5R6', 1234, 1170, 1189, 1934)");
\end{lstlisting}
\end{document}
问题出在第 4 行,因为 SQL 语句在字符串中使用了单引号。我收到 latex 错误(来自日志):
! Undefined control sequence.
\lst@um'@ ->\lst@ifupquote \textquotesingle
\else \char 39\relax \fi
l.299 ...cupdate("INSERT INTO CAMPAIGN VALUES ('C
1R1R2R3R4R5R6', 1234, 117...
The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., `\hobx'), type `I' and the correct
spelling (e.g., `I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.
如何修复这个问题?这是一个错误吗?我不明白在双引号内使用单引号有什么问题?
更新:解决方案似乎是添加\usepackage{textcomp}
前修改列表样式。
答案1
如果您将这两个包添加到序言中,您的示例就会很好地运行:
\usepackage{xcolor}
\usepackage{textcomp}
使用 TeX Live 2012 测试。