Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I want to generate a ps file with layers. Ocg.sty does not support ps, so I want to write a simple example by myself to see if its possible. I took a pdf version from here and tried to change the commands to generate a ps version (code is appended here).

Well, the ps file is generated, but it seems to be corrupt. When I use ps2pdf there are errors reported. If the part with "AddToShipoutPicture" is removed ps2pdf reports no errors, so it seems the error is there to find. But maybe more is wrong. Maybe someone can help to make a simple working solution.

\documentclass{article}
\RequirePackage{eso-pic}
\RequirePackage{ifpdf}

\newcommand{\setupOCG}{
  \special{ps: mark /_objdef {@ocg1} /type/dict /OBJ pdfmark}
  \special{ps: mark {@ocg1} <</Type/OCG /Name (My first layer)>> /PUT pdfmark}
  \special{ps: mark /_objdef {@ocg2} /type/dict /OBJ pdfmark}
  \special{ps: mark {@ocg2} <</Type/OCG /Name (My second layer)>> /PUT pdfmark}

  %register it and configure its default behaviour
  %\special{ps: mark /_objdef {{@ocg1} {@ocg2}} /type/array /OBJ pdfmark}
  \special{ps:mark {Catalog} <<
    /OCProperties <<
      /OCGs [{@ocg1} {@ocg2}]
      /D <</BaseState/ON /Order [{@ocg1} {@ocg2}]  /OFF [{@ocg1}]  >> %first layer invisible
    >>
  >> /PUT pdfmark}

  %add the OCG to the resources of the current page object
  \AddToShipoutPicture{%
    \special{ps:mark {Resources} <<
      /Properties <<
        %this maps the name to the OCG object
        /oc1 {@ocg1} /oc2 {@ocg2}
      >>
    >> /PUT pdfmark}
  }
}

\newcommand{\myocg}[2]{%    
\special{ps: mark /OC /#1 /BDC pdfmark}#2\special{ps: mark /EMC pdfmark}% ok
    %\special{ps: mark {ocgname\the\ocnum} <</OC {/@oc\the\ocnum}>> /PUT pdfmark}
}

\setupOCG

\begin{document}
  \myocg{oc1}{I am NOT visible on the first layer.}
  \myocg{oc2}{I am visible on the second layer.}
\end{document}
share|improve this question
1  
The /OC ... BDC-EMC bracket around page content to be made optional must be inserted directly into the page stream. Ghostscript implements only a subset of pdfmarks, /BMC and /EMC not being amongst these (according to gdevpdfm.c in the Ghostscript sources). –  AlexG Nov 26 '12 at 13:42
    
@AlexG: Please convert this comment into an answer. –  Martin Schröder Feb 2 '13 at 21:27

1 Answer 1

In order to create layers in PDFs using OCGs (optional content groups), the content in the page stream that is to be made optional must be enclosed in a /OC ... BDC-EMC bracket.

Unfortunately, the /BDC and /EMC pdfmarks, which serve this purpose, are not supported by Ghostscript's ps2pdf (according to gdevpdfm.c in the Ghostscript sources).

Currently, only Adobe Distiller supports them.

The corrected code example from the original posting, to be run through latex->dvips-> Distiller, reads:

\documentclass{article}

\newcommand{\setupOCG}{
  \special{ps: mark /_objdef {ocg1} /type/dict /OBJ pdfmark}
  \special{ps: mark {ocg1} <</Type/OCG /Name (My first layer)>> /PUT pdfmark}
  \special{ps: mark /_objdef {ocg2} /type/dict /OBJ pdfmark}
  \special{ps: mark {ocg2} <</Type/OCG /Name (My second layer)>> /PUT pdfmark}

  %register it and configure its default behaviour
  \special{ps:mark {Catalog} <<
    /OCProperties <<
      /OCGs [{ocg1} {ocg2}]
      /D <</BaseState/ON /Order [{ocg1} {ocg2}]  /OFF [{ocg1}]  >> %first layer invisible
    >>
  >> /PUT pdfmark}
}

\newcommand{\myocg}[2]{%    
  \special{ps: mark /OC {#1} /BDC pdfmark}#2\special{ps: mark /EMC pdfmark}%
}

\setupOCG

\begin{document}
  \myocg{ocg1}{I am NOT visible on the first layer.}
  \myocg{ocg2}{I am visible on the second layer.}
\end{document}
share|improve this answer

Your Answer

 
discard

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.