listings:突出显示单行分隔符,而不使用分隔符本身

listings:突出显示单行分隔符,而不使用分隔符本身

我想使用单行分隔符(带[l]参数),但不突出显示分隔符本身;类似

SQLite>打开 ex2.db

以下内容sqlite>应突出显示,但不应sqlite>突出显示。我只找到了针对多行分隔符的解决方案。我无法让它们适用于单行分隔符:

在此处输入图片描述

\documentclass[a4paper,12pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[svgnames]{xcolor}
\usepackage{listings}
\usepackage{newtxtt} 

\newcommand{\functionHighlightAfterSqlite}[1]{\ttfamily\bfseries\color{red}{#1}}

\lstnewenvironment{lstSqlite}
{\lstset{
    basicstyle=\ttfamily,
    keywordstyle=\bfseries,
    language=SQL,    
    moredelim=*[l][\functionHighlightAfterSqlite]{sqlite>},
    moredelim=[l][\ttfamily\bfseries\color{red}]{...>}} } % highlights delimiter
{}

\begin{document}
\begin{lstSqlite}
sqlite> .open ex2.db
sqlite> select *
   ...> from tab;
Git|1
SQLite|3
\end{lstSqlite}
\end{document}

答案1

与所述技巧相同这个答案

在此处输入图片描述

\documentclass[a4paper, 12pt]{article}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[svgnames]{xcolor}
\usepackage{listings}
\usepackage{newtxtt} 

\newcommand\styleAfterSqlitePrompt{%
    \ttfamily\bfseries\color{red}%
}

% switch to keep track of context (in a command or not)
\newif\ifcom\comfalse


\makeatletter

% This is what happens if a delimiter is encountered
\newcommand\processSqlitePrompt[1]{%
    \ifcom%                       % In this case, we're already in a command.
    \else                         % Otherwise, we just started a command;
        \global\comtrue%          % set the switch to true and
        {\lst@basicstyle #1}%     % typeset the delimiter in the basic style.
    \fi
    \styleAfterSqlitePrompt%      % In any case, apply the comment style.
}

% Reset the switch at each End-Of-Line character.
\lst@AddToHook{EOL}{\global\comfalse}

\makeatother


\lstnewenvironment{lstSqlite}{%
    \lstset{
        language     = SQL,
        basicstyle   = \ttfamily,
        keywordstyle = \bfseries,
        moredelim    = *[il][\processSqlitePrompt{sqlite>}]{sqlite>},
        moredelim    = *[il][\processSqlitePrompt{\ \ \ ...>}]{\ \ \ ...>},
    }%
}{}

\begin{document}

\begin{lstSqlite}
sqlite> .open ex2.db
sqlite> select *
   ...> from tab;
Git|1
SQLite|3
\end{lstSqlite}

\end{document}

相关内容