1

I have written some source code using listings package but is stretching out of the page. This package leaves huge spaces between the words, so is there a way to make them smaller? At least like the spaces in normal text?

Also I tried to use emph and emphstyle and it only identifies single words. I wrote for each and it doesn't recognize it. What should I do to recognize multiple words?

\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}

closed as unclear what you're asking by siracusa, Mensch, Phelype Oleinik, Troy, Stefan Pinnow Oct 19 '18 at 4:14

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    It would be helpful if you composed a fully compilable MWE including \documentclass and the appropriate packages that sets up the problem. While solving problems can be fun, setting them up is not. Then, those trying to help can simply cut and paste your MWE and get started on solving the problem. – Peter Grill Oct 28 '14 at 4:40
  • Ok I will edit now! – Adam Oct 28 '14 at 4:40
  • 1
    You seem to be asking two unrelated questions, here. On TeX.SX, we try to keep unrelated questions on separate pages. If you have multiple questions that are unrelated to one another, you should ask each in a separate TeX.SX "question". You'll stand a better chance of getting a satisfactory answer to each of your questions. – jub0bs Oct 28 '14 at 8:02
  • @Jubobs I thought that because the problems appeared together and there were both regarding listings it would be better not to keep asking about the same package. If there is a problem I can of course separate them. – Adam Oct 28 '14 at 8:05
  • 2
    Add also columns=flexible to \lstset. – egreg Oct 28 '14 at 8:21