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 lstlisting which has breaklines and frame enabled. The actual setting is as follows.

\lstset{
    basicstyle=\ttfamily,
    showstringspaces=false,
    breaklines=true,
    keywordstyle={},
    frame=single,
    frameround=tttt,
    rulecolor=\color{gray},
    escapechar=\&
}

The problem is, whenever a line is too long and thus breaks into two, the frame around it doesn't include the second line. Here is an image of how it looks like:

Broken frame around lstlisting

Edit: Alright, I found out that I was missing some important information. I am styling the listing myself (as opposed to automatically) as you can see in the image. That is why I use & as escapechar and practically all of the contents of the listing is escaped. See this code (note that this is software generated!):

\documentclass{article}
\usepackage{listings, xcolor}

\lstset{
        basicstyle=\ttfamily,
        showstringspaces=false,
        breaklines=true,
        keywordstyle={},
        frame=single,
        frameround=tttt,
        rulecolor=\color{gray},
        escapechar=\&
}
\begin{document}
\begin{lstlisting}
&\#include <stdio.h>&
&int main()&
&\{&
        &\textcolor[rgb]{0.0,0.2,0.4}{printf("Enter a, b, and c (space separated) in the following equation:\textbackslash{}nax\^{}2+bx+c = 0\textbackslash{}n");}&
        &\textcolor[rgb]{0.0,0.2,0.4}{scanf("\%f \%f \%f", \&a, \&b, \&c);}&
        &\textcolor[rgb]{0.0,0.2,0.4}{printf("What happens when I type two less{-}than signs like this? << \textbackslash{}n");}&
&\}&
\end{lstlisting}
\end{document}

In this code, I just used \textcolor for demonstration, although that IS principally what I am doing.

This code produces this output:

enter image description here

This is pdflatex's output:

This is pdfTeX, Version 3.1415926-1.40.11 (TeX Live 2010)
 restricted \write18 enabled.
