General ⇒ Creating a line break in a code sample
- tripwire45
- Posts: 129
- Joined: Thu Apr 10, 2008 4:35 am
Creating a line break in a code sample
I've been tasked with creating some code samples in one of the documents I maintain. The easiest way for me to do this is to put the code between two verbatim tags. That way, the code will appear in the PDF exactly the way it was written, special symbols and all.
The one bad thing that happens is that very long lines of code run off the right side of the page. I know there is a special symbol that you can include to indicate when an otherwise single line of code is broken, usually for vertical space, but I can't find out how to insert it in LaTeX...particular within two verbatim tags. I'm attaching part of the code sample and the PDF output to show you the issue at hand (I don't think this one needs a minimal working example).
The standard texts and Googling aren't yielding results this time. Can you help out here? Thanks.
-Trip
The one bad thing that happens is that very long lines of code run off the right side of the page. I know there is a special symbol that you can include to indicate when an otherwise single line of code is broken, usually for vertical space, but I can't find out how to insert it in LaTeX...particular within two verbatim tags. I'm attaching part of the code sample and the PDF output to show you the issue at hand (I don't think this one needs a minimal working example).
The standard texts and Googling aren't yielding results this time. Can you help out here? Thanks.
-Trip
- Attachments
-
- code2.png (13.8 KiB) Viewed 13390 times
-
- code1.png (5.89 KiB) Viewed 13390 times
- Stefan Kottwitz
- Site Admin
- Posts: 10290
- Joined: Mon Mar 10, 2008 9:44 pm
Creating a line break in a code sample
LaTeX.org admin
- localghost
- Site Moderator
- Posts: 9201
- Joined: Fri Feb 02, 2007 12:06 pm
Creating a line break in a code sample
The listings package should offer suitable options. The documentation shows you how to setup a listing environment that fits your needs. Options for many programming languages are available.
Best regards
Thorsten¹
Best regards
Thorsten¹
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes[/size]
¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1
Board Rules
Avoidable Mistakes[/size]
¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1
- tripwire45
- Posts: 129
- Joined: Thu Apr 10, 2008 4:35 am
Re: Creating a line break in a code sample
Wow! That was quick. I'll have a look at the documentation and let you know how it works. Thanks.
-Trip
-Trip
- tripwire45
- Posts: 129
- Joined: Thu Apr 10, 2008 4:35 am
Creating a line break in a code sample
Well, it worked...sort of. Here's what I added to the preamble:
That allows particularly long lines to be broken with arrows indicating the break. See the attachment "break1.png" for the result. One problem this introduces is some rather odd symbols intermixed with the code. Here's a sample line:
How it renders in the PDF is shown in attachment "symbol1.png".
Code: Select all
\usepackage{color,listings}
\lstset{breaklines=true,breakindent=0pt,
prebreak=\mbox{\tiny$\searrow$},
postbreak=\mbox{{\color{blue}\tiny$\rightarrow$}}}
Code: Select all
Response.Write(WebResp.StatusCode & "<br />")
- Attachments
-
- symbol1.png (6.56 KiB) Viewed 13368 times
-
- break1.png (7.66 KiB) Viewed 13371 times
- Stefan Kottwitz
- Site Admin
- Posts: 10290
- Joined: Mon Mar 10, 2008 9:44 pm
Re: Creating a line break in a code sample
Hi Trip,
it looks like a visible space. Did you set showspaces=true? Or try to set it to false.
Stefan
it looks like a visible space. Did you set showspaces=true? Or try to set it to false.
Stefan
LaTeX.org admin
- tripwire45
- Posts: 129
- Joined: Thu Apr 10, 2008 4:35 am
Creating a line break in a code sample
Here's what I added:
I'm attaching two images, "showspacestrue.png" and "showspacesfalse.png" to illustrate the differences. Don't quite seem to be working out, though.
Code: Select all
\usepackage{color,listings}
\lstset{showspaces=true,breaklines=true,breakindent=0pt,
prebreak=\mbox{\tiny$\searrow$},
postbreak=\mbox{{\color{blue}\tiny$\rightarrow$}}}
- Attachments
-
- showspacetrue.png (6.51 KiB) Viewed 13365 times
-
- showspacefalse.png (6.25 KiB) Viewed 13366 times
- Stefan Kottwitz
- Site Admin
- Posts: 10290
- Joined: Mon Mar 10, 2008 9:44 pm
Creating a line break in a code sample
Since "<br />" is a string try showstringspaces=false.
Stefan
Stefan
LaTeX.org admin
- tripwire45
- Posts: 129
- Joined: Thu Apr 10, 2008 4:35 am
Re: Creating a line break in a code sample
You're a genius, Stefan. It worked. Thanks. 

- Stefan Kottwitz
- Site Admin
- Posts: 10290
- Joined: Mon Mar 10, 2008 9:44 pm
Re: Creating a line break in a code sample
You could have found it inside the documentation too.
Stefan

Stefan
LaTeX.org admin