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.

Is it possible to make source code with minted copyable?

Here is a minimal example how I include source code (completely, with minimal java code, on GitHub):

\documentclass{beamer}
\usepackage[utf8]{inputenc} % this is needed for german umlauts
\usepackage[ngerman]{babel} % this is needed for german umlauts
\usepackage[T1]{fontenc}    % this is needed for correct output of umlauts in pdf
\usepackage{minted}         % needed for the inclusion of source code

\begin{document}
    \section{Section}
    \subsection{MySubSection}
    \begin{frame}{Blubtitle}
        \inputminted[linenos=true, numbersep=5pt, tabsize=4, fontsize=\small]{java}{IataCode.java}
    \end{frame}
\end{document}

When I copy the results, I get this:

public class IataCode {
public static void printIATACodes(String[] myArray) {
for (int i = 0; i < myArray.length; i++) {
System.out.println(myArray[i]);
}
}
7
public static void main(String[] args) {
String[] iataCodes = new String[4];
// Flughafen München
iataCodes[0] = "MUC";
// Flughafen Berlin Brandenburg
iataCodes[1] = "BER";
// Flughafen Augsburg
iataCodes[2] = "AGB";
printIATACodes(iataCodes);
}
8
9
10
11
12
13
14
15
16
17
18
}

But I would like to get this:

public class IataCode {
    public static void printIATACodes(String[] myArray) {
        for (int i = 0; i < myArray.length; i++) {
            System.out.println(myArray[i]);
        }
    }

    public static void main(String[] args) {
        String[] iataCodes = new String[4];
        // Flughafen München
        iataCodes[0] = "MUC";
        // Flughafen Berlin Brandenburg
        iataCodes[1] = "BER";
        // Flughafen Augsburg
        iataCodes[2] = "AGB";
        printIATACodes(iataCodes);
    }
}

So, I basicly want to know if I can achieve this (see compiled PDF and minimal source code example to try it yourself) for minted, too.

share|improve this question
    
I’d say: forget it. Copying from PDFs simply doesn’t work. So far I haven’t seen a satisfactory solution for this (this applies in general, not just to minted, and not even just to preformatted code). –  Konrad Rudolph Nov 18 '12 at 14:01
    
An aside about your comments: babel isn't really needed for German umlauts, but to use German hyphenation patterns instead of the default English ones. –  doncherry Nov 18 '12 at 18:20

1 Answer 1

up vote 9 down vote accepted

I can help with the line numbers, but not with the indenting. I am not sure it is possible to specify in the PDF (at least in a viewer-independent way) that the indentation should be copied too. You might consider instead attaching the source code file to the PDF (see eg the attachfile, attachfile2 and embedfile packages for that).

The line numbers are produced by the fancyvrb package rather than by minted itself, so an accsupp patch to fancyvrb is needed, similar to the one for listings in the linked answer. There is no style key like there is for the line numbers in listings, but the fancyvrb manual suggests modifying the \theFancyVerbLine macro. This is the approach I have taken in the following (I have just overwritten the existing definition instead of patching):

\documentclass{beamer}
\usepackage[utf8]{inputenc} % this is needed for german umlauts
\usepackage[ngerman]{babel} % this is needed for german umlauts
\usepackage[T1]{fontenc}    % this is needed for correct output of umlauts in pdf
\usepackage{minted}         % needed for the inclusion of source code

% from http://tex.stackexchange.com/questions/57151/how-do-i-prevent-conflicts-between-accsupp-and-hyperref
\usepackage{accsupp}
\newcommand\emptyaccsupp[1]{\BeginAccSupp{ActualText={}}#1\EndAccSupp{}}


%default definition is: \def\theFancyVerbLine{\rmfamily\tiny\arabic{FancyVerbLine}}
\let\theHFancyVerbLine\theFancyVerbLine% don't apply our patch to hyperref's version
\def\theFancyVerbLine{\rmfamily\tiny\emptyaccsupp{\arabic{FancyVerbLine}}}

\begin{document}
    \section{Section}
    \subsection{MySubSection}
    \begin{frame}{Blubtitle}
        \inputminted[linenos=true, numbersep=5pt, tabsize=4, fontsize=\small]{java}{IataCode.java}
    \end{frame}
\end{document}
share|improve this answer
    
I have tried attachfile (and embedfile with navigator), but it seems not to work: github.com/MartinThoma/LaTeX-examples/tree/master/documents/… Am I using it wrong? Is there any minimal example how to use attachfile? –  moose Dec 19 '12 at 9:10
1  
@moose: Your example seems to work. It generates an icon in the document at the point of the \attachfile (which one can double-click to open the attachment), and an entry in the attachments sidebar (where you can do the same). Perhaps it is a problem with your viewer? –  cyberSingularity Dec 19 '12 at 9:23
    
Yes, it doesn't work with evince and Google Chromes internal PDF-viewer, but it does work with Adobe Reader. Thanks :-) –  moose Dec 19 '12 at 9:26

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.