I am not able to both
- Change the color of comments marked by --, AND
- DO NOT replace hyphens (-) by the minus (−) sign.
A MRE follows.
\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}
Rationale
With the space restrictions of today's publications, in particular 2-column ACM or IEEE conference proceedings, we simply cannot afford to use \ttfamily for showing SQL queries, only the starting SELECT consumes 1/4 of the column width. For this reason we would need to print SQL queries in a more compact font. Using \sffamily brings many problems that are not in the \ttfamily, in particular the replacement of hyphens with minus signs.
In the following figures the problem can be seen. The first figure is showing when commenting out literate={-}{-}1
The second figure shows when the literate command above is activated.
As another problem, when using the \sffamily and I copy/paste the text from the resulting PDF file, the underline characters are missing, which is problematic since students must be able to reproduce the SQL queries in their terminal.
This is the result of a copy/paste
SELECT ST AsText(ST Intersection(geometry ’Linestring(1 1,3 3)’, geometry ’Linestring(1 3,3 1)’));
In summary, we would need to write SQL queries with exactly the same behavior as in the \ttfamily but in a more compact font.
Towards an 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.
Any idea to solve this ?






