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}
Adam
  • 4,684
  • 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 04:40
  • Ok I will edit now! – Adam Oct 28 '14 at 04: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 08: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 08:05
  • 2
    Add also columns=flexible to \lstset. – egreg Oct 28 '14 at 08:21
  • @egreg thank you I added it and it kind of helped but it is still a pretty big gap. – Adam Oct 28 '14 at 17:36
  • @Adam I can't see big gaps: the space between words is the same width as the letters. – egreg Oct 28 '14 at 17:43
  • @egreg this is a little subjective but I copied some code from a text book and the one from listings stretches far more that the one in the book. And this with a monospace font! It seems logical to me that there must be a parameter in listings to change that, as it is importand in source code to keep some context in the same line. – Adam Oct 28 '14 at 18:12
  • @Adam About your first question, see http://tex.stackexchange.com/questions/27653/how-to-emphasize-within-a-listing-two-successive-identifiers-separated-by-a-spac?rq=1. About your second question: you may have tab characters in your listings. They have a width of 8 spaces by default. That may be why your listing appears too wide. – jub0bs Oct 29 '14 at 14:41
  • @Jubobs thank you. I have set the tabs in spacific number of spaces but I will check it again. :) – Adam Oct 29 '14 at 18:31

0 Answers0