1

Is it possible to switch between a list and an equivalent tree (both already coded) with a clickable link with latex i.e. toggle between them ?

Does LaTeX allow to typeset dynamically with a clickable link ?

Edit 1: The below code shows a list and its equivalent tree.

\documentclass[11pt]{article}
\usepackage{tikz}
\usepackage{verbatim}
\usetikzlibrary{trees}
\begin{document}
\tikzstyle{every node}=[draw=black,thick,anchor=west]
%\tikzstyle{selected}=[draw=red,fill=red!30]
%\tikzstyle{optional}=[dashed,fill=gray!50]
\begin{tikzpicture}[%
  grow via three points={one child at (0.5,-0.7) and
  two children at (0.5,-0.7) and (0.5,-1.4)},
  edge from parent path={(\tikzparentnode.south) |- (\tikzchildnode.west)}]
  \node {texmf}
    child { node {doc}}     
    child { node {fonts}}
    child { node {source}}
    child { node  {tex}
      child { node {generic}}
      child { node  {latex}}
      child { node {plain}}
    }
    child [missing] {}              
    child [missing] {}              
    child [missing] {}              
    child { node {texdoc}};
\end{tikzpicture}


\begin{itemize}
\item texmf
\begin{itemize}
\item doc
\item fonts
\item source
\item tex
\begin{itemize}
\item generic
\item latex
\item plain
\end{itemize}
\item texdoc
\end{itemize}
\end{itemize}
\end{document}

Output:

Output

What I want is to have a clickable link or button on the page that can toggle between these two (show one at a time). Is this possible in LaTeX?

  • Question 1: yes. Question 2: no. – cfr Jul 15 '18 at 18:01
  • @cfr can you provide a detailed answer for question 1. – paulplusx Jul 15 '18 at 18:04
  • 1
    Can you provide a Minimal Working Example i.e. code for a small document with the content you describe? It is pretty easy in Beamer, for example. – cfr Jul 15 '18 at 18:08
  • Can you please provide a detailed minimal working example that codes your list and equivalent tree. It is much easier to help you if we have some working code to start from. Currently we have to guess what you mean by "a list and an equivalent tree", so people are unlikely to offer a solution because it is very likely that they would be wasting their time because these terms are open to misinterpretation. – Andrew Jul 16 '18 at 4:40
  • 1
    Does the existing answer do what you need? If not, can you specify your constraints? For example, does it need to be usable in any reasonable PDF viewer, so that Adobe-specific stuff must be avoided? That will limit the options, so it is important to know what your constraints are. – cfr Jul 16 '18 at 23:37
5

One can realize such dynamic with Optional Content Groups (OCGs or layers):

\documentclass{article}
\usepackage{tikz}
\usepackage{verbatim}
\usetikzlibrary{trees}
\usepackage{eforms}
\usepackage[uselayers]{aeb_pro}

\begin{document}

\def\myTree{%
\tikzstyle{every node}=[draw=black,thick,anchor=west]
%\tikzstyle{selected}=[draw=red,fill=red!30]
%\tikzstyle{optional}=[dashed,fill=gray!50]
\begin{tikzpicture}[%
  grow via three points={one child at (0.5,-0.7) and
  two children at (0.5,-0.7) and (0.5,-1.4)},
  edge from parent path={(\tikzparentnode.south) |- (\tikzchildnode.west)}]
  \node {texmf}
    child { node {doc}}
    child { node {fonts}}
    child { node {source}}
    child { node  {tex}
      child { node {generic}}
      child { node  {latex}}
      child { node {plain}}
    }
    child [missing] {}
    child [missing] {}
    child [missing] {}
    child { node {texdoc}};
\end{tikzpicture}
}

\def\myItem{%
\begin{itemize}
\item texmf
\begin{itemize}
\item doc
\item fonts
\item source
\item tex
\begin{itemize}
\item generic
\item latex
\item plain
\end{itemize}
\item texdoc
\end{itemize}
\end{itemize}
}

\pushButton[\CA{Press me to toggle}\autoCenter{n}%
\A{\JS{%
toggleSetThisLayer("myToggleA");
toggleSetThisLayer("myToggleB");
}}]{myBtn}{5cm}{12bp}

\bigskip

