53

I was just wonder if it would be possible to have a "Copy to Clipboard Button" next to each listing which would just copy the verbatim content of the lstlistings environment to the clipboard.

An acroread-only solution would suffice for my purposes.

9
  • This would be amazing, and I'm sure it can be done, too, using some pdfTeX-foo. Commented Jun 5, 2013 at 19:19
  • 2
    What I have done in the past is to make the line numbers not selectable in pdf, so that you can select/cut/paste the code only. if this is an acceptable solution I can provide an answer
    – ArTourter
    Commented Jun 5, 2013 at 20:05
  • 3
    @ArTourter That solution is here already. It's neat, but still the user has to ensure that he selected everything and you know how it goes: One false move and your three pages from where you started...
    – arney
    Commented Jun 6, 2013 at 6:29
  • Wonderful. I was going to ask this today and see that you asked same today! Commented Jun 6, 2013 at 7:29
  • 7
    Another way of doing it is using the attachfile package. in that case the user would be able to save the source to file as opposed to copy it to the clipboard. Note that the text to be attached has to come from a separate file though.
    – ArTourter
    Commented Jun 6, 2013 at 12:31

1 Answer 1

27

Ok, here are two approaches, both of which not perfect, both of which not 100% what was requested. But maybe together we can optimise them :-)

\documentclass{article}

\usepackage{listings}
\usepackage{attachfile}
\usepackage{accsupp}
\usepackage{verbatim}
\usepackage{color}
\usepackage[misc]{ifsym}

\definecolor{lstbgcolor}{rgb}{0.9,0.9,0.9} 

\makeatletter
\lst@RequireAspects{writefile}

% Use \attachfile command to add listing content as paperclip.
\lstnewenvironment{attachedlisting}{%
  \lst@BeginAlsoWriteFile{\jobname.lsttmp}%
}
{%
  \lst@EndWriteFile
  \marginpar{\attachfile[appearance=false,icon=Paperclip,mimetype=text/tex]{\jobname.lsttmp}}
}

% Use accsupp package to add listing content as copyable text.
\lstnewenvironment{copyablelisting}{%
  \lst@BeginAlsoWriteFile{\jobname.lsttmp}%
}
{%
  \lst@EndWriteFile
  \let\verbatim@processline\add@lstline
  \global\let\lstfile\empty
  \verbatiminput{\jobname.lsttmp}%
  \marginpar{(\BeginAccSupp{method=escape,ActualText={\lstfile}}\PaperPortrait\EndAccSupp{})}
}
\def\add@lstline
{\xdef\lstfile{\unexpanded\expandafter{\lstfile}\the\verbatim@line\string^^J}}
\makeatother

\begin{document}

  \lstset{breakatwhitespace=true,breaklines=true,language=[LaTeX]TeX,basicstyle=\small\ttfamily,flexiblecolumns,backgroundcolor=\color{lstbgcolor}}

  \begin{attachedlisting}
    \documentclass{article}
    \title{Cartesian closed categories and the price of eggs}
    \author{Jane Doe}
    \date{September 1994}
    \begin{document}
      \maketitle
      Hello world!
    \end{document}
  \end{attachedlisting}

  \begin{copyablelisting}
    \documentclass{article}
    \title{Cartesian closed categories and the price of eggs}
    \author{Jane Doe}
    \date{September 1994}
    \begin{document}
      \maketitle
      Hello world!
    \end{document}
  \end{copyablelisting}

\end{document}

enter image description here

The first environment, attachedlisting displays a paper clip which allows to open the listing contents in an editor (or save to file).

The second environment, copyablelisting displays a symbol which, when copied, will paste to the listing contents.

Open Issues

  1. Placement of symbols: As the listings environment needs to be ended before the file is completely written, the placement of the "target symbol" is a problem. To get it to the top, probably two runs would be neccessary.
  2. Copy Symbol: I wasn't able to make a single character which will paste to the listing content. Somehow this trick won't work if the "copy symbol" is not surrounded by at least one "normal" character on both sides and those are also copied. Hence, at the moment, you'll get two superfluous characters before and after the listing text. I also wasn't able to make them "invisible" somehow.

So this is as good as I can make it for the time being. I'm open to suggestions of course.

2
  • Well, kudos to you for digging deep
    – arney
    Commented Nov 26, 2013 at 23:41
  • 2
    What about if you already have the file, include it using \lstinputlisting but want to include the possibility for coping the text like the copyablelisting example, how can I do that? Commented Aug 31, 2017 at 10:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .