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...

  • 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. Dec 2, 2013 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, 2016 at 12:06

2 Answers 2


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}


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




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.