4

This question already has an answer here:

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!

marked as duplicate by egreg, Mensch, mafp, Werner, Sean Allred Aug 19 '13 at 20:58

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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

  • 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
  • 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

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