# Why is the space between words in my source code listing so big?

I used the following code to list my java code, but the it looks like there's too much space between words. What can I do?

% source code listing
\usepackage{listings}
\usepackage{color}
\usepackage{xcolor}

\usepackage{caption}

% citation style
\usepackage{apacite}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox{gray}{\parbox{\textwidth}{#1#2#3}}}

\begin{document}
\begin{lstlisting}[label=some-code,caption=bind to service call]
bindService(new Intent(this, Recognizer.class), recConnection, Context.BIND_AUTO_CREATE);
\end{lstlisting}
\end{document}


The snapshot of what I got. The code not completely shown, and space too wide, in function bindService.

If you set columns=flexible then spaces are not stretched as much. Also you should allow breaking of lines, but only at whitespaces. Finally, it is best to tell listings that the language being set is Java:

\documentclass{article}
% source code listing
\usepackage{listings}
\usepackage{color}
\usepackage{xcolor}

\usepackage{caption}

% citation style
\usepackage{apacite}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox{gray}{\parbox{\textwidth}{#1#2#3}}}

\lstset{language=java,columns=flexible,breaklines=true,breakatwhitespace=true}

\begin{document}

\begin{lstlisting}[label=some-code,caption=Bind to service call]
bindService(new Intent(this, Recognizer.class), recConnection, Context.BIND_AUTO_CREATE);
\end{lstlisting}

\end{document}

• might also look better using a typewriter font, or perhaps sans serif. – daleif Dec 10 '13 at 11:51
• @daleif certainly such choices can made by setting basicstyle=\ttfamily or \sffamily... – Andrew Swann Dec 10 '13 at 11:57

If you want to preserve column alignment, but want to reduce inter character spacing, set the basewidth option to a smaller value:

\documentclass{article}
\usepackage{listings}
\usepackage{xcolor} % for setting colors

% set the default code style
\lstset{
language=C++,
frame=tb, % draw a frame at the top and bottom of the code block
tabsize=4, % tab space width
showstringspaces=false, % don't mark spaces in strings
numbers=left, % display line numbers on the left
commentstyle=\color{green}, % comment color
keywordstyle=\color{blue}, % keyword color
stringstyle=\color{red}, % string color
basicstyle=\footnotesize\ttfamily,
basewidth = {.48em}
}

\begin{document}

\begin{lstlisting}[caption={C++ code using listings}]
#include <iostream>
int main() {
// print hello to the console
std::cout << "Hello, world!" << std::endl;
return 0;
}
\end{lstlisting}

\end{document}


You have to use breaklines=true so that the lines are broken in to two.

\documentclass{article}
% source code listing
\usepackage{listings}
\usepackage{color}
\usepackage{xcolor}

\usepackage{caption}

% citation style
\usepackage{apacite}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox{gray}{\parbox{\textwidth}{#1#2#3}}}

\begin{document}
\begin{lstlisting}[breaklines=true,label=some-code,caption=bind to service call]     %% <---here
bindService(new Intent(this, Recognizer.class), recConnection, Context.BIND_AUTO_CREATE);
\end{lstlisting}
\end{document}


• Wouldn't it be better to add that to \lstset in the preamble? He may also want columns=fullflexible (and perhaps basicstyle=\ttfamily). – Torbjørn T. Dec 10 '13 at 11:39
• @TorbjørnT. That is a fair possibility. But my crystal ball is greasy ;-) – user11232 Dec 10 '13 at 11:40