21

Take the following table:

\documentclass{article}
\begin{document}
\begin{tabular}{|c|c|}
11 & 21 \\
12 & 22 \\
\end{tabular}
\end{document}

What are the techniques to make this table text-copyable (as much as possible)?

For example, Evince sees this as text (note that order and lines are lost):

11
12
21
22

(Acrobat does slightly better job in this case but still can get confused in larger tables).

One alternative I explored is to use accsup. However I don't know how to enforce tabs (between cell) and new lines; this doesn't work:

\documentclass{article}
\usepackage{accsupp}
\begin{document}
\BeginAccSupp{ActualText=11\t 21\n 12\t 22\n} %this line produces errors
\begin{tabular}{|c|c|}
11 & 21 \\
12 & 22 \\
\end{tabular}
\EndAccSupp{}
\end{document}

What code I have to put to get this as copyable text 11 \tab 21 \n 12 \tab 22 \n? Is there other technique to help the PDF viewer at lest follow (flow) the copyable text in a more sensible way?


EDIT: I made some mild progress using accsup by defining a line-by-line alternative text:

\documentclass{article}
\usepackage{accsupp}
\begin{document}
\begin{tabular}{|c|c|}
\BeginAccSupp{ActualText=11 21} 11 & 21 \EndAccSupp{} \\
\BeginAccSupp{ActualText=11 21} 12 & 22 \EndAccSupp{} \\
\end{tabular}
\end{document}

Still I don't know how to add the tab characters but the copying has improved


EDIT 2: I can produce a tab charactar with \BeginAccSupp{method=hex,unicode,ActualText=0009} but then I can't combine it with normal text. (As in ActualText=a ?0009? b).


EDIT 3: A line-by-line variant from Heiko's answer:

\documentclass{article}
\usepackage{accsupp}
\begin{document}
\begin{tabular}{|c|c|}
\BeginAccSupp{method=plain, ActualText=11\string\t 21}
11 & 21
\EndAccSupp{}\\
\BeginAccSupp{method=plain, ActualText=12\string\t 22} 
12 & 22 
\EndAccSupp{}\\
\end{tabular}
\end{document}
  • 2
    Why not define a macro that produces the AccSupp content and the table row at the same time? Considering the input is so similar... You'll need expl3 for this, I think, because table rows are horror to create through macro's. :-D – 1010011010 Aug 4 '14 at 18:17
  • @1010011010, I don't know how to do it for variable number of cells. Originally I though on just replacing the & by \BeginAccSupp{..., ActualText=0009} & \EndAccSupp{} but still the contents is scrambled. – alfC Aug 4 '14 at 19:31
  • 1
    Have you considered to use Okular? If you copy a selection as usual (text) it works the same way as Evince, but Okular has also an interactive table selection tool, which helps to recognize table rows/cols and manually edit a grid. Just in case, your first table was copied nicely from Okular Version 0.14.3 into Excel as well as into Gnumeric spreadsheet. – g.kov Aug 5 '14 at 10:29
12
+250

Method pdfstringdef

If hyperref is loaded, \pdfstringdef is available and can be used for package accsupp via method=pdfstringdef:

\documentclass{article}
\usepackage[pdfencoding=auto]{hyperref}
\usepackage{accsupp}
\begin{document}
\BeginAccSupp{
  method=pdfstringdef,
  ActualText=11\unichar{"0009}21\unichar{"000A}12\unichar{"0009}22\unichar{"000A},
}
\begin{tabular}{|c|c|}
11 & 21 \\
12 & 22 \\
\end{tabular}
\EndAccSupp{}
\end{document}

\textHT can be used instead of \unichar{"000A} and \textLF instead of \unichar{"000A}.

Method pdfstringdef is the most comfortable method, because it supports a wide range of TeX code. \pdfstringdef is the very same macro, which is used for the bookmarks.

Method plain

The most riskiest method is plain, here the string is given, as it should appear in the PDF file after expansion. Requirement is a good knowledge about string syntax of PDF or PostScript.

\t and \n can usually not be used directly, because they do not expand to themselves, \t is an accent command and \n is undefined usually. Here \string (or \detokenize) can be used to convert \t and \n to two character tokens \ and t or \ and \n:

\documentclass{article}
\usepackage{accsupp}
\begin{document}
\BeginAccSupp{  
  method=plain,
  ActualText=11\string\t 21\string\n 12\string\t 22\string\n,
}
\begin{tabular}{|c|c|}
11 & 21 \\
12 & 22 \\
\end{tabular}
\EndAccSupp{}
\end{document}

Method escape

The risk of plain is, that backslashes at the wrong places or unmatched parentheses can lead to a broken/invalid PDF file. Method escape avoids this by escaping the problematic characters. Therefore \t and \n can't be used the same way as with method plain, because the backslash would be escaped itself. Instead the macros should expand to the character tokens:

\documentclass{article}

% define \HT as horizontal tabular
\begingroup
  \lccode`\0=9\relax
\lowercase{\endgroup
  \def\HT{0}%
}

% define \LF as line feed
\begingroup
  \lccode`\0=10\relax
\lowercase{\endgroup
  \def\LF{0}%
}

\usepackage{accsupp}
\begin{document}
\BeginAccSupp{  
  method=escape,
  ActualText=11\HT 21\LF 12\HT 22\LF,
}
\begin{tabular}{|c|c|}
11 & 21 \\   
12 & 22 \\   
\end{tabular} 
\EndAccSupp{} 
\end{document}
  • Why is plain risky? what can go wrong? It seems the most straight forward. – alfC Aug 12 '14 at 0:27
  • @alfC Try method=plain,ActualText=( with different PDF viewers. For example, xpdf and ghostscript complain with an error message ("Syntax error: Unterminated string ...", "Error reading a content stream. The page may be incomplete ..."). – Heiko Oberdiek Aug 12 '14 at 5:02
  • My xpdf (3.04 Linux) and gv (3.7.4) don't give any error or warning related to accsup. – alfC Aug 12 '14 at 17:37
  • Thanks, it works. I will probably use a line-by-line version of it (see my Edit). – alfC Aug 12 '14 at 17:47
5

I'm not an expert for PDF. Your belief, that the PDF contains something like a table / tabular, seems questionable. I don't think there is a tabular in the PDF, just rules and glyphs.

First step: you copy the content of the PDF. What do you copy? Just the content, whatever it is in the PDF: glyphs, graphics. Some software is able to enrich the content, which will be transfered to the clipboard, with information. My old version of Acrobat Pro e.g. offers to copy text "as a table". But this depends on the viewer of the PDF. To my knowledge there is no way to label at table in the PDF as "TABLE".

Now, you paste the content of the clipboard into another software. Maybe this software analyses the content of the clipboard and reckognises the outlines of a table.

In some cases it seems an improvement to use the command \pdfinterwordspaceon, but my own tests showed poor results.

So: If you wish to share tables, don't print them into a PDF.

  • 1
    I agree that text is not table. At worst, just text can be copied from a PDF. However, tables can be "simulated" by text, for example by using separators such as commas and end-of-lines. Most "table" programs (like Excel or OpenOffice) detect tabulation and paste the text as a table. This is mostly for convenience, for example to paste more or less well to an excel sheet or to a text editor/email, in other words to be able to copy part (lines) or the whole table. Thanks for the tip on \pdfinterwordspaceon, it can be useful to improve "copyability". – alfC Aug 4 '14 at 17:52
  • This recent answer of mine echoes your remarks here. – jub0bs Aug 9 '14 at 12:04

Your Answer

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

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