8

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.

enter image description here

3 Answers 3

9

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:

Sample output

\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}
2
  • 2
    might also look better using a typewriter font, or perhaps sans serif.
    – daleif
    Dec 10, 2013 at 11:51
  • 1
    @daleif certainly such choices can made by setting basicstyle=\ttfamily or \sffamily... Dec 10, 2013 at 11:57
8

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}

C++ code using lstings

1
  • +1 for this is the real answer that reduces the width without affecting other features of the text (like mono-space)
    – onlycparra
    Mar 18, 2022 at 8:51
3

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}

enter image description here

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .