GeneralCreating a line break in a code sample

LaTeX specific issues not fitting into one of the other forums of this category.
User avatar
tripwire45
Posts: 129
Joined: Thu Apr 10, 2008 4:35 am

Creating a line break in a code sample

Post by tripwire45 »

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
Attachments
code2.png
code2.png (13.8 KiB) Viewed 13390 times
code1.png
code1.png (5.89 KiB) Viewed 13390 times

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
Stefan Kottwitz
Site Admin
Posts: 10290
Joined: Mon Mar 10, 2008 9:44 pm

Creating a line break in a code sample

Post by Stefan Kottwitz »

Hi Trip,

did you try the listings package?

Stefan
LaTeX.org admin
User avatar
localghost
Site Moderator
Posts: 9201
Joined: Fri Feb 02, 2007 12:06 pm

Creating a line break in a code sample

Post by localghost »

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¹
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
User avatar
tripwire45
Posts: 129
Joined: Thu Apr 10, 2008 4:35 am

Re: Creating a line break in a code sample

Post by tripwire45 »

Wow! That was quick. I'll have a look at the documentation and let you know how it works. Thanks.

-Trip
User avatar
tripwire45
Posts: 129
Joined: Thu Apr 10, 2008 4:35 am

Creating a line break in a code sample

Post by tripwire45 »

Well, it worked...sort of. Here's what I added to the preamble:

Code: Select all

\usepackage{color,listings}
\lstset{breaklines=true,breakindent=0pt,
        prebreak=\mbox{\tiny$\searrow$},
        postbreak=\mbox{{\color{blue}\tiny$\rightarrow$}}}
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:

Code: Select all

Response.Write(WebResp.StatusCode & "<br />")
How it renders in the PDF is shown in attachment "symbol1.png".
Attachments
symbol1.png
symbol1.png (6.56 KiB) Viewed 13368 times
break1.png
break1.png (7.66 KiB) Viewed 13371 times
User avatar
Stefan Kottwitz
Site Admin
Posts: 10290
Joined: Mon Mar 10, 2008 9:44 pm

Re: Creating a line break in a code sample

Post by Stefan Kottwitz »

Hi Trip,

it looks like a visible space. Did you set showspaces=true? Or try to set it to false.

Stefan
LaTeX.org admin
User avatar
tripwire45
Posts: 129
Joined: Thu Apr 10, 2008 4:35 am

Creating a line break in a code sample

Post by tripwire45 »

Here's what I added:

Code: Select all

\usepackage{color,listings}
\lstset{showspaces=true,breaklines=true,breakindent=0pt,
        prebreak=\mbox{\tiny$\searrow$},
        postbreak=\mbox{{\color{blue}\tiny$\rightarrow$}}}
I'm attaching two images, "showspacestrue.png" and "showspacesfalse.png" to illustrate the differences. Don't quite seem to be working out, though.
Attachments
showspacetrue.png
showspacetrue.png (6.51 KiB) Viewed 13365 times
showspacefalse.png
showspacefalse.png (6.25 KiB) Viewed 13366 times
User avatar
Stefan Kottwitz
Site Admin
Posts: 10290
Joined: Mon Mar 10, 2008 9:44 pm

Creating a line break in a code sample

Post by Stefan Kottwitz »

Since "<br />" is a string try showstringspaces=false.

Stefan
LaTeX.org admin
User avatar
tripwire45
Posts: 129
Joined: Thu Apr 10, 2008 4:35 am

Re: Creating a line break in a code sample

Post by tripwire45 »

You're a genius, Stefan. It worked. Thanks. :D
User avatar
Stefan Kottwitz
Site Admin
Posts: 10290
Joined: Mon Mar 10, 2008 9:44 pm

Re: Creating a line break in a code sample

Post by Stefan Kottwitz »

You could have found it inside the documentation too. ;)

Stefan
LaTeX.org admin
Post Reply