列表包中的单词之间的间距以及强调标识符

列表包中的单词之间的间距以及强调标识符

我使用包编写了一些源代码listings,但超出了页面范围。这个包在单词之间留出了很大的空白,有没有办法让它们变小?至少像普通文本中的空格一样?

我还尝试使用emph,但emphstyle它只能识别单个单词。我写了for each,但它无法识别。我该怎么做才能识别多个单词?

\documentclass[12pt]{article}

\usepackage
[
  top=0.7in,
  bottom=1.2in,
  left=0.8in,
  right=0.8in
]{geometry}


\usepackage{parskip}

\setlength{\parindent}{0cm}

\usepackage[fleqn]{amsmath}

\usepackage{unicode-math}

\usepackage{fontspec}

\usepackage[english,greek]{babel}


\setmainfont
[
  Ligatures=TeX,
  Extension=.otf,
  UprightFont=*,
  BoldFont=*Bold,
  ItalicFont=*It,
  BoldItalicFont=*BoldIt,
  Mapping=tex-text
]{GFSArtemisia}

\setsansfont[Mapping=tex-text]{GFSArtemisia.otf}


%Math fonts

\setmathfont{latinmodern-math.otf}
\setmathfont[range=\varnothing]{Asana-Math.otf}
\setmathfont[range=\int]{latinmodern-math.otf}

\usepackage{listings}

\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}

\begin{document}

\lstset
{
    basicstyle=\ttfamily,
    keepspaces=true,
    emph=
    {
    if, for each, loop do,
    then return, function
    },
    emphstyle=\color{red}
}

\selectlanguage{english}


\begin{lstlisting}[title={Breadth-First Search}]
function BREADTH-FIRST-SEARCH(Problem) return A solution or failure

Node= Root

if Problem.GOAL-TEST(Node.STATE) then return SOLUTION(Node)

Frontier= Node

Explored= Empty

loop do

    if EMPTY(Frontier) then return failure

    Node= POP(Frontier)

    add Node.STATE to Explored

    for each Action in Problem.ACTIONS(Node.STATE) do

        Child= CHILD-NODE(Problem,Node,Action)

        if Child.STATE is not in Explored or Frontier then

            if Problem.GOAL-TEST(Child.STATE) then return SOLUTION(Child)
            Frontier=INSERT(Child, Frontier)
\end{lstlisting}

\end{document}

相关内容