By suppressing a line number, I mean something like the following:
Line 16 is split across two lines in the figure, but it's really one code statement, and I didn't want to have to refer to the statement using multiple line numbers in the text. Here is the LaTeX source for the key line:
String firstName = /*@\\@*/ /*@\underline{fullName.substring(0,spaceInd-1)};@*/
Note that since this snippet is used within the listings environment, the whitespace and the lack of a newline are significant. I used
\lstset{escapeinside={/*@}{@*/}}
before this listing to declare the escape sequence for adding other formatting. The {\*@\\@*/}
inserts a linebreak, and then the spaces before the next characters appear as indentation in the figure. Anyway, I googled around and couldn't find this trick, so hopefully this writeup will be useful to someone.
UPDATE (10/27/2008): In response to a request in a comment below, here is a minimal-ish full example that uses the technique. I've confirmed that this example works with the MacTex 2008 distribution (pdfTeXk, Version 3.1415926-1.40.9 and version 1.4 of the listings package).
\documentclass{article}
\usepackage{listings}
\lstset{escapeinside={/*@}{@*/}}
\begin{document}
\begin{figure}[t]
\begin{lstlisting}[numbers=left]
String firstName = /*@\\@*/ /*@\underline{fullName.substring(0,spaceInd-1)};@*/
\end{lstlisting}
\caption{Example illustrating suppression of line numbers.}
\end{figure}
\end{document}
5 comments:
Yup, you rescued my day of searching
Unfortunately, it doesn't work for me. (The second part of the broken line is numbered, too.) Maybe you could post the complete example with all options?
Hi, tried your idea, works fine in case you have no frame around the listing. But if you have one, it's broken afterwards.
But there is a better (and much easier) solution.
\lstset{breaklines = {true}}
In combination with a minipage around the listing, it works perfect.
\begin{figure}
\centering
\begin{minipage}{150mm}
\begin{lstlisting}
>>your code here<<
\end{lstlisting}
\end{minipage}
\caption{your caption}
\label{your_label}
\end{figure}
Thanks! This helped me make my own solution. By-the-way, using /*@//@*/ to force a new line breaks the background color (if you set it to anything but white). I also wanted more control over how lines were broken than the `breaklines' option provides.
I created a short style file:
lstnumbermajic.sty
----------------------------
\newcommand{\lstomitnum}{\global\advance\lst@skipnumbers-1}
\newcommand{\lstcontinue}{\global\advance\lst@skipnumbers-1
\global\advance\c@lstnumber-1}
\newcommand{\lstskipnum}[1]{\addtocounter{lstnumber}{#1}}
------------------------------
Including this in the preamble, lines can be broken with:
1 String MyString= \*@\lstcontinue@*\
"Hello world!"
2 double x;
3 double y; \*@\lstskipnum{8}\lstomitnum@*\
...
13 double z=x+y;
You then just break lines manually, the way you want.
@Antonio: awesome! I will edit the post and recommend your solution.
Post a Comment