9

Consider the following class.cls:

\LoadClass{article}
\RequirePackage{tikz}

and the following test.tex:

\documentclass{class}
\begin{document}
Test 
\end{document}

With pdftex (TeXLive 2016), I do:

pdftex -ini -jobname="test-fmt-pdftex" "&pdflatex" mylatexformat.ltx """test.tex"""
pdftex -fmt=test-fmt-pdftex.fmt test.tex

It works with no problem.

With luatex (TeXLive 2016), I do:

luatex -ini -jobname="test-fmt-luatex" "&lualatex" mylatexformat.ltx """test.tex"""
luatex -fmt=test-fmt-luatex.fmt test.tex

The second line ends up with an error:

! error:  (pdf backend): referenced object has wrong type others; should be obj
!  ==> Fatal error occurred, no output PDF file produced!

I know that I cannot dump in a luatex format lua code or opentype fonts but I think that tikz does not do that. There must be an other reason. Do you have any ideas?


I investigated a bit.

As far as I understand, there has been an updated version of pgfsys-luatex.def to comply with the changes made in luatex related to \pdf... primitives. This is one hint I can think of.

There is also this in the luatex manual:

One change involves the so called xforms and ximages. In pdfTEX these are implemented as so called whatsits. But contrary to other whatsits they have dimensions that need to be taken into account when for instance calculating optimal line breaks. In LuaTEX these are now promoted to normal nodes, which simplifies code that needs those dimensions.

But I must admit I don't really understand all that.

cjorssen
  • 10,032
  • 4
  • 36
  • 126
  • In your comment there, you said that it would not work due to the use of pdftexcmds. Do you no longer think that's true? – cfr Aug 27 '16 at 12:42
  • 1
    It uses directlua in various places, including pgfutil-common, which I'm guessing gets loaded by tikz although I didn't trace all this through. – cfr Aug 27 '16 at 12:59
  • 1
    Also the RCS stuff uses directlua and the code for TikZ comments that \usepgflibrary is provided by pgfutil-common. So TikZ definitely relies on \directlua and the Lua code it loads. – cfr Aug 27 '16 at 13:02
  • @cfr Thanks for investigating. It's been a while since I read the code. Do you see any mean to circumvent the situation? – cjorssen Aug 27 '16 at 13:07
  • If David couldn't suggest anything useful on your other question, then I am certainly not going to be able to come up with anything! Do you really need TikZ in the format? I realise that you have a liking for custom formats, but there isn't really any reason to use one, is there? The usual reason people mention is compilation time, but the difference is going to be really negligible with today's hardware. – cfr Aug 27 '16 at 15:01
  • @cfr You're probably right regarding david's comments. I'll have to write a patch to separate more engine behaviours at pgf level. Maybe it is not that intricated. One thing that I still don't understand is the error triggered by luatex regarding pdf objects. – cjorssen Aug 27 '16 at 15:31
  • @cfr Regarding compilation speed, it does matter with my somewhat old hardware + hundreds of compilations à day. – cjorssen Aug 27 '16 at 15:32
  • 2
    pgfsys-luatex.def calls at the end two commands \pgfutil@setuppdfresources and \pgfutil@addpdfresource@colorspaces. If you remove these calls your example works. The commands call the code in \pgf@sys@setuppdfresources@plain and this set up pdf objects: disabling the obj in \pgfutil@everyby avoids the error too. I don't know why the similar code doesn't give an error with pdflatex but I would avoid to move the creation of objects to the format with pdflatex too. – Ulrike Fischer Aug 27 '16 at 17:21
  • @UlirikeFischer Wow. That seems really interesting. Do you think it has something to do with xforms? Do you think I should ask on luatex-dev list? – cjorssen Aug 27 '16 at 18:49
  • If compilation speed is that critical, why are you moving to LuaTeX? Presumably you just can't manage without it for some reason. Because the change of compiler will have a far more dramatic effect on compilation speed than loading TikZ from file versus in the format. – cfr Aug 27 '16 at 19:27
  • @cfr ok, but it is still surprising that change of behaviour between luatex and pdftex. – cjorssen Aug 27 '16 at 19:47
  • By the way, it fails with TL 2015, though with a different error. (It panics first.) – cfr Aug 27 '16 at 23:49
  • @UlrikeFischer You should answer this .... No point in wasting the bounty ;). – cfr Sep 01 '16 at 23:01

1 Answers1

6

pgfsys-luatex.def calls at the end two commands

  • \pgfutil@setuppdfresources and
  • \pgfutil@addpdfresource@colorspaces.

If you remove these calls your example works. So there is somewhere the source of the problem. The commands call the code in \pgf@sys@setuppdfresources@plain and this set up pdf objects: disabling the obj in \pgfutil@everyby avoids the error too.

Boiling everthing a bit down one can create the following (senseless!!) minimal document:

\documentclass{book}
\usepackage{ifluatex}
\ifluatex
\pdfextension obj  reserveobjnum \edef\myobjnum{\the\numexpr\pdffeedback lastobj\relax}
\else
\pdfobj reserveobjnum \edef\myobjnum{\the\pdflastobj}
\fi
%\makeatother
\begin{document}
\ifluatex
\immediate \pdfextension obj  useobjnum \myobjnum {<</pgfprgb [/Pattern /DeviceRGB]>>}
\else
\immediate \pdfobj  useobjnum \myobjnum {<</pgfprgb [/Pattern /DeviceRGB]>>}
\fi
blub
\end{document}

It compiles fine with pdflatex and lualatex and one can open the pdf in the adobe reader (which means that it is not completly broken). With pdflatex you can put the preamble in a custom format using your compilation steps. But a compilation with luatex -fmt=myfmt.fmt test-utf8 ends with

! error:  (pdf backend): cannot find referenced object
!  ==> Fatal error occurred, no output PDF file produced!

It looks as if luatex doesn't like it if one tries to reserve objects in the format. I can't tell you if this is intended, or some side effect, or even a bug: you will have to ask on the luatex list.

Even if it works with pdftex: I would avoid to put objects in a format anyway (and so not move tikz there).

Ulrike Fischer
  • 327,261
  • Thanks to you, Ulrike, we now have an analysis of what's going on here. In addition, I've been able to code a dirty workaround to include tikz (and others) in my format. I'm going to post on luatex list: maybe devs will tell us if it's a bug. Thanks again! – cjorssen Sep 02 '16 at 12:58