[Tex/LaTex] Lstlisting in twocolumn document

listingstwo-column

I have a problem when using lstlisting in twocolumn document. At my university I have to make reports in twocolumn documents. My teacher send me pre done .tex file with this code in it:

\documentclass[a4paper, 10pt, twocolumn]{article}
\setlength{\columnsep}{5mm}
\usepackage{anysize}
\marginsize{1cm}{1cm}{1cm}{1cm}
\usepackage{polski}
\usepackage[cp1250]{inputenc}
%\usepackage[framed, numbered]{mcode}
\usepackage{listings}
\usepackage{graphicx}
\usepackage[usenames,dvipsnames]{color}
\usepackage{marvosym} %dla symbolu euro
\usepackage{array}
\usepackage[section] {placeins}
\renewcommand{\baselinestretch}{1.15} %interlinia

But when I use lstlisting function, text from 1st column overlaps 2nd column, and I have to manually wrap too long lines to make it clear. Is it any better way of dealing with such a thing? I'm new to LaTeX, so please be as precise as possible. 🙂 Sorry for my english, I'm not a native english speaker.

Best Answer

Use the breaklines=true option:

\documentclass[a4paper, 10pt, twocolumn]{article}
\usepackage{listings}
\usepackage{lipsum}

\lstset{breaklines=true}

\begin{document}

\lipsum[4]
\begin{lstlisting}
test listing test listing test listing test listing test listing test listing test listing test listing test listing test listing test listing
\end{lstlisting}
\lipsum[4]

\end{document}

enter image description here

Related Question