[tex-k] dvips, mpage, dictionary stack

hanche@math.ntnu.no hanche@math.ntnu.no
21 Mar 2002 16:41:48 -0000

A colleague is working on a big book manuscript.  He occassionally
prints out the entire book, after running the ps file through mpage to
save some paper.  Lately, the print job has started crashing near the
end with a dictstackoverflow error.

I turns out that TeXDict is getting pushed onto the dictionary stack
once per input page and never popped off again.

This is not the fault of dvips alone; rather, it is the result of
interaction between dvips and mpage.  Nevertheless, I want to suggest
a change to dvips to alleviate the problem.  Description follows.

1. The PostScript file emitted by dvips has the following setup
section (between the prolog section and the first page):

%%Feature: *Resolution 600dpi
TeXDict begin
%%PaperSize: A4


The intention is clear:  This is to be executed once only.  The
trailer section, at the end of the file, contains the matching end,
popping TeXDict off the dictionary stack.

2. Mpage copies the (non-comment) contents of the original setup
section to the front of every input page.  It does not copy the
trailer section to the end of each input page.  This creates the
begin/end imbalance.


1. According to the PostScript Language Reference Manual (PLRM), the
setup section is intended for device setup.  Putting a dictionary on
the stack can hardly be counted as device setup!  I would consider
this questionable practice.

2. Since device setup could easily interfere with the proper operation
of mpage, I think maybe mpage should omit the setup section
altogether.  Of course, if it did, dvips output in its present form
could not be mpaged at all.


Changes to dvips:  Remove "TeXDict begin" from the setup section, and
"end" from the trailer.  Instead, add these to the beginning and end
of each individual page.

In addition to fixing the problem with mpage, I think this change will
improve the robustness and page independence of dvips output.

I include an *untested* patch that I believe will do this.  Note,
however, if this is to be done, maybe something similar should be done
in connection with sectioning.  I haven't touched that code, however,
since I don't understand how it works.  (I never used it.)  It is
quite possible that my patch will break sectioning, but I leave that
to the experts.

--- output.c.orig	Sun Jan 27 12:49:58 2002
+++ output.c	Thu Mar 21 17:22:06 2002
@@ -1311,8 +1311,6 @@
    if (multiplesects && ! disablecomments)
       (void)fprintf(bitfile, "%%DVIPSBeginSection\n") ;
-   cmdout("TeXDict") ;
-   cmdout("begin") ;
    if (endprologsent || disablecomments || multiplesects == 0) {
       (void)fprintf(bitfile, "\n") ;
       paperspec(finpapsiz->specdat, 0) ;
@@ -1405,6 +1403,8 @@
          (void)fprintf(bitfile, "%%%%Page: %d %d\n", pagenum, thispage) ;
    linepos = 0 ;
+   cmdout("TeXDict") ;
+   cmdout("begin") ;
    numout((integer)pagenum) ;
    numout((integer)thispage-1) ;
    cmdout("bop") ;
@@ -1424,6 +1424,7 @@
       chrcmd('p') ;
    cmdout("eop") ;
+   cmdout("end") ;
--- dosection.c.orig	Sun Nov  1 04:45:06 1998
+++ dosection.c	Thu Mar 21 17:17:14 2002
@@ -176,7 +176,6 @@
       cmdout("eos") ;
-   cmdout("end") ;
 #ifdef HPS
    if (HPS_FLAG) cmdout("\nend") ; /* close off HPSDict */

- Harald