列表包自訂語言分隔符號匹配左側

列表包自訂語言分隔符號匹配左側

我正在嘗試定義一種新的類似 cli 的語言介面,但我無法弄清楚如何使用分隔符號來匹配我的提示。

我想對文字套用樹不同的樣式。在下面的範例中,我用~,-或給每個字元加了下劃線,=以表明它們應該屬於哪個樣式組。

# configure terminal
~ ==================
(config)# json notification host a
--------~ ========================
(something-something)# url http://10.99.10.1:8000/path
---------------------~ ===============================

我一直在嘗試這樣做:

\documentclass{article}
\usepackage{listings}
\lstdefinelanguage{iCli}
{
  comment=[l]{\%},
  basicstyle=\small,
  keywords={\#},
  keywordstyle=\textbf,
  delim=[s][keywordstyle]{(}{\#}
}

\begin{document}
\begin{lstlisting}[language=iCli]
% This is a comment
# configure terminal
(config)# json notification host a
\end{lstlisting}
\end{document}

但它沒有#作為關鍵字突出顯示

答案1

需要一點黑客技術;見下文。

在此輸入影像描述

\documentclass{article}

% the following two packages are for bold typewriter font
\usepackage[T1]{fontenc}
\usepackage{beramono}

\usepackage{listings}
\usepackage{xcolor}

\lstdefinelanguage{iCli}{
  comment    = [l]{\%},
  literate   = \#{{\processhash}}1,              %<-- required for bold prompt
  moredelim  = **[is][\processmydelims]{(}{)\#}, %<-- for highlighting (...)#
  basicstyle = \ttfamily,                        %<-- for a nice typewriter font
}

% helper macros
\newcommand\processmydelimsend{}
\newcommand\processmydelims{%
  \renewcommand\processmydelimsend{\textcolor{red}{)}\textbf{\#}\egroup}%
  \bgroup\color{red}(\aftergroup\processmydelimsend%
}
\makeatletter
\newcommand\processhash{%
  \ifnum\lst@mode=\lst@Pmode%
    \bfseries%
  \fi
  \#%
}
\makeatother

\begin{document}

\begin{lstlisting}[language=iCli]
# configure terminal
(config)# json notification host a
(something-something)# url http://10.99.10.1:8000/path
\end{lstlisting}

\end{document}

相關內容