Discussion:
latex listings - line breaking problem
Jakub Suder
2007-06-16 21:44:53 UTC
Permalink
Hi,

I'm writing my MSc thesis in Lyx, and today I installed 1.5.0rc1 and
tried to use code listings (I have lots of Java examples in my
thesis), but I've encountered a problem. I'm using a custom class from
my university - mgragh (I don't know if the list accepts attachments,
so I've put it here:
http://cassiopea.net.autocom.pl/public/mgragh.zip) and I've noticed
that this class prevents long lines from being broken inside a listing
block.

I have code like this (Lyx view source box):

\begingroup
\inputencoding{latin1}
\begin{lstlisting}[breaklines=true,showstringspaces=false]
(code fragment with long lines)
\end{lstlisting}
\endgroup

And the lines aren't broken, they just extend over the margin and are
cut eventually. When I paste the code into a new empty document using
"article" class, everything is ok, the lines are broken into parts.
Then if I change the document class to mgragh - it stops working.

Do you know what may be wrong? What should I change in the class to
make it compatible with listings?

Regards,
Jakub Suder
Helge Hafting
2007-06-18 10:38:38 UTC
Permalink
Post by Jakub Suder
Hi,
I'm writing my MSc thesis in Lyx, and today I installed 1.5.0rc1 and
tried to use code listings (I have lots of Java examples in my
thesis), but I've encountered a problem. I'm using a custom class from
my university - mgragh (I don't know if the list accepts attachments,
http://cassiopea.net.autocom.pl/public/mgragh.zip) and I've noticed
that this class prevents long lines from being broken inside a listing
block.
\begingroup
\inputencoding{latin1}
\begin{lstlisting}[breaklines=true,showstringspaces=false]
(code fragment with long lines)
\end{lstlisting}
\endgroup
And the lines aren't broken, they just extend over the margin and are
cut eventually. When I paste the code into a new empty document using
"article" class, everything is ok, the lines are broken into parts.
Then if I change the document class to mgragh - it stops working.
Do you know what may be wrong? What should I change in the class to
make it compatible with listings?
Automatically breaking program listings is a very tricky business.
Usually, the automatic breaking isn't good, and can sometimes
even introduce syntax errors. (depending on the language in use.)

When I wrote a programming book, I wrote all the sample code
so that it wasn't too wide. Keep statements short, use
few levels of indentation, and break lines manually when this
isn't enough. Using short identifier names (one letter is often
enough, and avoid the wider uppercase ones.)
Setting code samples with a small narrow font helps too, if
all else fails.

Keeping the statements short and so on
is often an advantage anyway - the code gets easier to read.

Loading...