带有列表包的单字符关键字/运算符,无空格

带有列表包的单字符关键字/运算符,无空格

我想为 增加一种非常简单的宏语言listings。它使用单字母关键字/运算符,中间没有空格。我不知道如何创建不需要在它们之间添加某种分隔符的关键字,即我可以为 、 、 制作关键字,G它们HI着色,G H I但 不会GHI

宏语言为:

  • 09, AF是数字
  • GZ,.<>;:是关键字/运算符
  • "…"是带引号的字符串
  • 所有其他字符都被忽略(我想将它们染成灰色)

因此有效的宏可能是

G2R67S4F, X 5A. ODC>4ET"A quoted string"P

在上面的中,我想要G R S X , . > T P被着色为关键字并被 67 DC 4E着色为数字。

答案1

一种直接的方法是使用listings' 执行literate替换的能力:

在此处输入图片描述

\documentclass{article}

\usepackage{listings,xcolor}

\colorlet{numbercol}{blue}% Colour of numbers: 0-9 and A-F
\colorlet{keywordcol}{red}% Colour of keywords: G-Z and ,.<>;:
\colorlet{stringcol}{green}% Colour of strings: "..."

\lstset{
  basicstyle = \ttfamily,
  morestring=[b][\color{stringcol}]",
  showstringspaces=false,
  literate = *{0}{{\textcolor{numbercol}{\ttfamily 0}}}1 % numbers
              {1}{{\textcolor{numbercol}{\ttfamily 1}}}1
              {2}{{\textcolor{numbercol}{\ttfamily 2}}}1
              {3}{{\textcolor{numbercol}{\ttfamily 3}}}1
              {4}{{\textcolor{numbercol}{\ttfamily 4}}}1
              {5}{{\textcolor{numbercol}{\ttfamily 5}}}1
              {6}{{\textcolor{numbercol}{\ttfamily 6}}}1
              {7}{{\textcolor{numbercol}{\ttfamily 7}}}1
              {8}{{\textcolor{numbercol}{\ttfamily 8}}}1
              {9}{{\textcolor{numbercol}{\ttfamily 9}}}1
              {A}{{\textcolor{numbercol}{\ttfamily A}}}1
              {B}{{\textcolor{numbercol}{\ttfamily B}}}1
              {C}{{\textcolor{numbercol}{\ttfamily C}}}1
              {D}{{\textcolor{numbercol}{\ttfamily D}}}1
              {E}{{\textcolor{numbercol}{\ttfamily E}}}1
              {F}{{\textcolor{numbercol}{\ttfamily F}}}1
              {G}{{\textcolor{keywordcol}{\ttfamily G}}}1 % keywords
              {H}{{\textcolor{keywordcol}{\ttfamily H}}}1
              {I}{{\textcolor{keywordcol}{\ttfamily I}}}1
              {J}{{\textcolor{keywordcol}{\ttfamily J}}}1
              {K}{{\textcolor{keywordcol}{\ttfamily K}}}1
              {L}{{\textcolor{keywordcol}{\ttfamily L}}}1
              {M}{{\textcolor{keywordcol}{\ttfamily M}}}1
              {N}{{\textcolor{keywordcol}{\ttfamily N}}}1
              {O}{{\textcolor{keywordcol}{\ttfamily O}}}1
              {P}{{\textcolor{keywordcol}{\ttfamily P}}}1
              {Q}{{\textcolor{keywordcol}{\ttfamily Q}}}1
              {R}{{\textcolor{keywordcol}{\ttfamily R}}}1
              {S}{{\textcolor{keywordcol}{\ttfamily S}}}1
              {T}{{\textcolor{keywordcol}{\ttfamily T}}}1
              {U}{{\textcolor{keywordcol}{\ttfamily U}}}1
              {V}{{\textcolor{keywordcol}{\ttfamily V}}}1
              {W}{{\textcolor{keywordcol}{\ttfamily W}}}1
              {X}{{\textcolor{keywordcol}{\ttfamily X}}}1
              {Y}{{\textcolor{keywordcol}{\ttfamily Y}}}1
              {Z}{{\textcolor{keywordcol}{\ttfamily Z}}}1
              {,}{{\textcolor{keywordcol}{\ttfamily ,}}}1
              {.}{{\textcolor{keywordcol}{\ttfamily .}}}1
              {<}{{\textcolor{keywordcol}{\ttfamily <}}}1
              {>}{{\textcolor{keywordcol}{\ttfamily >}}}1
              {;}{{\textcolor{keywordcol}{\ttfamily ;}}}1
              {:}{{\textcolor{keywordcol}{\ttfamily :}}}1
}

\begin{document}

\begin{lstlisting}
G2R67S4F, X 5A. ODC>4ET"A quoted string"P
\end{lstlisting}

\end{document}

literate=[*]<replacement item> ... <replacement item>工作方式为,每个都<replacement item>采用 的形式{<search>}{<replace>}{<width>},其中<search>被替换为<replace>并占据<width>字符的宽度。可选的s 仅*适用<replacement item>于非字符串条目。

您可以调整颜色以满足您的需要。

答案2

谢谢,效果非常好!

我把它放在语言定义中:

\lstdefinelanguage{BL233} {   
   basicstyle = \ttfamily,
   morestring=[b][\color{stringcol}]",
   showstringspaces=false,
   literate = *{0}{{\textcolor{numbercol}{\ttfamily 0}}}1 % numbers
 }

还让我定义第二种语言扩展来解释单字母宏命令:例如,将 S 扩展为 Start

{S}{{\textcolor{keywordcol}{\ttfamily S}}{\textcolor{meaningcol}{\ttfamily tart}}}5

相关内容