我正在尝试定义一种类似于 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}