我正在使用 listing 包定义 VHDL 语言。由于它不是一种流行的语言,默认设置不符合我的预期,因此我编写了以下语言定义来改进它:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\usepackage{color}
\definecolor{shellGreen}{RGB}{19,193,106}
\definecolor{backcolor}{rgb}{0.95,0.95,0.92}
\definecolor{mateBlack}{RGB}{45,45,50}
\definecolor{comment}{rgb}{0.1,0.6,0.2}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\lstdefinestyle{vhdl}{
language=vhdl,
frame=single,
basicstyle=\scriptsize,
breaklines=true,
captionpos=b,
keepspaces=true,
backgroundcolor=\color{backcolor},
keywordstyle=[1]\color{blue}\bf,
keywordstyle=[2]\color{red}\bf,
keywordstyle=[3]\color{cyan!50}\bf,
stringstyle=\color{orange},
commentstyle=\color{comment},
tabsize=2,
% number=left,
% numberstep=5pt,
showspaces=false,
showstringspaces=false,
showtabs=false,
moredelim=[s][\textcolor{green}]{component}{is},
morekeywords=[1]{
library, use ,all,entity,is,port,in,out,end,architecture,of, body,
function, variable, begin,and,or,Not,downto,ALL, signal, process, if,
else, elsif, case, when, then, range, to, component, type, with, select,
others, constant, inout, buffer, map, true, false, array, subtype, wait,
wait for, generic, =, <, >, <=, >=, =>,
},
alsoletter={=, <, >},
morekeywords=[2]{
STD_LOGIC_VECTOR,STD_LOGIC,IEEE,STD_LOGIC_1164, work, local, real,
math_real, time, NUMERIC_STD,STD_LOGIC_ARITH,STD_LOGIC_UNSIGNED,
std_logic_vector, std_logic, ieee, numeric_std, std_ulogic,
std_logic_1164, natural, bit, bit_vector, signed, unsigned,
boolean, integer
},
morekeywords=[3]{rising_edge, falling_edge, resize, to_signed, to_unsigned},
morecomment=[l]{--},
morecomment=[s][\color{orange}]{'}{'},
}
\lstset{style=vhdl}
\begin{document}
VHDL code:
\begin{lstlisting}[style=vhdl]
component Input is
port(
startButton : in std_logic; -- When 1, start game
fire : in std_logic; -- When 1, shoot a rocket
clk : in std_logic; -- 40MHz
reset : in std_logic; -- Active high
left : in std_logic; -- Left arrow button
right : in std_logic; -- Right arrow button
newMissile : out std_logic; -- If 1, new missile launched
gameStarted : out std_logic; -- When 0, show start screen
alienX : out std_logic_vector(9 downto 0); -- first alien position from left screen
alienY : out std_logic_vector(8 downto 0); -- first alien position from top screen
shipPosition : out std_logic_vector(9 downto 0) -- Ship x coordinate
);
end component;
\end{lstlisting}
\end{document}
我有两个问题。第一个问题是如何将“component”和“is”之间的所有内容都涂成绿色,但分隔符应保持为蓝色(在我的示例中,它们也是绿色)。我尝试了以下方法:(moredelim=[s][\textcolor{green}]{component}{is},
请注意,如果没有此行,“component”和“is”将显示为蓝色,正如我想要的那样)。
第二个问题是,当空间不足以显示较长的一行时,列表会开始新行。但是,如果开始新行之前显示的最后一个单词是彩色的,则框架也会被着色(在我上面的代码中,当注释被拆分时,这种情况发生了三次)。
提前感谢所有能帮助我的人。
答案1
第二个问题很容易解决,只需要给出一个明确的rulecolor
参数:
rulecolor=\color{black}
第一个问题可以通过定义一个执行块样式的新命令来解决component ... is
:
\def\component#1{%
\textbf{\textcolor{blue}{component\ }}%
\textcolor{green}{#1}%
\textbf{\textcolor{blue}{\ is}}%
}
参数是component
和之间的代码is
(不带空格)。listings
允许您使用带有一个参数的样式命令moredelim
,同时在第一个可选参数为 时丢弃分隔符本身[is]
:
moredelim=[is][\component]{component\ }{\ is},
以下是完整、更新的示例:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\usepackage{color}
\definecolor{shellGreen}{RGB}{19,193,106}
\definecolor{backcolor}{rgb}{0.95,0.95,0.92}
\definecolor{mateBlack}{RGB}{45,45,50}
\definecolor{comment}{rgb}{0.1,0.6,0.2}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\lstdefinestyle{vhdl}{
language=vhdl,
frame=single,
basicstyle=\scriptsize,
breaklines=true,
captionpos=b,
keepspaces=true,
backgroundcolor=\color{backcolor},
keywordstyle=[1]\color{blue}\bf,
keywordstyle=[2]\color{red}\bf,
keywordstyle=[3]\color{cyan!50}\bf,
stringstyle=\color{orange},
commentstyle=\color{comment},
tabsize=2,
% number=left,
% numberstep=5pt,
showspaces=false,
showstringspaces=false,
showtabs=false,
moredelim=[is][\component]{component\ }{\ is},
morekeywords=[1]{
library, use ,all,entity,is,port,in,out,end,architecture,of, body,
function, variable, begin,and,or,Not,downto,ALL, signal, process, if,
else, elsif, case, when, then, range, to, component, type, with, select,
others, constant, inout, buffer, map, true, false, array, subtype, wait,
wait for, generic, =, <, >, <=, >=, =>,
},
alsoletter={=, <, >},
morekeywords=[2]{
STD_LOGIC_VECTOR,STD_LOGIC,IEEE,STD_LOGIC_1164, work, local, real,
math_real, time, NUMERIC_STD,STD_LOGIC_ARITH,STD_LOGIC_UNSIGNED,
std_logic_vector, std_logic, ieee, numeric_std, std_ulogic,
std_logic_1164, natural, bit, bit_vector, signed, unsigned,
boolean, integer
},
morekeywords=[3]{rising_edge, falling_edge, resize, to_signed, to_unsigned},
morecomment=[l]{--},
morecomment=[s][\color{orange}]{'}{'},
rulecolor=\color{black},
}
\def\component#1{%
\textbf{\textcolor{blue}{component\ }}%
\textcolor{green}{#1}%
\textbf{\textcolor{blue}{\ is}}%
}
\lstset{style=vhdl}
\begin{document}
VHDL code:
\begin{lstlisting}[style=vhdl]
component Input is
port(
startButton : in std_logic; -- When 1, start game
fire : in std_logic; -- When 1, shoot a rocket
clk : in std_logic; -- 40MHz
reset : in std_logic; -- Active high
left : in std_logic; -- Left arrow button
right : in std_logic; -- Right arrow button
newMissile : out std_logic; -- If 1, new missile launched
gameStarted : out std_logic; -- When 0, show start screen
alienX : out std_logic_vector(9 downto 0); -- first alien position from left screen
alienY : out std_logic_vector(8 downto 0); -- first alien position from top screen
shipPosition : out std_logic_vector(9 downto 0) -- Ship x coordinate
);
end component;
\end{lstlisting}
\end{document}