5

Introduction

At the moment I’m preparing a custom class which is likely to be compiled with XeTeX or LuaTeX. It is for a book layout that should be professionally printed and therefor I want to set the TrimBox of the output PDF. The problem is the size of the bleed is user defined by a key-val class option. Therefore I need to be able to use macros as part of the \special/\pdfpageattr commands.

General Idea

The general Idea is to use either \specials (XeTeX) or \pdfpageattr (LuaTeX, PDFTeX) to set the TrimBox. The distinction of the compiler can be done with LaTeX3 \sys_if_engine_ and also the TrimBox strings can be defined as token lists. Then I define a macro using the variable content (:V) of these token lists to write the correct values to the PDF.

Code

This is the minimal example code I came up with.

\documentclass{article}

\usepackage{xparse}

\ExplSyntaxOn\makeatletter %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% bleed value (keyval class option in real document)
\dim_new:N \g_@@_bleed_dim 
\dim_set:Nn \g_@@_bleed_dim { 3mm }

%% set page size (and other stuff in the real document)
\usepackage { geometry }
   \geometry { papersize = { 185mm , 245mm } }

%% set crop size
\usepackage [ center ] { crop }
   \CROP@size {
      \dim_eval:n { \paperwidth + 2 \g_@@_bleed_dim }
   } {
      \dim_eval:n { \paperheight + 2 \g_@@_bleed_dim }
   }

%% new variant
\cs_generate_variant:Nn \dim_to_decimal_in_bp:n { N }

% values for the rim box
\tl_new:N \g_@@_trim_box_quadruple_tl
\tl_set:Nx \g_@@_trim_box_quadruple_tl {
   \dim_to_decimal_in_bp:N \g_@@_bleed_dim \c_space_token
   \dim_to_decimal_in_bp:N \g_@@_bleed_dim \c_space_token
   \dim_to_decimal_in_bp:n { \paperwidth + \g_@@_bleed_dim } \c_space_token
   \dim_to_decimal_in_bp:n { \paperheight + \g_@@_bleed_dim }
}

%% special for use with XeTeX
\tl_new:N \g_@@_trim_special_tl
\tl_set:Nx \g_@@_trim_special_tl {
   pdf: \c_space_token put \c_space_token @thispage \c_space_token
   << \c_space_token /TrimBox \c_space_token [ \g_@@_trim_box_quadruple_tl ] \c_space_token >>
}

%% page attribute for use with LuaTeX and PDFTeX
\tl_new:N \g_@@_trim_pageattr_tl
\tl_set:Nx \g_@@_trim_pageattr_tl {
   /TrimBox \c_space_token [ \g_@@_trim_box_quadruple_tl ]
}

