6

I wonder wheither reusing \sbox'es decreases only .tex code or also its final output whether DVI/PDF/... i.e. does TeX expand them while writing DVI or PDF or does the viewer do that? For example gnuplot doesn't use \sbox'es for identical tickmarks on opposite sides of a plot.

9

No, saveboxes created with \sbox and friends do not reduce the final file size. The content is written to the output file every time it is used in the document.

I actually wrote a LaTeX package storebox which provides "storeboxes" which work like saveboxes but only store the content once in the PDF. However, this only works with pdflatex in PDF mode, not with DVI, because it uses a feature of the PDF format.

| improve this answer | |
  • As I understand DVI file format is a kind of machine language with opcodes, and a stack, so does \sbox still reduce file size of DVI? perhaps the conversion to PDF expands them... – propaganda Jan 13 '12 at 20:25
  • 1
    OK, I went through the short DVI file type specification, and there's no display lists or call functions, so TeX necessarily expands repeated \sbox usage – propaganda Jan 13 '12 at 21:59
  • But does the use of saveboxes reduce compilation time, if not file size? My gut says yes. – Steven B. Segletes Nov 29 '17 at 15:25
  • 1
    @StevenB.Segletes: As the content is only typeset once the box is saved it indeed reduces compilation time compared to typeset the content several times by using a macro. The file size is not reduced using \sbox as the content is repeated in the output file on every use. – Martin Scharrer Nov 29 '17 at 15:57
4

As Martin said, standard LaTeX save-boxes \sbox, \savebox and lrbox environment repeat the DVI, PS and PDF code on every \usebox.

Package xsavebox provides \xsbox, \xsavebox, xlrbox and \xusebox, which, apart from box naming, are used in the same way as the standard LaTeX commands, but insert the box content only once as a PDF XObject. Repeated usage of \xusebox inserts references to the object. This works for all common workflows, including dvips+ps2pdf.

E.g. \xsbox{My Box 1}{Hello world!} ... \xusebox{My Box 1}

or \xsbox{MyBoxOne}{Hello world!} ... \theMyBoxOne

Of course, this does not make sense for boxes with little content as in the example because some overhead is introduced. But it may reduce the PDF file size for repeated TikZ graphics, used e.g. as watermarks, and alike.

| improve this answer | |
  • Interesting package. Just one remark: when you use \setlength it should be, in my opinion, \dim_set:Nn which already allows for dimension expressions. Not for \ratio from calc, but this can be emulated with xfp. – egreg Nov 29 '17 at 15:31
  • Indeed, the motivation for using \setlength in the definition of package/command options was to address potential usage of calc-style dimension expressions provided by the user. This ensures the same (expected) behaviour as with the standard \savebox command. Otherwise the user code would need to be parsed in order to substitute \ratio or other macros. – AlexG Nov 29 '17 at 15:46
  • For instance \setlength{\newYsize}{\Ysize*\ratio{\textwidth}{\Xsize}} (the example in the manual for calc) becomes \setlength{\newYsize}{\fpeval{\textwidth/\Xsize}\Ysize}. But both syntaxes are accepted, it seems, so possibly your strategy is good. – egreg Nov 29 '17 at 15:49
  • You can avoid overhead of xfp at least for such operation, see tex.stackexchange.com/a/383689/4686 – user4686 Nov 29 '17 at 16:38
  • Finally, the user decides what goes into te optional width argument of \xsavebox/\savebox (calc-style, \dimexpr, ...). And this is beyond the control of the command's implementation. Therefore, \setlength was used internally to fully comply with \savebox behaviour. – AlexG Nov 29 '17 at 20:22

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.