TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to display ocgs in a pdf and use the ocgx package to achieve this. Sometimes the content I have in the environment is so long it needs a page break. This is incompatible with the ocg system and the content on the second page is always displayed instead of being optional. A simple fix would be to detect if the ocg is going to contain a page break and split the environment, e.g.


would be detected and the output would resemble the result of e.g.




How can I achieve this? I tried wrapping in a mdframed with beforebreak and afterbreak hooks to break and restart the ocg environment but I don't think ocgs like being nested in other environments...

share|improve this question
I think the ocgs-p package is making things unneccessarily difficult here. From all I see, you should be able to use \ocg to start and \endocg to end an OCG, without nesting problems. – Stephan Lehmke Dec 2 '13 at 16:00
@StephanLehmke : No, this cannot work out-of-the-box. Content to be marked as optional requires the insertion of opening and closing tags into the page content stream. Every PDF page has its own content stream and the scope of the tags is confined within such a page content stream. Thus, a stack of open layers must be maintained. Open layers must be automatically closed at the end of a page and reopened at the start of the new page. This has been implemented in the ocgx2 package. – AlexG Mar 4 at 12:06

Package ocgx2 (as of v0.11, [2015/11/23]) does it automatically.

In the code example that follows, text is placed on nested layers which extend across page breaks. In order to switch the layer visibility use the 'Layers' panel of the PDF viewer:





\begin{ocg}{outer layer (red)}{outer}{on}
  \begin{ocg}{inner layer (blue)}{inner}{on}

share|improve this answer

I've been trying this myself but don't have the expertise to dig into mdframed.dtx and extract the obviously necessary box-splitting code. I've hacked together what seems to be a workable solution from Breakable vboxes.

I'd appreciate some help converting this plain-texy solution into a more latexy one.




\def\continuesplitting{\null % In case this starts a new page
    \setbox\partialocgbox=\vsplit\totalocgbox to\dimen255

%TODO give this macro an appropriate name



share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.