[Tex/LaTex] How to tell alltt environment to wrap text

listingsverbatim

I have an already formatted text (space-wise) that is also being colored etc, so I am using alltt environment to print it. The text is code actually, but I am not using listings because I don't want it to color my code (I am doing the coloring based on which line is important so it's different).

Problem is now, I can't figure a way to tell alltt to wrap the text. It would eventually look ugly if the text is wrapped, but it is even more ugly if they go out of the right border of the page!!

So, is there a way to wrap text with alltt? If no, is it possible to get text that has styles (such as \textit{...} etc) with the listings package?

Edit: Here is an example:

Preamble (some styling):

\newcommand{\somewhatImportantCode}[1]{\textcolor[rgb]{0.0,0.35,0.6}{#1}}
\newcommand{\veryImportantCode}[1]{\textcolor[rgb]{0.0,0.5,0.9}{#1}}

Code:

\begin{alltt}
\somewhatImportantCode{\#include <iostream>}

int main()
\{
  \veryImportantCode{std::cout <<} "Hello World!" \veryImportantCode{<< std::endl};
  return 0;
\}
\end{alltt}

And here is how it looks like:

enter image description here

Note that that goal is to direct the user to parts of the code that are more significant and that's why its styling is different. As you can see, the \somewhatImportantCode and \veryImportantCode could cover any short or long part of the code.

Best Answer

With listings you just need to add breaklines=true to enable line breaking, you can also include commands that are to be executed by defining an escapechar which lets you put custom formatting commands.

enter image description here

If you don't want the italics formatting applied to the keywords, just remove the setting keywordstyle={}.

\documentclass[border=2pt]{standalone}
\usepackage{xcolor}
\usepackage{listings}

\newcommand{\somewhatImportantCode}[1]{\textcolor[rgb]{0.0,0.35,0.6}{#1}}
\newcommand{\veryImportantCode}[1]{\textcolor[rgb]{0.0,0.5,0.9}{#1}}

\lstset{
  language=C,
  basicstyle=\ttfamily,
  showstringspaces=false,
  breaklines=true,
  keywordstyle={\textit},
  %morekeywords={text},% list your attributes here, 
  escapechar=\&% char to escape out of listings and back to LaTeX
}

\begin{document}
\begin{lstlisting}
&\somewhatImportantCode{\#include <iostream>}&

int main()
{
  &\veryImportantCode{std::cout <<}& "Hello World!" &\veryImportantCode{<< std::endl}&;
  return 0;
}
\end{lstlisting}
\end{document}