4

I am trying to ident my source code in a frame using the listing package. An example code is given below:

\documentclass[article]{llncs}
    \usepackage[english]{babel}
    \usepackage[english]{varioref}
    \usepackage[utf8]{inputenc}
    \usepackage{graphicx}
    \usepackage{color}
    \usepackage[usenames,dvipsnames]{xcolor}
    \usepackage{listings}
    \lstset{basicstyle=\ttfamily\small\color{RoyalBlue}}
    \usepackage{csquotes}
    \usepackage{xcolor}
\begin{document}
\begin{lstlisting}[rulecolor=\color{black},frame=single,label={lst:dir},caption= example code that overflows the frame]

sssssssss sssss sssssssss ssssssssssss sssssssssssssssssssssssssss

\end{lstlisting}
\end{document}

The problem is some part of the code(letter "s") overflows the frame. I would really appreciate it if someone can suggest any solution for this problem. Please note that I don't want to use linebreaks or any thing similar. In fact I don't want to disturb my code at any cost! Thank you!

ρss
  • 281

1 Answers1

3

If the source code alignments are not much of an issue, the following decreases the lines widths columns=fullflexible together with a variable-sized typewriter font:

\documentclass{article}
\usepackage{color}
\usepackage{listings}
\lstset{
  basicstyle=\fontfamily{lmvtt}\selectfont\small\color{blue},
  columns=fullflexible,
}

\begin{document}
\begin{lstlisting}[
  rulecolor=\color{black},
  frame=single,
]
sssssssss sssss sssssssss ssssssssssss sssssssssssssssssssssssssss
\end{lstlisting}
\end{document}

For more details and examples, see my answer to a similar question.

Result

Smaller font sizes

Instead of \small smaller font sizes could be used:

\small, \footnotesize, \scriptsize, \tiny.

See "The Not So Short Introduction to LaTeX2e", "Table 6.2: Font sizes".

Option linebreaks – answer to an earlier version of the question

Option breaklines allows line breaks in source code listings:

\documentclass{article}
    \usepackage{color}
    \usepackage{listings}
    \lstset{basicstyle=\ttfamily\small\color{blue}}
\begin{document}
\begin{lstlisting}[
  breaklines,
  rulecolor=\color{black},
  frame=single,
]
sssssssss sssss sssssssss ssssssssssss sssssssssssssssssssssssssss
\end{lstlisting}
\end{document}

Result

Heiko Oberdiek
  • 271,626
  • Thanks, but I don't want to use breaklines it will make my code difficult to understand. Is there any way to increase the frame width? – ρss Aug 19 '13 at 20:26
  • You will sooner or later hit the page border. You can try columns=flexible or even columns=fullflexible with variable-width typewriter font (\usepackage[variablett]{lmodern}). Or decrease the font size. And the line lengths of the source file should be limited. – Heiko Oberdiek Aug 19 '13 at 20:32
  • I am using \lstset{basicstyle=\ttfamily\small\color{blue}} is there a font size smaller than "small"? I tried footnotesize even it didn't help. – ρss Aug 19 '13 at 20:35
  • 1
    @pss Due to the way the default fonts work (actually, this may be across the system), setting them even to \tiny won't change the width as much as you'd like it to. Good code should also wrap to around 80 characters; there is never any reason it cannot. (Edit: almost never.) – Sean Allred Aug 19 '13 at 21:00
  • @SeanAllred Thanks, the tiny option worked! but now the font is too small to read. Is there a font size option between small and tiny. ;-) – ρss Aug 19 '13 at 21:14
  • 1
    @pss I would really recommend breaking your code up into readable segments, but a full list of font sizes (along with some other great information as a general reference) is available at the LaTeX Wikibook. – Sean Allred Aug 19 '13 at 21:16
  • @SeanAllred Thanks for the reference! The \scriptsize option solved my problem. If you put your above comment as an answer then I'll select it as a selected answer. :-) Else I'll answer it my self. :P – ρss Aug 19 '13 at 21:27
  • @HeikoOberdiek thanks for the suggestion, but using \fontfamily{lmvtt} option doesn't retains the whitespaces and tabs in my source code. As a result I have to edit the source code. – ρss Aug 19 '13 at 21:35
  • @pss I'm glad I could help, but this question already had an answer here (hence the duplicate). No more answers are needed. – Sean Allred Aug 19 '13 at 21:39