使用列表定义自定义语言并定义自动缩进。MWE 不执行缩进
\documentclass{book}
\usepackage[formats]{listings}
\usepackage{xcolor}
% Define Language
\lstdefinelanguage{sofl}
{
breaklines=true,
tabsize=2,
sensitive=false, % keywords are not case-sensitive
morecomment=[l]{//}, % l is for line comment
morecomment=[s]{/*}{*/}, % s is for start and end delimiter
morestring=[b]", % defines that strings are enclosed in double quotes
morekeywords={begin,end}
}
\lstdefineformat{sofl}%
{%
\{=\newline\string\newline\indent,%
\}=[;]\newline\noindent\string\newline,%
\};=\newline\noindent\string\newline,%
;=[\ ]\string\space
}%
\lstnewenvironment{sofl} {
\lstset{format=sofl}
\lstset{language=sofl}
} {}
\begin{document}
\begin{sofl}
flow_b ->
begin
if
flow_e = False;
f(data_b) ->
begin
flow_d = True;
data_d = data_b;
end
not f(data_b) ->
begin
flow_d = True;
data_d = data_b;
end
fo
end
\end{sofl}
\end{document}
更新示例以使用此片段时
\lstdefineformat{sofl}%
{%
begin=\newline\string\newline\indent,%
end=[;]\newline\noindent\string\newline,%
end;=\newline\noindent\string\newline,%
;=[\ ]\string\space
}%
编译器给出错误Extra \fi. \begin{sofl}
。