20

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?

Raphael
  • 5,983

1 Answers1

27

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}
  • 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
  • I just remembered this older answer to a question of mine. Maybe \detokenize can be useful here, too, in that we don't have to worry about expansion. Then, the "top-level" LaTeX code ends up being copy-pasted, which may or may not be helpful for people other than the original author. An "expand all macros defined on document level, but not others" may be what you really want. – Raphael Mar 18 '15 at 15:40
  • 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 01: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 01:25
  • Cool, glad I knew something for a change! :) Just one question: would we have to expect any problems should we "enable" this for all math environments by default? (Arguaby, this should be what happens when copy-pasting, always.) – Raphael Mar 19 '15 at 06:57
  • 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 09:58
  • +1: Saw this by reading https://tex.stackexchange.com/questions/426826: Great idea! – Dr. Manuel Kuehner Apr 16 '18 at 18:50
  • @StevenB.Segletes: It's great and how to with UTF characters (like {Q}_{α})? – Balaji Sep 24 '21 at 11:48
  • @Balaji Try detokenizing what goes into the "ActualText", as in \newcommand\copypaste[2][\relax]{% \ifx\relax#1% \BeginAccSupp{method=escape,ActualText={\detokenize{#2}}}% #2% \EndAccSupp{}% \else% \BeginAccSupp{method=escape,ActualText={\detokenize{#1}}}% #2% \EndAccSupp{}% \fi% } and compile in lualatex or xelatex. Not sure if this will work for your use case, though. – Steven B. Segletes Sep 24 '21 at 16:41