2

I am trying to find out how to define a style (or at least just a colour) on columns of an SQL query in the same way of the keywords style in the listings package. An example of the code can be found under this link https://github.com/davidstutz/latex-resources/blob/master/listings-sql/sql.tex

The current output of the listings package is:

enter image description here

I want to colour the columns ("firstname" in the example above) in the query automatically, just like any other SQL editors. Here is an example from DataGrip:

enter image description here

  • 1
    Welcome to TeX.SE! Currently it is not really clear what your question is about. In what situation do you want to show your SQL queries? What do your queries look like? What should be colored, are the column names fixed or can it be anything? Do you want to use the listings package for this or not? It would help to understand your question better if you add code for a small example document to your question that shows your input, and a description or an image with a sketch of how you want your output to look. – Marijn Apr 11 '20 at 08:39
  • Hi @Marijn, I will add screenshots for more clarification! – Ziad Salem Apr 11 '20 at 08:50
  • Thanks for the screenshot, it is a bit more clear now. So is it correct that you want to color the column names but not table names (such as person)? Or do you want to color everything that is not a keyword? Also, could you show a screenshot of an editor that does this? I have tried a couple of different highlighters in different editors and they all color only keywords and leave column and table names black. – Marijn Apr 11 '20 at 10:36
  • 1
    I would like to colour only the columns. I have just added a screenshot from the DataGrip IDE. – Ziad Salem Apr 11 '20 at 11:52
  • @Marijn I do use actually Listings and I have already attached a link in my question to a GitHub project as a code example of what I do use. The code of the Listings package that I do use in my latex file is almost the same as it is in the link. I did just some more formatting stuff. – Ziad Salem Apr 11 '20 at 12:00

2 Answers2

2

The package piton directly differentiates the names of the tables and the names of fields. However, piton requires LuaLaTeX.

\documentclass{report}
\usepackage{xcolor}
\usepackage{piton}

\begin{document}

\begin{Piton}[language=SQL] select firstname from person where firstnamew is null; \end{Piton}

\end{document}

Output from above code

F. Pantigny
  • 40,250
1

The following approach is based on pattern matching in the SQL query. If one of the predefined language keywords is encountered then a macro is called to examine this keyword. If the keyword is select or where then all following identifiers are printed in purple. If the keyword is from then the following identifiers will be black.

This is done by setting a boolean switch in the macro for the keyword style, and checking this switch in the macro for the identifier style. The keyword style macro also has \color{blue} at the end to set the color for the keywords.

Note that such a simple pattern matching approach may fail for more complex SQL queries. Also it is case sensitive, so SELECT firstname will not work.

MWE

\documentclass[a4paper,12pt]{article}

\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{xstring}

\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{ltgray}{rgb}{0.5,0.5,0.5}

\makeatletter
\newif\ifcolname
\colnamefalse

\def\keywordcheck{%
\IfStrEq*{\the\lst@token}{select}{\global\colnametrue}{}%
\IfStrEq*{\the\lst@token}{where}{\global\colnametrue}{}%
\IfStrEq*{\the\lst@token}{from}{\global\colnamefalse}{}%
\color{blue}%
}
\def\setidcolor{%
\ifcolname\color{purple}\else\color{black}\fi%
}
\makeatother

\lstset{%
    backgroundcolor=\color{white},
    basicstyle=\footnotesize,
    breakatwhitespace=false,
    breaklines=true,
    captionpos=b,
    commentstyle=\color{dkgreen},
    deletekeywords={...},
    escapeinside={\%*}{*)},
    extendedchars=true,
    frame=single,
    keepspaces=true,
    language=SQL,
    otherkeywords={is},
    morekeywords={*,modify,MODIFY,...},
    keywordstyle=\keywordcheck,
    identifierstyle=\setidcolor,
    numbers=left,
    numbersep=15pt,
    numberstyle=\tiny,
    rulecolor=\color{ltgray},
    showspaces=false,
    showstringspaces=false, 
    showtabs=false,
    stepnumber=1,
    tabsize=4,
    title=\lstname
}

\begin{document}

\begin{lstlisting}[language=sql]
select firstname, char_length(firstname), lastname from person
where firstnamew is null and lastname LIKE '%son';
\end{lstlisting}

\end{document}

Result:

enter image description here

Marijn
  • 37,699
  • Thank you @Marjin.

    I added the "keywordcheck" code on my own code it did not work. I copied your code example on a new tex file it did work. I just need now to figure out how to integrate your pattern matching code into my listings file. P.S. I have my latex not in one file. I have one file to the listings definition and another file for the main latex file, where I just import the listings file into my main latex file.

    – Ziad Salem Apr 11 '20 at 14:00
  • I have also another question. What does the "%" symbol do in these lines (\def\keywordcheck{%) till (\lstset{%)? – Ziad Salem Apr 11 '20 at 14:01
  • Without the % symbol extra spaces may be inserted in the output, see https://tex.stackexchange.com/questions/7453/what-is-the-use-of-percent-signs-at-the-end-of-lines. It is not always strictly necessary, but it is a good practise that each line of a macro definition ends with a %. – Marijn Apr 11 '20 at 16:18
  • To integrate the code: it should be sufficient to add the two definitions (for \keywordcheck and for \setidcolor, and also to set the two options keywordstyle=\keywordcheck and identifierstyle=\setidcolor in \lstset. Note also that I used the color purple that is not defined for the color package, therefore I replaced \usepackage{color} in the original code with \usepackage{xcolor}. – Marijn Apr 11 '20 at 16:21
  • It did work. Thanks for your help :)! – Ziad Salem Apr 12 '20 at 06:05