3

Context: I want to insert the BibTeX entry of my document at the beginning thereof. I thus want this code snippet meet two criteria:

  1. Be easily copy-paste-able (code seen = code copy-pasted)
  2. Looks good (enable 'structuring' of the code snippet with both tabs and spaces).

What? Using tabs and spaces for formatting code??! What a sacrilege!

Don't you say? Here is an example of the code snippet to make things more explicit:

@article{Turing1950,
⇥Title␣␣␣=␣{Computing machinery and intelligence},
⇥Author␣␣=␣{Turing, Alan~M.},
⇥Journal␣=␣{Mind},
⇥Year␣␣␣␣=␣{1950},
⇥Pages␣␣␣=␣{433--460},
⇥Volume␣␣=␣{59},
}

I here distinguish syntactical indentation done with Tab (and here represented with ), from indentation for outlook only (i.e. in order to make all = be aligned) done with Space and here represented with .

Issue: I use a simple lstlisting environment. The result looks great but it got unwanted line breaks when I copy-paste it [see MWE 1].

I also tried the solution proposed by Martin Monperrus on his website. Copy-paste works pretty well (no unwanted line-break, yet equal signs are not aligned), but the look does not. [see MWE 2].

I have also tested the various possibilities for listings' column setting but none is fully satisfying (see below). verbatim environment doesn't work neither (for both look and copy-paste).

Question: How to display a copy-pastable and well structured BibTeX key in my document?


MWE 1

  • code:

    \documentclass{scrbook}
        \usepackage[utf8]{inputenc}
        \usepackage[T1]{fontenc}
        \usepackage{textcomp}
        \usepackage{listings}
            \lstset{%
                basicstyle=\ttfamily,
                tabsize=2,
                breaklines=true,
                breakindent=1em,
                upquote=true,
                columns=[c]fixed,
                literate={*}{{\char42}}1
                         {-}{{\char45}}1
            }
    \begin{document} 
    \begin{lstlisting}
    @article{Turing1950,
        Title   = {Computing machinery and intelligence},
        Author  = {Turing, Alan~M.},
        Journal = {Mind},
        Year    = {1950},
        Pages   = {433--460},
        Volume  = {59},
    }
    \end{lstlisting}
    \end{document}
    
  • Display:
    enter image description here

  • Copy-paste:

    @article { Turing1950 ,
    Title
    = { Computing machinery and intelligence } ,
    Author = { Turing , Alan ~ M .} ,
    Journal = { Mind } ,
    Year
    = {1950} ,
    Pages
    = {433 - - 460} ,
    Volume = {59} ,
    }
    

MWE 2

  • code:

    \documentclass{article}
    \usepackage[T1]{fontenc}
    \usepackage{textcomp}
    \usepackage{listings}
    \lstset{ 
    %basicstyle=\ttfamily,% <----- make things even worse
    upquote=true,
    columns=fullflexible,
    literate={*}{{\char42}}1
             {-}{{\char45}}1
             {\ }{{\copyablespace}}1
    }
    
    \usepackage[space=true]{accsupp}
    % requires the latest version of package accsupp
    \newcommand{\copyablespace}{
        \BeginAccSupp{method=hex,unicode,ActualText=00A0}
    \ %
        \EndAccSupp{}
    }
    
    \begin{document} 
    \begin{lstlisting}
    @article{Turing1950,
        Title   = {Computing machinery and intelligence},
        Author  = {Turing, Alan~M.},
        Journal = {Mind},
        Year    = {1950},
        Pages   = {433--460},
        Volume  = {59},
    }
    \end{lstlisting}
    \end{document}
    
  • Display:
    enter image description here

  • Copy-paste:

    @article{Turing1950,
    Title       =   {Computing   machinery   and   intelligence},
    Author     =   {Turing,   Alan~M.},
    Journal   =   {Mind},
    Year         =   {1950},
    Pages       =   {433--460},
    Volume     =   {59},
    }
    

columns=fixed copy-paste = ✘ | display = ✔
columns=flexible copy-paste = ✘ | display = ✘ (year only messes up in copy-paste)
columns=spaceflexible copy-paste = ✘ | display = ✘ (year only messes up in copy-paste)
columns=fullflexible copy-paste = ✘ | display = ✘

  • 1
    Please clarify what you mean by "the BibTeX of my document". (The usual meaning of "BibTeX" is that it's an executable program.) E.g., are you looking to create a document that typesets the contents of a bib file in some special verbatim mode, with distinctions made between tab characters ("soft spaces") and hard spaces? – Mico May 6 '17 at 6:23
  • @Mico : I just miss-typed "BibTeX entry", my bad. On the back of the title page of my thesis, I want to insert the copy-pastable BibTeX reference of the document. – ebosi May 6 '17 at 13:24
  • @ebo -- to make the question a bit less confusing, please edit the title as well to say "entry" and not "key". that said, i don't think i know any way of presenting this information in the visual format you want unless it's treated as verbatim. and even then, i don't know that copy-paste will retain the spacing. – barbara beeton May 6 '17 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.