Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

First Issue

I'm using the listings package to present some algorithms using pseudocode. The problem is that sometimes the lines of my pseudocode are too "long" and listings breaks them in 2 lines. For example, you can see the following screenshots:

enter image description here

and

enter image description here

in which in the first case the line is broken into 3 other lines while in the second example the ) is moved to the next line.

Is there any way to fix that? E.g. by making a specific line inside listing have smaller letters?

Second Issue

Also I use \lstinline{} for keywords that appear in the pseudocode inside my document. The problem is that sometimes it doesn't work "fine" as can be seen below:

enter image description here

counter in the document is inside a \lstinline{} snippet and the . is after it but the created document moves the . to the next line which isn't that nice (in appearance). Any ideas how to solve it?

share|improve this question

closed as unclear what you're asking by Jubobs, Jesse, cmhughes, Peter Jansson, Claudio Fiandrino Jan 16 at 7:24

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.

3  
Please add a minimal working example (MWE) that illustrates your problem. It would just speed up your changes of getting a solution. –  Werner Sep 20 '12 at 21:25
add comment

1 Answer 1

Package listings uses in its default setting a fixed width for each letter. The width is quite large, because letter such as M must fit in. For source code listings the requirements for the vertical alignment are not very high. Thus a flexible columns layout often looks much better. See 2.10 Fixed and flexible columns of the documentation. Flexible settings use a column width of 0.4em instead of 0.6em for setting flexible.

Source code also contains lots of words. Setting the words in a font with variable glyph widths improve the look of the words. Even for typewriter fonts there are variable width fonts. For example, the Latin Modern font family (successor of Computer Modern) contains such a variable typewriter font, see the examples below. The line length usually shrinks, because letters with smaller widths (e.g. i) need less space as in a font with equal width for all glyphs.

The following shows an example with non-variable/variable typewriter font and the three settings for the columns. It also shows the problematic alignment cases for the flexible column layouts.

\NeedsTeXFormat{LaTeX2e}
\documentclass[12pt,a4paper]{article}
\setlength{\overfullrule}{5pt}

% use variable width typewriter font
\usepackage[T1]{fontenc}
\usepackage[
  variablett
]{lmodern}
\usepackage{listings}
\lstset{basicstyle=\ttfamily}

\begin{document}
  \newcommand*{\test}[1]{%
    \lstinputlisting[
      language  = {[LaTeX]TeX},
      literate  = {TeX}{\TeX}3
                  {LaTeX}{\LaTeX}4
                  {LaTeX2e}{\LaTeXe}6,
      moretexcs = {NeedsTeXFormat,lstset,lstinputlisting},
      columns   = {#1},
    ]{\jobname.tex}%
  }
  \renewcommand*{\ttdefault}{lmtt}% not variable
  \test{fixed}\hrule\test{flexible}\hrule\test{fullflexible}
  \newpage
  \renewcommand*{\ttdefault}{lmvtt}% variable
  \test{fixed}\hrule\test{flexible}\hrule\test{fullflexible}
\end{document}

The following images contains the source code listings in the following settings

fixed/non-variable        fixed/variable
flexible/non-variable     flexible/variable
fullflexible/non-variable fullflexible/variable

Comparison


Another possibilty is the use of a smaller font size for the listings to reduce the line width.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.