7

Is there a way to trim the margins of the entire document by LaTeX? I need to do that temporarily, to make more comfortable my typesetting work. I figured out to pass this option by command line with the \AtBeginDocument technique but a preamble solution is also welcome.

To better explain my purposes I'd like to get the same result that I can achieve with the bash command (that here works because of the coloured page):

pdfcrop --margins '-50 -50 -50 -50' mydoc.pdf mydoc.pdf

E.g. for the code:

\documentclass[a4paper,11pt]{article}

%% \usepackage{xcolor}
%% \pagecolor[HTML]{E4EDE9}

\usepackage{blindtext}

\begin{document}

\section{Section}
\blindtext

\blindtext

\blindtext\footnote{\blindtext}

\end{document}

I want to change the layout from:

enter image description here

NOTE. I obtain this layout by passing the color option by command line:

pdflatex '\AtBeginDocument{\RequirePackage{xcolor} \pagecolor[HTML]{E4EDE9}} \input{Latex.tex}'

(I want to thank Thérèse for this very eye-relaxing background color)

to:

enter image description here

An okular specific solution is welcome but, since I use many viewers, I prefer a LaTeX solution.

NOTE2. I need a solution that does not change the text layout. I need only to trim the pdf as it stands.


SOLUTION

I want to share the solution that works in my case (thanks to Steven B. Segletes). This is the code for a command line cropped and coloured compilation:

pdflatex '\AtBeginDocument{\newlength\DX \DX=3cm \paperwidth=\dimexpr\paperwidth-\DX\relax \hoffset=\dimexpr\hoffset-.5\DX\relax \newlength\DY \DY=3cm \paperheight=\dimexpr\paperheight-\DY\relax \voffset=\dimexpr\voffset-.1\DY-.5\footskip\relax \RequirePackage{xcolor} \pagecolor[HTML]{E4EDE9}} \nonstopmode\input{mydoc.tex}'

NOTE ADDED. After TeX Live 2020 October update this solution needs an update: Trim margins of the entire document by command line (after TeX Live 2020 update)

7
  • you can specify the page size via geometry package, or simply set \pdfpageheight and \pdfpagewidth directly Jul 11, 2017 at 9:33
  • @DavidCarlisle I'm not able to use the geometry package with my documents because it messes up my standard layout. With \pdfpageheight and \pdfpagewidth the document is not centered but trimmed on bottom and right margins. What is my mistake? Jul 11, 2017 at 10:31
  • geometry only changes the typeset layout if you tell it to do so, setting pdfpageheight and pdfpagewidth will just change the size so trim from right and bottom but you can always re-adjust with \hoffset and \voffset Jul 11, 2017 at 10:38
  • @DavidCarlisle Really, I can't understand. Even if I use just \usepackge[a4paper]{geometry} in my preamble my layout is messed up. How can I tell geometry to not do so? Jul 11, 2017 at 11:26
  • geometry has an option passthrough or somesuch that says not to change anything, or of course you can use geometry rather than primitive length settings to set all your document. If you use a4paper option that tells geometry to set the text block for 14 paper size so does set the typesetting related lengths. Jul 11, 2017 at 11:40

3 Answers 3

3

Here I take the baseline article documentclass (using the pass option of geometry) and change the \paperwidth and \paperheight by the specified amount and then associated margins (actually \hoffset and \voffset) by one half of the page change dimensions.

\documentclass{article}
\usepackage{lipsum}
\usepackage[pass]{geometry}
\newlength\DX
\DX=3.5in
\paperwidth=\dimexpr\paperwidth-\DX\relax
\hoffset=\dimexpr\hoffset-.5\DX\relax
\newlength\DY
\DY=2.8in
\paperheight=\dimexpr\paperheight-\DY\relax
\voffset=\dimexpr\voffset-.5\DY-.5\footskip\relax
\begin{document}
\lipsum[1-10]
\end{document}

enter image description here

Without any changes:

enter image description here

5
  • This. with some calibration, works for me. Thanks al lot! Jul 11, 2017 at 13:31
  • One more thing. Is it possible to pass these strings by command line? I tried to put all that stuff in an \AtBeginDocument command but it does not work. Jul 11, 2017 at 13:42
  • @GabrieleNicolardi Not quite sure how to pass it by command line. But you could place \usepackage[pass]{geometry} \newlength\DX \DX=3.5in \paperwidth=\dimexpr\paperwidth-\DX\relax \hoffset=\dimexpr\hoffset-.5\DX\relax \newlength\DY \DY=2.8in \paperheight=\dimexpr\paperheight-\DY\relax \voffset=\dimexpr\voffset-.5\DY-.5\footskip\relax in a separate file, and then \input that file in the preamble. To not trim it, you could just place a second blank file with the name of the \input file. That is, alternate whether the input file is blank or contains those commands. Jul 11, 2017 at 13:46
  • 1
    Done! You don't need the geometry package for this solution! I compiled with pdflatex '\AtBeginDocument{\newlength\DX \DX=3cm \paperwidth=\dimexpr\paperwidth-\DX\relax \hoffset=\dimexpr\hoffset-.5\DX\relax \newlength\DY \DY=3cm \paperheight=\dimexpr\paperheight-\DY\relax \voffset=\dimexpr\voffset-.1\DY-.5\footskip\relax} \nonstopmode\input{mydoc.tex}' and it works. Thank you. Jul 11, 2017 at 14:48
  • @GabrieleNicolardi That's a neat technique Jul 11, 2017 at 14:56
7

Why don't you use crop package?!

As David Carlisle said you can also use geometry package, but I prefer crop in this case; for more details see the package's manual.

with setting width and height you can achieve your desired margins.

\documentclass[a4paper,11pt]{article}
\usepackage[a4,center,noinfo,cross, width=13.5cm,height=22.5cm]{crop}
\usepackage{blindtext}

\begin{document}

\section{Section}
\blindtext
\blindtext
\blindtext\footnote{\blindtext}
\end{document}

enter image description here

4
  • It works with my simple example but I'm not able to make it works with my working documents layout... Jul 11, 2017 at 10:11
  • The problem I have is that the pages are trimmed only on the right-bottom. Can you help me with that? Jul 11, 2017 at 11:31
  • I have no idea about that, maybe your are using a class or packages thta redefines the margins. It's better to make a MWE that shows the problem.
    – javadr
    Jul 11, 2017 at 11:38
  • Unfortunately I'm not allowed to do that. :-( Jul 11, 2017 at 11:55
3

You can try

\AtBeginDocument{%
\edef\mt{\the\textwidth}%
\edef\mtt{\the\textheight}%
\RequirePackage{geometry}%
\geometry{paperwidth=\dimexpr\mt+1cm\relax,
paperheight=\dimexpr\mtt+1cm\relax,margin=.5cm}
\RequirePackage{xcolor} 
\pagecolor[HTML]{E4EDE9}}

\documentclass[a4paper,11pt]{article}

\usepackage{blindtext}

\begin{document}

\section{Section}
\blindtext

\blindtext

\blindtext\footnote{\blindtext}

\end{document}
1
  • Thank you. I have the same problem as with the crop package. I solved with Steven B. Segletes solution. Jul 11, 2017 at 13:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .