我无法同时做到
- 更改标有 --、AND 的注释的颜色
- 请勿用减号(−)代替连字符(-)。
接下来是一份 MRE。
\documentclass{report}
\usepackage{xcolor}
\usepackage{listings}
\lstset{language=SQL, columns=fullflexible, showstringspaces=false,
basicstyle=\small\sffamily, keywordstyle=\color{blue},%
% alsodigit={-},
alsoletter={-},
% literate={-}{-}1, % Do not replace hyphen by minus symbol
morecomment=[l][commentstyle]{--},%
morecomment=[s][commentstyle]{/*}{*/},%
commentstyle=\color{red},
morekeywords={REFERENCES,OVER,PARTITION,LEAD}}
\begin{document}
A SQL query.
\begin{lstlisting}
CREATE TABLE Vehicle (
VehicleId INTEGER PRIMARY KEY,
MakeId INTEGER NOT NULL,
Model CHAR(30) NOT NULL,
CreationDate DATE NOT NULL,
...
FOREIGN KEY (CustomerId) REFERENCES Makes(makeId),
.. )
\end{lstlisting}
Another SQL query with comments. I cannot make to both
(1) Change the color of comments marked by \textsf{-{-}}, and
(2) DO NOT replace hyphens (\textsf{-}) by the minus ($-$) sign.
\begin{lstlisting}
SELECT getTime(tint '[1@2001-01-01, 2@2001-01-11, 1@2001-01-15)');
-- {[2001-01-01, 2001-01-15)}
SELECT getTime(tint '{[1@2001-01-01, 2@2001-01-11],[1@2001-01-15]}');
/* {[2001-01-01 00:00:00, 2001-01-11 00:00:00],
[2001-01-15 00:00:00, 2001-01-15 00:00:00]} */
\end{lstlisting}
Another query with an underline `{\sffamily \_}' character.
\begin{lstlisting}
SELECT ST_AsText(ST_Intersection(geometry 'Linestring(1 1,3 3)',
geometry 'Linestring(1 3,3 1)'));
-- POINT(2 2)
\end{lstlisting}
\subsection*{Towards a Solution}
The last remaining problem after the suggestions here
(in particular, using the cm-super fonts) is that when copy/pasting
from the resulting PDF, the asterics are replaced by a square box.
\begin{lstlisting}[frame=single]
SELECT getTime(tint '{[1@2001-01-01, 2@2001-01-11],[1@2001-01-15]}');
/* {[2001-01-01 00:00:00, 2001-01-11 00:00:00],
[2001-01-15 00:00:00, 2001-01-15 00:00:00]} */
\end{lstlisting}
\end{document}
基本原理
由于当今出版物的空间限制,尤其是 2 列 ACM 或 IEEE 会议论文集,我们根本无法使用\ttfamily
来显示 SQL 查询,仅开头SELECT
就占用了 1/4 列宽。因此,我们需要以更紧凑的字体打印 SQL 查询。使用 会\sffamily
带来许多 中没有的问题\ttfamily
,尤其是用减号替换连字符。
下图中可以看到问题。第一张图是在注释掉时显示的literate={-}{-}1
第二张图显示了上面的读写命令何时被激活。
另一个问题是,当\sffamily
我使用并从生成的 PDF 文件中复制/粘贴文本时,下划线字符丢失了,这是有问题的,因为学生必须能够在他们的终端中重现 SQL 查询。
这是复制/粘贴的结果
选择 ST AsText(ST 交点(几何图形'Linestring(1 1,3 3)',几何图形'Linestring(1 3,3 1)'));
总之,我们需要编写与中行为完全相同\ttfamily
但字体更紧凑的 SQL 查询。
寻求解决方案
这里的建议(特别是使用 cm-super 字体)之后剩下的最后一个问题是,当从生成的 PDF 复制/粘贴时,星号会被方框取代。
有什么办法可以解决这个问题吗?
答案1
连字符的问题可以通过应用 Ulrike Fischer 的答案来解决https://tex.stackexchange.com/a/424193. 这是为了添加
\makeatletter
\lst@CCPutMacro
\lst@ProcessOther {"2D}{\lst@ttfamily{-{}}{-}}
\@empty\z@\@empty
\makeatother
也在alsoletter={-},
评论中。
下划线的问题可以通过应用 egreg 的答案来解决https://tex.stackexchange.com/a/42817。这是补充\usepackage[T1]{fontenc}
。
\documentclass{report}
\usepackage[T1]{fontenc}
\usepackage{xcolor}
\usepackage{listings}
\lstset{language=SQL, columns=fullflexible, showstringspaces=false,
basicstyle=\small\sffamily, keywordstyle=\color{blue},%
% alsodigit={-},
% alsoletter={-},
% literate={-}{-}1, % Do not replace hyphen by minus symbol
morecomment=[l][commentstyle]{--},%
morecomment=[s][commentstyle]{/*}{*/},%
commentstyle=\color{red},
morekeywords={REFERENCES,OVER,PARTITION,LEAD}}
\makeatletter
\lst@CCPutMacro
\lst@ProcessOther {"2D}{\lst@ttfamily{-{}}{-}}
\@empty\z@\@empty
\makeatother
\begin{document}
A SQL query.
\begin{lstlisting}
CREATE TABLE Vehicle (
VehicleId INTEGER PRIMARY KEY,
MakeId INTEGER NOT NULL,
Model CHAR(30) NOT NULL,
CreationDate DATE NOT NULL,
...
FOREIGN KEY (CustomerId) REFERENCES Makes(makeId),
.. )
\end{lstlisting}
Another SQL query with comments. I cannot make to both
(1) Change the color of comments marked by \textsf{-{-}}, and
(2) DO NOT replace hyphens (\textsf{-}) by the minus ($-$) sign.
\begin{lstlisting}
SELECT getTime(tint '[1@2001-01-01, 2@2001-01-11, 1@2001-01-15)');
-- {[2001-01-01, 2001-01-15)}
SELECT getTime(tint '{[1@2001-01-01, 2@2001-01-11],[1@2001-01-15]}');
/* {[2001-01-01 00:00:00, 2001-01-11 00:00:00],
[2001-01-15 00:00:00, 2001-01-15 00:00:00]} */
\end{lstlisting}
Another query with an underline `{\sffamily \_}' character.
\begin{lstlisting}
SELECT ST_AsText(ST_Intersection(geometry 'Linestring(1 1,3 3)',
geometry 'Linestring(1 3,3 1)'));
-- POINT(2 2)
\end{lstlisting}
\end{document}
答案2
广告 2
对我来说,您的请求广告 2 听起来像是一件艺术品:
- 连字符的呈现方式取决于...
- 字体
- 所以你可能想要
\ttfamily
,courier
或类似的东西
例如参见问题关于列表。
下面我演示了如何使用
basicstyle=\small\ttfamily,% <<< rendering of - depends on font
但也许(!)再次阅读手册basicstyle
。
广告 1
广告 1,颜色似乎已经到位了??
识字
对于替换,我还向您展示了一些愚蠢的字符序列替换,如果您需要它们的话:
literate={get}{GOt**}1,% <<< just for demonstration
但是,如果您坚持在特定字体中以特定的方式呈现连字符,而这又采用了不同的方式来实现一致的字体布局,那么您可能会遇到困难。
评论
正如 daleif 所说,如果您能审阅一下您的问题并在必要时提供更多的澄清和说明,我将不胜感激。
结果和代码
\documentclass{report}
\usepackage{xcolor}
\usepackage{listings}
\lstset{language=SQL, columns=fullflexible, showstringspaces=false,
% basicstyle=\small\sffamily,
basicstyle=\small\ttfamily,% <<< rendering of - depends on font
literate={get}{GOt**}1,% <<< just for demonstration
keywordstyle=\color{blue},%
% alsodigit={-},
alsoletter={-},
% literate={-}{-}1, % Do not replace hyphen by minus symbol
morecomment=[l][commentstyle]{--},%
morecomment=[s][commentstyle]{/*}{*/},%
commentstyle=\color{red},
morekeywords={REFERENCES,OVER,PARTITION,LEAD}}
\begin{document}
A SQL query.
\begin{lstlisting}
CREATE TABLE Vehicle (
VehicleId INTEGER PRIMARY KEY,
MakeId INTEGER NOT NULL,
Model CHAR(30) NOT NULL,
CreationDate DATE NOT NULL,
...
FOREIGN KEY (CustomerId) REFERENCES Makes(makeId),
.. )
\end{lstlisting}
答案3
这里有一个不同的方法,考虑到literate
替代的工作方式,它也采用正则表达式,请参阅手册:
- 它使用你的原版
basicstyle=\small\sffamily,
- 但用不同的字体替换连字符,如下所示:
literate={--}{\ttfamily{--}}1 {-}{\ttfamily{-}}1, % <<<<<
使用正则表达式,您甚至可以更精细地调整字体替换,例如,仅针对格式化的日期等。
\documentclass{report}
\usepackage{xcolor}
\usepackage{listings}
\lstset{language=SQL, columns=fullflexible, showstringspaces=false,
basicstyle=\small\sffamily,
literate={--}{\ttfamily{--}}1 {-}{\ttfamily{-}}1, % <<<<<
keywordstyle=\color{blue},%
% alsodigit={-},
alsoletter={-},
% literate={-}{-}1, % Do not replace hyphen by minus symbol
morecomment=[l][commentstyle]{--},%
morecomment=[s][commentstyle]{/*}{*/},%
commentstyle=\color{red},
morekeywords={REFERENCES,OVER,PARTITION,LEAD}}
\begin{document}
A SQL query.
\begin{lstlisting}
CREATE TABLE Vehicle (
VehicleId INTEGER PRIMARY KEY,
MakeId INTEGER NOT NULL,
Model CHAR(30) NOT NULL,
CreationDate DATE NOT NULL,
...
FOREIGN KEY (CustomerId) REFERENCES Makes(makeId),
.. )
\end{lstlisting}
Another SQL query with comments. I cannot make to both
(1) Change the color of comments marked by \textsf{-{-}}, and
(2) DO NOT replace hyphens (\textsf{-}) by the minus ($-$) sign.
\begin{lstlisting}
SELECT getTime(tint '[1@2001-01-01, 2@2001-01-11, 1@2001-01-15)');
-- {[2001-01-01, 2001-01-15)}
SELECT getTime(tint '{[1@2001-01-01, 2@2001-01-11],[1@2001-01-15]}');
/* {[2001-01-01 00:00:00, 2001-01-11 00:00:00],
[2001-01-15 00:00:00, 2001-01-15 00:00:00]} */
\end{lstlisting}
\end{document}
答案4
仅供参考。也许你应该看看piton
。使用该包,你可以执行以下操作(你的问题直接得到解决)。但是,piton
仅适用于 LuaLaTeX。
\documentclass{report}
\usepackage{xcolor}
\usepackage{piton}
\usepackage{fontspec}
\PitonOptions{language=SQL}
\SetPitonStyle[SQL]
{
Keyword = \color{blue} ,
Comment = \color{red} ,
Name.Table = \color{black} ,
Name.Field = \color{black} ,
Name.Builtin = \color{black} ,
String.Long = \color{black} ,
Number = \color{black}
}
\begin{document}
\setmonofont{lmsans10-regular.otf}
\begin{Piton}
SELECT ST_AsText(ST_Intersection(geometry 'Linestring(1 1,3 3)',
geometry 'Linestring(1 3,3 1)'));
-- POINT(2 2)
\end{Piton}
\begin{Piton}
SELECT getTime(tint '{[1@2001-01-01, 2@2001-01-11],[1@2001-01-15]}');
/* {[2001-01-01 00:00:00, 2001-01-11 00:00:00],
[2001-01-15 00:00:00, 2001-01-15 00:00:00]} */
\end{Piton}
\end{document}