%% command to set the specials for xetex
\cs_new:Npn \@@_special:n #1 {
   \AtBeginShipout { \immediate \special { #1 } }
   \immediate \special { #1 }
}

%% command to set the page attributes for pdftex/luatex
\cs_new:Npn \@@_pdfpageattr:n #1 {
   \pdfpageattr { #1 }
}

%% make a variants to make sure we get the expaned content
\cs_generate_variant:Nn \@@_special:n { V }
\cs_generate_variant:Nn \@@_pdfpageattr:n { V }

%% [A] THIS DOES NOT WORK!
%% set the trim box according to engine
%\bool_if:nTF { \sys_if_engine_luatex_p: || \sys_if_engine_pdftex_p: } {
%   \@@_pdfpageattr:V \g_@@_trim_pageattr_tl
%} {
%   \sys_if_engine_xetex:TF {
%      \usepackage{atbegshi}
%      \@@_special:V \g_@@_trim_special_tl
%   } { ERROR: UNKOWN MACHINE! }
%}

%% just a macro to show some debug info
\NewDocumentCommand { \debuginfo } { } {
   \par\noindent
   Machine: ~ \texttt{ \c_sys_engine_str }

   \par\bigskip\noindent
   special: \\
   \mbox { \texttt { \g_@@_trim_special_tl } }

   \par\medskip\noindent
   pageattribute: \\
   \mbox { \texttt { \g_@@_trim_pageattr_tl } }
}

\ExplSyntaxOff\makeatother %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% [B] THIS WORKS!
%% for pdftex and luatex
%\pdfpageattr { /TrimBox [8.50392 8.50392 532.91336 702.99211] }
%% for xetex
%\usepackage{atbegshi}
%\AtBeginShipout { \special { pdf: put @thispage << /TrimBox [8.50392 8.50392 532.91336 702.99211] >> } }
%\special { pdf: put @thispage << /TrimBox [8.50392 8.50392 532.91336 702.99211] >> }

\begin{document}
\section*{Test Document}
\debuginfo \clearpage Just another page \ldots
\end{document}

Result

Unfortunately my idea doesn’t work as expected.

When using the manual setup ([B] in the code) everything is fine and the TrimBox is set up properly for PDFTeX, LuaTeX and XeTeX. But when using the auto generated token lists instead ([A] in the code), it does not work: With XeTeX I get a PDF but without any TrimBox settings and with PDFTeX or LuaTeX the PDF file is corrupted and can’t be opened with a viewer even uncompressing it with cpdf fails.

Question

Simple: What’s my mistake?

Notes

If this question leads to a robust code I would make a package out of it an publish it on CTAN.

While researching I found the following questions and want to make clear that this is not a duplicate question ;-)

Furthermore the pdf packages seems to be no help in this case. At least I can’t find anything about trimming in the manual …

4

The problem is that \c_space_token does not expand to a space, I used \space here. It is \let to a space so it is an unexpandable implicit character, if you uncomment the first line with \pdfcompresslevel so the pdf stream is readable you will see that \c_space_token is written literally into the pdf stream between the trim box coordinates. Conversely \space is a macro that expands to a space character.

%\pdfcompresslevel0
\documentclass{article}

\usepackage{xparse}

\ExplSyntaxOn\makeatletter %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% bleed value (keyval class option in real document)
\dim_new:N \g_@@_bleed_dim 
\dim_set:Nn \g_@@_bleed_dim { 3mm }

%% set page size (and other stuff in the real document)
\usepackage { geometry }
   \geometry { papersize = { 185mm , 245mm } }

%% set crop size
\usepackage [ center ] { crop }
   \CROP@size {
      \dim_eval:n { \paperwidth + 2 \g_@@_bleed_dim }
   } {
      \dim_eval:n { \paperheight + 2 \g_@@_bleed_dim }
   }

%% new variant
\cs_generate_variant:Nn \dim_to_decimal_in_bp:n { N }

% values for the rim box
\tl_new:N \g_@@_trim_box_quadruple_tl
\tl_set:Nx \g_@@_trim_box_quadruple_tl {
   \dim_to_decimal_in_bp:N \g_@@_bleed_dim \space 
   \dim_to_decimal_in_bp:N \g_@@_bleed_dim \space
   \dim_to_decimal_in_bp:n { \paperwidth + \g_@@_bleed_dim } \space
   \dim_to_decimal_in_bp:n { \paperheight + \g_@@_bleed_dim }
}

%% special for use with XeTeX
\tl_new:N \g_@@_trim_special_tl
\tl_set:Nx \g_@@_trim_special_tl {
   pdf: ~ put ~ @thispage ~
   << ~ /TrimBox \space [ \g_@@_trim_box_quadruple_tl ] ~ >>
}

%% page attribute for use with LuaTeX and PDFTeX
\tl_new:N \g_@@_trim_pageattr_tl
\tl_set:Nx \g_@@_trim_pageattr_tl {
   /TrimBox \space [ \g_@@_trim_box_quadruple_tl ]
}




%% command to set the specials for xetex
\cs_new:Npn \@@_special:n #1 {
   \AtBeginShipout { \immediate \special { #1 } }
   \immediate \special { #1 }
}

%% command to set the page attributes for pdftex/luatex
\cs_new:Npn \@@_pdfpageattr:n #1 {
   \pdfpageattr { #1 }
}

%% make a variants to make sure we get the expaned content
\cs_generate_variant:Nn \@@_special:n { V }
\cs_generate_variant:Nn \@@_pdfpageattr:n { V }

%% [A] THIS DOES NOT WORK!
%% set the trim box according to engine
\bool_if:nTF { \sys_if_engine_luatex_p: || \sys_if_engine_pdftex_p: } {
   \@@_pdfpageattr:V \g_@@_trim_pageattr_tl
} {
   \sys_if_engine_xetex:TF {
      \usepackage{atbegshi}
      \@@_special:V \g_@@_trim_special_tl
   } { ERROR: UNKOWN MACHINE! }
}

%% just a macro to show some debug info
\NewDocumentCommand { \debuginfo } { } {
   \par\noindent
   Machine: ~ \texttt{ \c_sys_engine_str }

   \par\bigskip\noindent
   special: \\
   \mbox { \texttt { \g_@@_trim_special_tl } }

   \par\medskip\noindent
   pageattribute: \\
   \mbox { \texttt { \g_@@_trim_pageattr_tl } }
}

\ExplSyntaxOff\makeatother %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% [B] THIS WORKS!
%% for pdftex and luatex
%\pdfpageattr { /TrimBox [8.50392 8.50392 532.91336 702.99211] }
%% for xetex
%\usepackage{atbegshi}
%\AtBeginShipout { \special { pdf: put @thispage << /TrimBox [8.50392 8.50392 532.91336 702.99211] >> } }
%\special { pdf: put @thispage << /TrimBox [8.50392 8.50392 532.91336 702.99211] >> }

\begin{document}
\section*{Test Document}
\debuginfo \clearpage Just another page \ldots
\end{document}
  • Wow. That was easy. Thanks! Just find out that \c_space_tl works too so I’m using this because it feels more constituent in the context of LaTeX3 … – Tobi Mar 6 '16 at 11:32
  • Tobi and David; it was an excellent effort, thanks for this, just a small clarification, is it possible to fix this in LaTeX + dvips + Ps2PDF? Please suggest... – MadyYuvi Sep 26 '18 at 11:04
  • David, Is it possible to fix the BleedBox by using your suggestion? Please ... – MadyYuvi Aug 20 at 11:52

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.