GeneralNix language for lstlisting environment

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
bowsprit
Posts: 2
Joined: Thu Jun 13, 2024 7:35 am

Nix language for lstlisting environment

Post by bowsprit »

Hi,

I'm trying to define the Nix language, this is what I have so far.

Code: Select all

\documentclass{article}

\usepackage{listings}
\usepackage{xcolor}

\definecolor{stringColor}{HTML}{718a62}
\lstdefinelanguage{Nix}{
	keywords=[1]{true, false, null},
	keywords=[2]{let, in, with, rec, inherit},
	keywords=[3]{toString},
	keywordstyle=\color{blue},
	sensitive=true,
	% comments
	comment=[l]{\#},
	morecomment=[s]{/*}{*/},
	commentstyle=\color{gray},
	% strings
	morestring=[s]"",
	morestring=[s]{''}{''},
	stringstyle=\color{stringColor},
}

\begin{document}
  \begin{lstlisting}[language=Nix]
  {
    services.example = {
      enable = true;
      configFile = "${pkgs.suricata}/etc/suricata/suricata.yaml";
      #             ^^^^^^^^^^^^^^^^  <-- this should be red
    };
  }
  \end{lstlisting}
\end{document}
A string (and a multiline string using the two single quotes at the start and end) can contain an inline expression (${...}), which I'd like to mark red.

Any idea how I could implement this?

Thanks in advance!
Last edited by bowsprit on Tue Jun 18, 2024 7:20 am, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

bowsprit
Posts: 2
Joined: Thu Jun 13, 2024 7:35 am

Nix language for lstlisting environment

Post by bowsprit »

Got it working, with a little help from a friend.

Code: Select all

\documentclass{article}

\usepackage{listings}
\usepackage{xcolor}

\definecolor{stringColor}{HTML}{718a62}
\lstdefinelanguage{Nix}{
	keywords=[1]{true, false, null},
	keywords=[2]{let, in, with, rec, inherit},
	keywords=[3]{toString},
	keywordstyle=\color{blue},
	sensitive=true,
	% comments
	comment=[l]{\#},
	morecomment=[s]{/*}{*/},
	commentstyle=\color{gray},
	% strings
        morestring=*[d]{"},
        morestring=[s][\color{red}]{\$\{}{\}},
        morestring=*[d]{''},
	stringstyle=\color{stringColor},
}

\begin{document}
  \begin{lstlisting}[language=Nix]
  {
    services.example = {
      enable = true;
      configFile = "${pkgs.suricata}/etc/suricata/suricata.yaml";
      #             ^^^^^^^^^^^^^^^^  <-- this should be red
      multiline = ''
      Example ${test}
      '';
    };
  }
  \end{lstlisting}
\end{document}
User avatar
Stefan Kottwitz
Site Admin
Posts: 10319
Joined: Mon Mar 10, 2008 9:44 pm

Nix language for lstlisting environment

Post by Stefan Kottwitz »

Hi,

welcome to the forum!

That was an excellent minimal example. Nice to hear that you found a solution, and great that you posted it here! The next reader coming here by google search will be happy to find the solution.

Thanks!

Stefan
LaTeX.org admin
Post Reply