Text Formattingcopy & paste source code inserted using "listings" package

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
jhcepas
Posts: 3
Joined: Wed Aug 12, 2009 3:39 pm

copy & paste source code inserted using "listings" package

Post by jhcepas »

Dear all,

I am looking for the best way to create a programing toolkit manual using latex. In this manual, I will need to insert a lot of source code snippets. I found "listings" package the best way to do it, since it allows me to include syntax highlight, line numbering, etc. However, when I produce the final PDF, the source code boxes look nice, but cannot be copied and pasted back into a text editor because it messes up all the white spaces and tab indention of my original source code (python). Essentially, many blank spaces are always pasted as a single space. (I'm using acrobat reader to explore the PDF file)

I have tried "listings" options such as "columns=fullflexible" but it does not solve the problem. Actually, this seems to be an issue affecting any text in the PDF. For example, many "interword" spaces are always copied as a single black space.

Is there any way/trick to solve this?

Thanks in advance!
Jaime.
Last edited by jhcepas on Wed Aug 12, 2009 11:10 pm, edited 2 times in total.

Recommended reading 2024:

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

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

User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

copy & paste source code inserted using "listings" package

Post by localghost »

This example works fine for me.

Code: Select all

\documentclass[11pt,a4paper,english]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[includeheadfoot,margin=2cm]{geometry}
\usepackage{babel}
\usepackage{xcolor}
\usepackage{listings}

\lstset{%
  numbers=none,
  tabsize=3,
  breaklines=true,
  basicstyle=\small\ttfamily,
  framerule=0pt,
  backgroundcolor=\color{gray!25},
  columns=fullflexible
}

\begin{document}
  \lstinputlisting{minimal.cls}
\end{document}

Best regards and welcome to the board
Thorsten
jhcepas
Posts: 3
Joined: Wed Aug 12, 2009 3:39 pm

Re: copy & paste source code inserted using "listings" package

Post by jhcepas »

Thanks Thorsten,

I have tried your approach and it doesn't fix my problem... Several blank spaces are still copied as a single one in acrobat reader, which breaks the indentation of the original source code.

it sounds like something related with how PDFs handles interword spaces. Don't know if this can be changed.

Jaime.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Re: copy & paste source code inserted using "listings" package

Post by localghost »

That makes it necessary for you to provide an example that doesn't work. My example worked in every viewer I tested (Adobe Reader, Okular, KPDF).
jhcepas
Posts: 3
Joined: Wed Aug 12, 2009 3:39 pm

copy & paste source code inserted using "listings" package

Post by jhcepas »

Ok, there it goes. :)
Try to generate the PDF, then copy and paste the source code into an editor and it should look like here.

And thanks for your time.

example.tex

Code: Select all

\documentclass[oneside,english]{book}
\renewcommand{\ttdefault}{lmtt}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{listings}
\setcounter{secnumdepth}{3}
\setcounter{tocdepth}{3}
\usepackage{babel}

\usepackage[unicode=true, 
 bookmarks=true,bookmarksnumbered=false,bookmarksopen=false,
 breaklinks=false,pdfborder={0 0 1},backref=false,colorlinks=false]
 {hyperref}
\hypersetup{pdftitle={HOLA}}
 
\makeatletter

\usepackage[T1]{fontenc}
\usepackage[includeheadfoot,margin=2cm]{geometry}
\usepackage{babel}
\usepackage{xcolor}
\usepackage{listings}

\lstset{%
  numbers=none,
  tabsize=3,
  breaklines=true,
  basicstyle=\small\ttfamily,
  framerule=0pt,
  backgroundcolor=\color{gray!25},
  columns=flexible

}

\makeatother

\begin{document}

\section*{Example}

This is an example code:

\lstinputlisting{example.py}
\end{document}
example.py

Code: Select all

#!/usr/bin/env python 

from ete2 import Tree
tree = Tree( '(A:1,(B:1,(C:1,D:1):0.5):0.5);' ) 
 
 
# Visit nodes in postorder
for n in tree.traverse("postorder"):
  if n.is_leaf():
    print "LEAF"
    print n
  elif:
    print "INTERNAL NODE"
    print n

 
# It Will visit the nodes in the following order:
#           /-A
# ---------|
#          |          /-B
#           \--------|
#                    |          /-C
#                     \--------|
#                               \-D
# --A
#           /-B
# ---------|
#          |          /-C
#           \--------|
#                     \-D
# --B
#           /-C
# ---------|
#           \-D
# --C
# --D

Post Reply