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}
\documentclassand 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:40listingsit 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:05columns=flexibleto\lstset. – egreg Oct 28 '14 at 08:21listingsstretches 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