15

This question already has an answer here:

I'm trying to write code in a LaTeX document that can be copy/pasted from an outputted pdf using Adobe Reader and that also word wraps, rather than breach the right margin.

I've found that in verbatim mode I can copy/paste, but there isn't an option to enable word wrapping. In lstlistings I can enable word wrapping, but can't figure out how to make the text copy/paste properly. I get extra spaces when trying to copy/paste from Adobe Reader X. From searching online, it seems like I just need to choose what's inside lstset{...} correctly, but can't figure it out.

See the following example and the results after copy/pasting from the compiled document (notice the additional spacing)

\documentclass[letterpaper]{article}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{fancyhdr}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage[lmargin=2.5cm,rmargin=2.5cm,tmargin=1.5cm,bmargin=3.5cm]{geometry}
\usepackage{listings}  % Environment meant for source code (useful for word-wrapping verbatim text)
\lstset{language=[LaTeX]TeX,breaklines=true} % Word wrap within listings environment

\begin{document}
\lstset{basicstyle = \verbatim@font}

\begin{lstlisting}
//Define Variables
Test1_up = [XZ:This (Index)][1];
Test2_down = [XZ:This Index][1];

//Specify Parameter Values
beta1 = .2238202445016966;
beta2 = .6676275732948469;
\end{lstlisting}

\begin{verbatim}
//Define Variables
Test1_up = [XZ:This (Index)][1];
Test2_down = [XZ:This Index][1];

//Specify Parameter Values
beta1 = .2238202445016966;
beta2 = .6676275732948469;
\end{verbatim}

\end{document}

And the output after copy/pasting:

// Define Variables
Test 1_ up = [XZ: This ( Index ) ][1];
Test 2_ down = [XZ: This Index ][1];
// Specify Parameter Values
beta 1 = .2238202445016966;
beta 2 = .6676275732948469;
//Define Variables
Test1_up = [XZ:This (Index)][1];
Test2_down = [XZ:This Index][1];
//Specify Parameter Values
beta1 = .2238202445016966;
beta2 = .6676275732948469;

marked as duplicate by clemens, Heiko Oberdiek, Nils L, Kurt, egreg Jun 15 '13 at 21:26

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

15

The problem with spaces in the pasted text is in the fact that listings adds spaces between letters, unless the option columns=fullflexible is used.

\documentclass[letterpaper]{article}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{fancyhdr}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage[lmargin=2.5cm,rmargin=2.5cm,tmargin=1.5cm,bmargin=3.5cm]{geometry}
\usepackage{listings}  % Environment meant for source code (useful for word-wrapping verbatim text)
\lstset{language=[LaTeX]TeX,breaklines=true} % Word wrap within listings environment

\begin{document}
\lstset{basicstyle = \ttfamily,columns=fullflexible}

\begin{lstlisting}
//Define Variables
Test1_up = [XZ:This (Index)][1];
Test2_down = [XZ:This Index][1];

//Specify Parameter Values
beta1 = .2238202445016966;
beta2 = .6676275732948469;
\end{lstlisting}

\end{document}

enter image description here

Here's what I get from copy-pasting:

//Define Variables
Test1_up = [XZ:This (Index)][1];
Test2_down = [XZ:This Index][1];
//Specify Parameter Values
beta1 = .2238202445016966;
beta2 = .6676275732948469;

Note that basicstyle=\verbatim@font will throw an error.

  • Thanks @egreg - that works. (Though I didn't get an error from basicstyle=\verbatim@font) – user32268 Jun 17 '13 at 13:58
  • @user32268 Yes, @-commands are allowed in \lstset (which I don't agree with, by the way). – egreg Jun 17 '13 at 14:10
  • columns=fullflexible eliminates the spaces from the listing completely, not just removes the "extra" artificaial spaces from the rendered output. That corrupts the listing's formatting. – ajeh Dec 2 '14 at 18:13
  • 2
    doesn’t work for me, unfortunately, the spaces are still there – mirabilos Aug 6 '15 at 12:16
  • 1
    @Daniel It's viewer dependent. – egreg Jul 21 '16 at 20:43

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