entering extended mode
(./a.tex
LaTeX2e <2009/09/24>
Babel <v3.8l> and hyphenation patterns for english, dumylang, nohyphenation, ge
rman-x-2009-06-19, ngerman-x-2009-06-19, afrikaans, ancientgreek, ibycus, arabi
c, armenian, basque, bulgarian, catalan, pinyin, coptic, croatian, czech, danis
h, dutch, ukenglish, usenglishmax, esperanto, estonian, ethiopic, farsi, finnis
h, french, galician, german, ngerman, swissgerman, monogreek, greek, hungarian,
 icelandic, assamese, bengali, gujarati, hindi, kannada, malayalam, marathi, or
iya, panjabi, tamil, telugu, indonesian, interlingua, irish, italian, kurmanji,
 lao, latin, latvian, lithuanian, mongolian, mongolianlmc, bokmal, nynorsk, pol
ish, portuguese, romanian, russian, sanskrit, serbian, slovak, slovenian, spani
sh, swedish, turkish, turkmen, ukrainian, uppersorbian, welsh, loaded.
(/usr/local/texlive/2010/texmf-dist/tex/latex/base/article.cls
Document Class: article 2007/10/19 v1.4h Standard LaTeX document class
(/usr/local/texlive/2010/texmf-dist/tex/latex/base/size10.clo))
(/usr/local/texlive/2010/texmf-dist/tex/latex/listings/listings.sty
(/usr/local/texlive/2010/texmf-dist/tex/latex/graphics/keyval.sty)
(/usr/local/texlive/2010/texmf-dist/tex/latex/listings/lstmisc.sty)
(/usr/local/texlive/2010/texmf-dist/tex/latex/listings/listings.cfg))
(/home/sciabaz/texmf/tex/latex/xcolor.sty
(/usr/local/texlive/2010/texmf-dist/tex/latex/latexconfig/color.cfg)
(/usr/local/texlive/2010/texmf-dist/tex/latex/pdftex-def/pdftex.def)) (./a.aux)
(/usr/local/texlive/2010/texmf-dist/tex/context/base/supp-pdf.mkii
[Loading MPS to PDF converter (version 2006.09.02).]
)

LaTeX Font Warning: Font shape `OMS/cmtt/m/n' undefined
(Font)              using `OMS/cmsy/m/n' instead
(Font)              for symbol `textbraceleft' on input line 19.

[1{/usr/local/texlive/2010/texmf-var/fonts/map/pdftex/updmap/pdftex.map}]
(./a.aux)

LaTeX Font Warning: Some font shapes were not available, defaults substituted.

 )</usr/local/texlive/2010/texmf-dist/fonts/type1/public/amsfonts/cm/cmr10.pfb>
</usr/local/texlive/2010/texmf-dist/fonts/type1/public/amsfonts/cm/cmsy10.pfb><
/usr/local/texlive/2010/texmf-dist/fonts/type1/public/amsfonts/cm/cmtt10.pfb></
usr/local/texlive/2010/texmf-dist/fonts/type1/public/amsfonts/latxfont/lcircle1
.pfb>
Output written on a.pdf (1 page, 34186 bytes).
Transcript written on a.log.

Note that with Mike's code, I didn't get the font warning.

share|improve this question
    
As a quick side question, how do I prevent latex from converting << to a single character? I don't know how the proper english words are to be able to search for it! –  Shahbaz Feb 12 '12 at 0:24
    
Answer to my previous commen: I used <{}< –  Shahbaz Feb 13 '12 at 0:48
add comment

2 Answers

up vote 1 down vote accepted

I think what is happening is that since the multiple lines are produces while you are escaped to LaTeX, the lstlisting environment does not get a chance to add the vertical rule on the left and right hand side.

One simple way around this would be to use some other tool to actually put the frame around the code. Here is an example of using an mdframed environment (but any other alternate should work just as well) to produce:

enter image description here

\documentclass{article}
\usepackage{listings, xcolor}
\usepackage[framemethod=TikZ]{mdframed}

\lstset{
        basicstyle=\ttfamily,
        showstringspaces=false,
        breaklines=true,
        keywordstyle={},
        frame=none,%              changed to "none"
        %frameround=tttt,%        commented as not relevant with frame=none
        rulecolor=\color{gray},
        escapechar=\&
}
\begin{document}
\begin{mdframed}[roundcorner=5pt]
\begin{lstlisting}
&\#include <stdio.h>&
&int main()&
&\{&
        &\textcolor[rgb]{0.0,0.2,0.4}{printf("Enter a, b, and c (space separated) in the following equation:\textbackslash{}nax\^{}2+bx+c = 0\textbackslash{}n");}&
        &\textcolor[rgb]{0.0,0.2,0.4}{scanf("\%f \%f \%f", \&a, \&b, \&c);}&
        &\textcolor[rgb]{0.0,0.2,0.4}{printf("What happens when I type two less{-}than signs like this? << \textbackslash{}n");}&
&\}&
\end{lstlisting}
\end{mdframed}
\end{document}
share|improve this answer
    
That was what I thought also. But good to know there is a workaround for this. I will give it a shot and let you know –  Shahbaz Feb 13 '12 at 21:39
    
Apparently the package mdframed is much changed from TeXLive 2010 to TeXLive 2011. I installed 2011 and it works perfectly now. Thanks! –  Shahbaz Feb 14 '12 at 14:13
add comment

I'm not able to reproduce either of your questions with the following MWE:

\documentclass{article}
\usepackage{listings,xcolor}
\lstset{
    basicstyle=\ttfamily,
    showstringspaces=false,
    breaklines=true,
    keywordstyle=\color{red},
    frame=single,
    frameround=tttt,
    rulecolor=\color{gray},
}
\begin{document}
\begin{lstlisting}[language=C]
#include <stdio.h>
int main()
{
  printf("Enter a, b, and c (space separated) in the following equation:\nax^2+bx+c = 0\n");
  scanf("%f %f %f", &a, &b, &c);
  printf("What happens when I type two less-than signs like this? <<\n");
}
\end{lstlisting}
\end{document}

enter image description here

Do you get the same results using my code? If not, what TeX distribution are you using? If so, can you provide an alternate MWE that shows your problems?

EDIT:

I suspect the method you're using is going to cause a ton of extra work, and since I'm not sure what you're really trying to accomplish (why are you escaping out almost everywhere?), here's a second example -- the first part colors all identifiers the same way, the second part colors specific identifiers, and leaves others unformatted.

\documentclass{article}
\usepackage{listings,xcolor}
\lstset{
    basicstyle=\ttfamily,
    showstringspaces=false,
    breaklines=true,
    %keywordstyle=\color{red},
    %identifierstyle=\color{blue},
    %stringstyle=\color{green},
    frame=single,
    frameround=tttt,
    rulecolor=\color{gray},
}
\begin{document}
\begin{lstlisting}[language=C,identifierstyle=\color{blue},caption={highlight all identifiers}]
#include <stdio.h>
int main()
{
  float a, b, c;
  printf("Enter a, b, and c (space separated) in the following equation:\nax^2+bx+c = 0\n");
  scanf("%f %f %f", &a, &b, &c);
  printf("What happens when I type two less-than signs like this? <<\n");
}
\end{lstlisting}
\begin{lstlisting}[language=C,
  emph={printf},emphstyle=\color{blue},
  emph={[2]{scanf}},emphstyle={[2]\color{magenta}},
  caption={highlight specific words}
]
#include <stdio.h>
int main()
{
  float a, b, c;
  printf("Enter a, b, and c (space separated) in the following equation:\nax^2+bx+c = 0\n");
  scanf("%f %f %f", &a, &b, &c);
  printf("What happens when I type two less-than signs like this? <<\n");
}
\end{lstlisting}
\end{document}

enter image description here

share|improve this answer
    
indeed, I had missed a key information in my question. Please see my edit. –  Shahbaz Feb 13 '12 at 14:06
    
Made a second example set. What are you really trying to do here? I suspect there has to be a better way than manually coloring various line of code. –  Mike Renfro Feb 13 '12 at 14:47
    
Mike, you are right. Normally, I wouldn't do such a crazy thing! However, I'm not writing the latex code manually. I have written a program taking input similar to markdown, with some added stuff (more to come) mainly to create code documentation. The output are both html and latex. As far as the program is concerned, it is MUCH easier to escape everything, than try to understand what to escape and what not. –  Shahbaz Feb 13 '12 at 15:20
    
If you are interested in my program by the way, you can find it here –  Shahbaz Feb 13 '12 at 15:21
    
And the reason listings options don't work for me is that, I'm not highlighting specific keywords. I don't even care what language the code is written in, or even if it is written in a language. I color parts of the text as is dictated by the writer of the documentation source file. The main reason for highlighting is to direct attention of reader to a specific important command, or part of the text, rather than highlighting code for overall readability. –  Shahbaz Feb 13 '12 at 15:25
show 4 more comments

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.