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.

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.

share|improve this question
    
You can surely customize how listings marks the code so that nothing it coloured. Maybe you should post a small listings example document with what kind of line-based colouring you are using and ask the listings question directly. –  Alan Munn Dec 1 '11 at 14:01
    
@AlanMunn, There seems to be a lot on listings on the internet and if there is no way to wrap text in alltt, I don't think I would have a problem figuring out how to make it work with listings. So, let's first see if wrapping text is possible with alltt. If not, then I'll try to figure listings myself –  Shahbaz Dec 1 '11 at 14:05
    
@AlanMunn, you are right, I removed the P.S. which looked as if I wanted to switch to listings –  Shahbaz Dec 1 '11 at 14:06

2 Answers 2

up vote 3 down vote accepted

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}
share|improve this answer
    
Thanks, but the wrap in listings was not my problem. If alltt fails to wrap text, I need to know if listings can substitute it, that is, whether listings can accept commands, like \textit{...}? –  Shahbaz Dec 1 '11 at 14:09
    
Have updated the solution to show how to apply styles with listings. –  Peter Grill Dec 1 '11 at 14:22
    
Alright let me put an example in the question to show what I need (give me 5 minutes) –  Shahbaz Dec 1 '11 at 14:32
    
Interesting work with &. So how do you come back into lstlisting? Have you noticed that your texts are not being emphasized after the styled some? –  Shahbaz Dec 1 '11 at 14:57
    
@Shahbaz: Thanks. I noticed that and corrected it in the new version which uses the OP's code. –  Peter Grill Dec 1 '11 at 15:01

Even though the OP has already accepted the answer of Peter, I would like to suggest the following improvement: It uses listings moredelim=** feature to define active chars that turn on and off a certain formatting. Thereby, escaping to LaTeX and poluting the source code with macros is not necessary:

\documentclass[border=2pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{xcolor}
\usepackage{listings}

\newcommand{\somewhatImportantCode}{\color[rgb]{0.0,0.35,0.6}}
\newcommand{\veryImportantCode}{\color[rgb]{0.0,0.5,0.9}}

\lstset{
  language=C,
  basicstyle=\ttfamily,
  showstringspaces=false,
  breaklines=true,
  keywordstyle={\textit},
  moredelim=**[is][\somewhatImportantCode]{@}{@},
  moredelim=**[is][\veryImportantCode]{@@}{@@},
  moredelim=**[is][\color{red}]{@@@}{@@@},
}

\begin{document}
\begin{lstlisting}
@#include <iostream>  
@
int main()
{
  @@std::cout <<@@ "Hello World!" @@<< std::endl@@;
  return @@@0@@@;
}
\end{lstlisting}
\end{document}

I have used sequence of @ for the different levels of emphasis here, but you can use any (sequence of) character(s) for each; if you prefer, even different ones for "emphasis on" and "emphasis off" (see page 30 of the listings manual).

An additional plus is that (depending on the options used for moredelim=**) the formatting can be applied on top of each other and of the syntax formatting already provided by listings. In the example, this can be seen for the #include directive, which gets the "ImportantCode" color additionally to the \textit formatting of keywords:

screen shot

share|improve this answer
    
This is great thanks. However, in my case the previous one is even simpler. I am generating those tex files from another source and so pouring out macros is not a big deal. Either way, it would be writing the macro or it's shortcut which programming-wise, is no different. –  Shahbaz Dec 1 '11 at 17:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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