GeneralLong Lines in Code Listings

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
xatru
Posts: 1
Joined: Fri Sep 16, 2011 10:20 am

Long Lines in Code Listings

Post by xatru »

Greetings,

I'm the new one here ;) I started to write my Bachelor Thesis with LaTeX, had some problems but found good solutions here. Now I've a problem where I can't find any solution :( I use the Listings-Package for source code in my work.

Code: Select all

\usepackage{listings}
The general settings for listings are as the follow. I defined my own rules for JavaScript-Language (because JavaScript isn't supported)

Code: Select all

\lstset{
   language=JavaScript,
   breakatwhitespace=false,
   backgroundcolor=\color{lightgray},
   extendedchars=true,
   basicstyle=\footnotesize\ttfamily,
   showstringspaces=false,
   showspaces=false,
   numbers=left,
   numberstyle=\footnotesize,
   numbersep=9pt,
   tabsize=2,
   breaklines=true,
   showtabs=false,
   captionpos=b
}
This works fine - until yet. Now I’ve a .js file with very long names or variables, contents and so on (it’s a .js file for penetration testing). As an example:

Code: Select all

var x = <800*f>
<800*f> means ‘f’ is repeated 800 times. If I compile the document now I get the following error:

Code: Select all

Dimension too large ...
Is there any way to say the Listings-Package it should always break the line after a definite number of characters, or does anybody have an better idea how to put such long strings in a LaTeX-Document?

Thanks for help!

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

justdeath
Posts: 69
Joined: Mon Sep 05, 2011 10:27 am

Long Lines in Code Listings

Post by justdeath »

Your code does not compile, but this one does:

Code: Select all

\documentclass{article}
\usepackage{listings}
\usepackage{color}
\definecolor{lightgray}{RGB}{216,216,216}

\lstset{
   language=Java,
   breakatwhitespace=false,
   backgroundcolor=\color{lightgray},
   extendedchars=true,
   basicstyle=\footnotesize\ttfamily,
   showstringspaces=false,
   showspaces=false,
   numbers=left,
   numberstyle=\footnotesize,
   numbersep=9pt,
   tabsize=2,
   breaklines=true,
   showtabs=false,
   captionpos=b
}

\begin{document}
Some text.

\lstinputlisting[language=Java]{test.js}

More text.
\end{document}
And everything looks OK.

I am a loss what means:
<800*f> means ‘f’ is repeated 800 times
The package listings just displays programming code in a fancy way, it does not multiply variables. It just reads the file line by line and displays it accordingly to the language chosen.

You can try increasing TeX memory:
http://www.latex-community.org/forum/vi ... f=9&t=1872
http://pp3.sourceforge.net/manual/Incre ... emory.html

Nikolay
gnwiii
Posts: 1
Joined: Sat Oct 01, 2011 7:19 pm

Long Lines in Code Listings

Post by gnwiii »

Many languages support a line continuation mechanism to accept long identifiers and string constants in source files, such as ending a line with a backslash. There are also tools to reformat source files to use consistent indenting, etc. -- some allow you to set a max. line length and will use the appropriate continuation mechanism. If you can't find such a tool for your language, you can create your own simple processor. You may need two programs, one to wrap lines and another to unwrap before compiling if the language doesn't have a suitable continuation mechanism.

<http://jsbeautifier.org/> might be a starting point for javascript source files.
Post Reply