Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm writing a LaTeX document for a hands-on workshop which uses shell/bash commands. I'm making use of the listings package to format the chunks of code.

Some of the commands are rather long so I'm making use of breaklines and breakatwhitespace.

Here's my MWC:

\documentclass{book}

\usepackage{listings}
\usepackage{color}
\usepackage{xcolor}

\definecolor{darkgreen}{rgb}{0,0.9,0}

\lstset{%
    columns=fullflexible,
    aboveskip=5pt,
    belowskip=10pt,
    basicstyle=\small\ttfamily,
    numbers=left,
    numberstyle=\tiny\color{black!85},
    stepnumber=1, 
    numbersep=13pt,
    backgroundcolor=\color{black!5},
    showspaces=false,
    showstringspaces=false,
    showtabs=false,
    xleftmargin=20pt,
    xrightmargin=10pt,
    framesep=5pt,
    framerule=3pt,
    frame=leftline,
    rulecolor=\color{darkgreen},
    tabsize=2,
    breaklines=true,
    breakatwhitespace=true,
}

\begin{document}

\begin{lstlisting}
cd ~/
time velveth run_25 25 -fmtAuto -create_binary -shortPaired -separate SRR022863_1.fastq.gz SRR022863_1.fastq.gz -shortPaired2 -separate SRR022852_1.fastq.gz SRR022852_2.fastq.gz
time velvetg run_25
\end{lstlisting}

\end{document}

This is displayed as:

Current Styling

However, I'd like to the listing to include a line continuation character (\) at the ends of autobroken lines. Is there a way that listing can do this? This is what I'd like the listing to look like (with or without line numbers on the indented lines) without explicitly putting in the backslash:

Required Styling

share|improve this question

1 Answer 1

up vote 6 down vote accepted

You can use prebreak=<tokens> to insert <tokens> at the end of the current line:

\documentclass{book}

\usepackage{listings}
\usepackage{color}
\usepackage{xcolor}

\definecolor{darkgreen}{rgb}{0,0.9,0}

\lstset{%
    columns=fullflexible,
    aboveskip=5pt,
    belowskip=10pt,
    basicstyle=\small\ttfamily,
    numbers=left,
    numberstyle=\tiny\color{black!85},
    stepnumber=1, 
    numbersep=13pt,
    backgroundcolor=\color{black!5},
    showspaces=false,
    showstringspaces=false,
    showtabs=false,
    xleftmargin=20pt,
    xrightmargin=10pt,
    framesep=5pt,
    framerule=3pt,
    frame=leftline,
    rulecolor=\color{darkgreen},
    tabsize=2,
    breaklines=true,
    breakatwhitespace=true,
   prebreak=\small\symbol{'134}
}

\begin{document}

\begin{lstlisting}
cd ~/
time velveth run_25 25 -fmtAuto -create_binary -shortPaired -separate SRR022863_1.fastq.gz SRR022863_1.fastq.gz -shortPaired2 -separate SRR022852_1.fastq.gz SRR022852_2.fastq.gz
time velvetg run_25
\end{lstlisting}

\end{document}

enter image description here

Depending on the font used, it could be better to replace

prebreak=\small\symbol{'134}

with

prebreak=\textbackslash

after loading fontenc with the Cork encoding:

\usepackage[T1]{fontenc}

A complete example:

\documentclass{book}
\usepackage[T1]{fontenc}
\usepackage{listings}
\usepackage{color}
\usepackage{xcolor}

\definecolor{darkgreen}{rgb}{0,0.9,0}

\lstset{%
    columns=fullflexible,
    aboveskip=5pt,
    belowskip=10pt,
    basicstyle=\small\ttfamily,
    numbers=left,
    numberstyle=\tiny\color{black!85},
    stepnumber=1, 
    numbersep=13pt,
    backgroundcolor=\color{black!5},
    showspaces=false,
    showstringspaces=false,
    showtabs=false,
    xleftmargin=20pt,
    xrightmargin=10pt,
    framesep=5pt,
    framerule=3pt,
    frame=leftline,
    rulecolor=\color{darkgreen},
    tabsize=2,
    breaklines=true,
    breakatwhitespace=true,
   prebreak=\textbackslash
}

\begin{document}

\begin{lstlisting}
cd ~/
time velveth run_25 25 -fmtAuto -create_binary -shortPaired -separate SRR022863_1.fastq.gz SRR022863_1.fastq.gz -shortPaired2 -separate SRR022852_1.fastq.gz SRR022852_2.fastq.gz
time velvetg run_25
\end{lstlisting}

\end{document}

enter image description here

share|improve this answer
    
Thanks for the super-fast feedback. I've posted the images as requested. –  Nathan S. Watson-Haigh Oct 11 '12 at 4:03
    
@NathanS.Watson-Haigh I've updated my answer using the desired character. –  Gonzalo Medina Oct 11 '12 at 4:06
    
Excellent! The use of \symbol{'134} instead of \textbackslash means that I also get a copy-and-pastable backslash! –  Nathan S. Watson-Haigh Oct 11 '12 at 4:25
    
A bit of testing from a LaTeX newbie and my specific use-case. i.e. also need the backslash to be copy-pastable. I prefer the use of \symbol{'134} since this works whether or not \usepackage[T1]{fontenc} is specified. Whereas, \textbackslash is only copy-pastable if \usepackage[T1]{fontenc} is specified. –  Nathan S. Watson-Haigh Oct 11 '12 at 4:44
    
@NathanS.Watson-Haigh yes; however if you change to a different monospaced font (using the beramono package, for example) then \symbol{'134} won't give you a backslash. –  Gonzalo Medina Oct 11 '12 at 5:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.