18

I skimmed the abstracts of TUG 2014 and stumbled over "Fake spaces" with pdfTeX -- the best of both worlds by Ross Moore. The abstract says:

With the 2014 version of TeX Live, new primitives are included within pdfTeX that allow a pdffakespace to be inserted into the PDF content stream, occurring between words and at the end of lines.

In particular, this is intriguing:

Also to be shown is how a fake space allows extra material, such as the LaTeX source of inline or displayed mathematics, to be included invisibly within the PDF. With a Select/Copy/Paste of the mathematical expression, this included source coding comes along with the pasted text.

Unfortunately, there are no slides available, and the proceedings do not seem to contain anything about this (not that I could access them, as a non-member).

What are current applications of these fake spaces? In particular, has copy-paste of mathematics been implemented so that it may be used without disadvantages?

23

In reference to your point, In particular, has copy-paste of mathematics been implemented so that it may be used without disadvantages

I don't know the particulars of what you cite, but the accsupp package allows different things to be displayed in a PDF versus what shows up in a copy paste.

REVISED SOLUTION (using Raphael's suggestion of \detokenize)

It would seem that \detokenize alleviates the need for separate arguments for the typeset and actual-pdf text, which will greatly streamline the use of this approach. I extend my thanks to him.

I also show how this even works for \displaystyle math.

\documentclass{article}
\usepackage{accsupp}
\newcommand\copypaste[1]{%
    \BeginAccSupp{method=escape,ActualText={\detokenize{#1}}}%
      #1%
    \EndAccSupp{}%
}
\begin{document}
What I have is \copypaste{$x^2 + y^2 = z^2$} and \copypaste{$\frac{1}{2}$}.  
Try to copy/paste me.
\copypaste{\[
y = \frac{\cos{x}}{1+\cos{x}}
\]}

Here it is with no copy/paste tuning: $x^2 + y^2 = z^2$ and $\frac{1}{2}$.
\[
y = \frac{\cos{x}}{1+\cos{x}}
\]\end{document}

Here is the PDF's appearance:

enter image description here

Now, when I go into Adobe Reader on the produced PDF file and hit ctl-A ctl-C to copy the whole document, the paste into a text file will appear as:

What I have is $x^2 + y^2 = z^2$ and $\frac {1}{2}$ . Try to copy/paste me.
\[ y = \frac {\cos {x}}{1+\cos {x}} \]
Here it is with no copy/paste tuning: x2 + y2 = z2 and 1
2 .
y =
cos x
1 + cos x
1

ORIGINAL EDITED SOLUTION to allow optional argument with different "ActualText" content, for example, to use \noexpand for certain math arguments.

\documentclass{article}
\usepackage{accsupp}
\newcommand\copypaste[2][\relax]{%
  \ifx\relax#1%
    \BeginAccSupp{method=escape,ActualText={#2}}%
      #2%
    \EndAccSupp{}%
  \else%
    \BeginAccSupp{method=escape,ActualText={#1}}%
      #2%
    \EndAccSupp{}%
  \fi%
}
\begin{document}
What I have is \copypaste{$x^2 + y^2 = z^2$} and 
  \copypaste[$\noexpand\frac{1}{2}$]{$\frac{1}{2}$}.  
Try to copy/paste me.

Here it is with no copy/paste tuning: $x^2 + y^2 = z^2$ and $\frac{1}{2}$.

\end{document}
| improve this answer | |
  • Cool! I guess a globally activated solution (i.e. copy-paste all math environments as their source) would require \noexpand on all macros, otherwise we'd open a can of worms. Ah, fwiwi: works with Evince (3.10.3) as well. – Raphael Mar 18 '15 at 15:16
  • 1
    Where does the spurious space after \frac come from, by the way? – Raphael Mar 18 '15 at 15:36
  • 1
    @Raphael While the space is spurious as compared with what I wrote, I know \detokenize introduces the same kind of "spurious" spaces. Note that they are spurious in the visual sense only. Functionally, the space is irrelevant. – Steven B. Segletes Mar 19 '15 at 1:12
  • 1
    @Raphael The \detokenize did the trick, alleviating the need for an alternate argument. Thank you so much! – Steven B. Segletes Mar 19 '15 at 1:25
  • 1
    @Raphael I am no expert on either accsupp or PDF format in general. But, just playing around a bit further, I discovered that accsupp does not function across paragraph boundaries, so that \copypaste{a\par b} fails. Of course, that is not math, and so this wouldn't usually be a problem unless you were trying to be lazy and apply \copypaste across large stretches of code. – Steven B. Segletes Mar 19 '15 at 9:58

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.