# How to unfold rows in a table

I am trying to unfold rows in a table, just like text can be created to be shown and hidden using ocgx package. The problem here is that if I enter that line (of creating corresponding row in table) in ocgx, it shows error (obviously, since the full tabular environment is not in the ocg environment, just the line with alignment characters).

I want an environment that can compile or not compile the code written in it at my wish (toggled by pressing a button). Is that possible?

MWE

\documentclass{article}
\usepackage{ocgx}
\begin{document}
\begin{tabular}{|c|c|} \hline
Hi&Hello\\ \hline
\begin{ocg}{OCG 1}{ocg1}{1}
Hi&hello    \\ \hline
\end{ocg}
\end{tabular}

\switchocg{ocg1}{Button.}
\end{document}


EDIT Another MWE based on answer of AlexG.

\usepackage{ocgx2}
\usepackage{hyperref}

\begin{document}
\noindent%
\begin{tabular}{|c|c|} \hline
This is &row 1\\ \hline
\end{tabular}\\[-\dimexpr\lineskip+\fboxrule\relax]
\begin{ocg}{OCG 1}{ocg1}{on}
\begin{tabular}{|c|c|}\hline
This is &row 2\\ \hline
\end{tabular}\\[-\dimexpr\lineskip+\fboxrule\relax]
\end{ocg}
\begin{tabular}{|c|c|} \hline
This is &row 3\\ \hline
\end{tabular}
\noindent\switchocg{ocg1}{Button.}
\end{document}


Note that on pressing the button, second row becomes invisible, but it is still there, that I don't want. It should be replaced by row 3 on pressing the button.

• In your example, \end{ocg} is missing. Anyway, it will not work to span an environment across several tabular entries/lines, ocg or other. Mar 27 at 14:08
• @gernot, fixed the \end{ocg} problem. For your second line, note that \verb environment works to do its job irrespective of presence of anything in it. Something like that may also exist in this case, which compiles or comments a specific piece of code based on pressing a button. Mar 27 at 15:11

Alignment characters of tabular may not be used within any user-defined environment. (You may verify this with a no-op environment, such as \newenvironment{bla}{}{}).

One way to work around this could be wrapping every table row within a tabular of its own:

\documentclass{article}
\usepackage{ocgx2}
\usepackage{hyperref}

\newenvironment{blabla}{}{}

\begin{document}
\noindent%
\begin{tabular}{|c|c|} \hline
Hi&Hello\\ \hline
\end{tabular}\\[-\dimexpr\lineskip+\fboxrule\relax]
\begin{ocg}{OCG 1}{ocg1}{on}
\begin{tabular}{|c|c|}\hline
Hi&Hello\\ \hline
\end{tabular}
\end{ocg}

\noindent\switchocg{ocg1}{Button.}
\end{document}


"Unfolding" is not possible with the PDF format. In contrast to HTML, PDF is paginated content. After typesetting the stream of input, every glyph has its fixed position within a paragraph of text and within a page.

"Unfolding", as requested, would require re-typesetting the whole document during viewing time whenever text is hidden or shown by user-interaction, because the arrangement of paragraphs, the number of pages and even the table of contents would change. But, PDF viewers do not have a built-in (LaTeX-)typesetter.

• It doesn't have the effect of unfolding rows, if used in the middle of a table, it just makes them invisible, when they aren't shown. Mar 28 at 2:34
• Please check out the edit in my post, the previous minimal working example turned out to be too minimal to make my motive clear. Mar 28 at 2:45
• I extended the answer a bit. Mar 28 at 10:54
• Ok thank you @AlexG, I would be happy if that were possible, though. Mar 28 at 10:56