8

Is it possible to create a table in TeX and compile into PDF, which would have its column headers so, that clicking on them would sort the (numerical) table content by that column?

I understand that you can have Javascript somehow in the PDF, which could potentially allow something like this.

Has anyone done such a thing?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
morbusg
  • 1,319
  • 2
  • 11
  • 20
  • I've never seen a PDF with this. Anyway: This question is not TeX-centric; you will have better luck on [so]. – Martin Schröder Apr 01 '14 at 13:07
  • 1
    You could avoid the javascript requirement (which limits viewers) by simply pre-computing all the sorted permutations in "hidden" pages and just make the headers link to the appropriate page (something like beamer overlays) – David Carlisle Apr 01 '14 at 14:13
  • 3
    @morbusq If there is no need to do sorting on the fly, this should be doable with OCG layers. You have different versions of the table on several OCG layers. You can toggle the visibility of these layers by clicking on column headers. See: http://tex.stackexchange.com/questions/74877/parindent-in-tikz-node and the color model demo in http://mirrors.ctan.org/macros/latex/contrib/ocgx/demo-ocgx.pdf –  Apr 01 '14 at 14:19
  • Thanks @remus; that's actually an exact duplicate, but since it's on another SE site, I'm not sure how to proceed. – morbusg Apr 02 '14 at 16:41
  • @morbusg After a while i just want to inform you that i found a solution. See my answer. – Josef Apr 18 '14 at 09:40
  • This question appears to be off-topic because it belongs on TeX.stackexchange. – Dave Jarvis Apr 19 '14 at 05:26

1 Answers1

4

After my comment, that that should be possible with OCG layers, i just found out that the ocg-p package offers the ocgtabular environment, which does exactly what you want.

Example taken from the ocg-p doc:

\documentclass{article}
\usepackage[ocgtabular]{ocg-p}
\usepackage{datatool} % will be needed for this example
\usepackage{booktabs} % will be needed for this example
\DTLnewdb{sdata}
\DTLnewrow{sdata}
\DTLnewdbentry{sdata}{Firstname}{John}
\DTLnewdbentry{sdata}{Lastname}{Doe}
\DTLnewdbentry{sdata}{Grade}{5}
\DTLnewrow{sdata}
\DTLnewdbentry{sdata}{Firstname}{Paul}
\DTLnewdbentry{sdata}{Lastname}{Bauer}
\DTLnewdbentry{sdata}{Grade}{1}
\DTLnewrow{sdata}
\DTLnewdbentry{sdata}{Firstname}{Peggy}
\DTLnewdbentry{sdata}{Lastname}{Sue}
\DTLnewdbentry{sdata}{Grade}{3}
\DTLnewrow{sdata}
\DTLnewdbentry{sdata}{Firstname}{Ever}
\DTLnewdbentry{sdata}{Lastname}{Last}
\DTLnewdbentry{sdata}{Grade}{4}
\DTLnewrow{sdata}
\DTLnewdbentry{sdata}{Firstname}{Werner}
\DTLnewdbentry{sdata}{Lastname}{Moshammer}
\DTLnewdbentry{sdata}{Grade}{1}
\begin{document}
This table can be sorted by clicking on the headers:

\begin{ocgtabular}{llc}{sdata}{}
\toprule%
\bfseries \setocgtabularheader{Firstname}{First name}
& \bfseries \setocgtabularheader{Lastname}{Last name}
& \bfseries \setocgtabularheader{Grade}{Grade}
\DTLforeach{sdata}{\first=Firstname, \last=Lastname,\grade=Grade}{%
\DTLiffirstrow{\\ \midrule}{\\}
\first & \last & \grade
}
\\ \bottomrule%
\end{ocgtabular}
\end{document}

sorted by Firstname sorted by Lastname sorted by Grade

Josef
  • 232
  • 3
  • 12
  • A very "TeXnical" answer :) But there is an important footnote to this solution: **it does not "sort" the table in the PDF**. 'Sorting', in general, is possible with Javascript, but there is no way to move text around on a PDF page. This solution creates all sorted texts in advance, and in a PDF viewer it gets only shown and hidden -- it does *not* get 'sorted' live. – Jongware Apr 18 '14 at 09:45
  • 1
    Well, the question was migrated from `tex.stackexchange.com`! ;-) The question was about presenting data in a certain way. The technique behind should be negligible. In the point of view of `LaTeX` of course. Another point, the `Adobe Javascript API`and `Javascript` in gerneral is only supported by Adobe viewers AFAIK. Support of `OCG`s is much wider. – Josef Apr 18 '14 at 09:53
  • Absolutely. But I think it's important to point out that this is not a *general* way to "sort inside a PDF". – Jongware Apr 18 '14 at 09:57