\begin{tabular}{ll}
\xBld[initState=on]{myToggleA}
\parbox{5cm}{\myTree}
\eBld
&
\xBld[initState=off]{myToggleB}
\parbox{5cm}{\myItem}
\eBld
\end{tabular}
\end{document}

enter image description here

The technique is to set one OCG to the initial state to "off" and the other one to "on". With the button you then can toggle between them.

enter image description here

Note: The workflow I was using is: latex -> dvips -> distiller -> PDF

Due to implemented JavaScript within the resulting PDF, a JS based PDF-Viewer is needed. Adobe Reader does the best job.

The packages aeb_pro and eforms are found on CTAN and not part of the TL distribution - however part of the TL contribution.

EDIT:

The aeb_pro package ships an aeb_pro.js file. This file goes to the JavaScripts folder of your Acrobat app.

As I am using Acrobat Pro DC, the JS folder is located at:

C:\Users\<username>\AppData\Roaming\Adobe\Acrobat\Privileged\2015\JavaScripts

If the Privileged folder is not existing, generate it with the subfolders 2015\JavaScripts

For older versions the location of the JavaScripts folder my be different.

Workflow: with command line:

latex filename.tex
dvips filename.dvi

Then a filename.ps in the working folder is generated.

Open this *.ps file with the Distiller and a PDF will be produced.

This process can surely be automized within your favorite editor.

Note: Distiller always calls for ABSOLUTE paths and not relative.

Hope this helps.

  • Can you let me know how to use- latex -> dvips -> distiller -> PDF , I am having trouble configuring it. – paulplusx Jul 17 '18 at 2:29
  • @paulplusx: I edited my answer with some hints - hope this will help you – user151328 Jul 17 '18 at 7:13
  • @paulplusx: JS can make a PDF very dynamic in many more cases than only toggle OCGs, however there is a restriction to the PDF-Viewers as mentioned in my answer. – user151328 Jul 17 '18 at 7:25
  • I was actually thinking whether its possible to integrate JS and TeX before you gave your answer and then you actually gave me the perfect one :). I was looking for that dynamic thing. – paulplusx Jul 17 '18 at 9:31
  • 1
    @JürgenG : simpler and more portable with pkg ocgx2 – AlexG Jul 17 '18 at 13:43
3

Much simpler and more portable solution with package ocgx2:

  1. any engine: [(pdf|lua|xe)]latex --> Distiller not required, ps2pdf sufficient for latex+dvips route
  2. no embedded JavaScript --> better portability (e. g. Evince PDF viewer)
  3. all necessary packages in TeXLive (eforms, aeb_pro not required)

\documentclass{article}
\usepackage{tikz}
\usepackage{verbatim}
\usetikzlibrary{trees}

\usepackage{ocgx2}

\begin{document}

\newcommand\myTree{%
  \tikzstyle{every node}=[draw=black,thick,anchor=west]
  %\tikzstyle{selected}=[draw=red,fill=red!30]
  %\tikzstyle{optional}=[dashed,fill=gray!50]
  \begin{tikzpicture}[%
    grow via three points={one child at (0.5,-0.7) and
    two children at (0.5,-0.7) and (0.5,-1.4)},
    edge from parent path={(\tikzparentnode.south) |- (\tikzchildnode.west)}]
    \node {texmf}
      child { node {doc}}
      child { node {fonts}}
      child { node {source}}
      child { node  {tex}
        child { node {generic}}
        child { node  {latex}}
        child { node {plain}}
      }
      child [missing] {}
      child [missing] {}
      child [missing] {}
      child { node {texdoc}};
  \end{tikzpicture}
}

\newcommand\myItem{%
  \begin{itemize}
  \item texmf
  \begin{itemize}
  \item doc
  \item fonts
  \item source
  \item tex
  \begin{itemize}
  \item generic
  \item latex
  \item plain
  \end{itemize}
  \item texdoc
  \end{itemize}
  \end{itemize}
}

\switchocg{tree list}{\fbox{Press me to toggle}}\\[2ex]

\begin{tabular}{ll}
\begin{ocg}{Tree}{tree}{on}
  \parbox{5cm}{\myTree}
\end{ocg}
&
\begin{ocg}{List}{list}{off}
  \parbox{5cm}{\myItem}
\end{ocg}
\end{tabular}

\end{document}

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.