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)

Gabriele
  • 1,815
  • you can specify the page size via geometry package, or simply set \pdfpageheight and \pdfpagewidth directly – David Carlisle Jul 11 '17 at 09: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? – Gabriele Jul 11 '17 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 – David Carlisle Jul 11 '17 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? – Gabriele Jul 11 '17 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. – David Carlisle Jul 11 '17 at 11:40
  • Why not use a pdf viewer which can zoom to textwidth? For example the internal viewer of texstudio is capable to do this – samcarter_is_at_topanswers.xyz Jul 11 '17 at 15:06
  • @samcarter I have hundreds of emacs functions and okular has a good forward and reverse search with emacs. – Gabriele Jul 11 '17 at 18:41

3 Answers3

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

javadr
  • 2,404
  • It works with my simple example but I'm not able to make it works with my working documents layout... – Gabriele Jul 11 '17 at 10:11
  • The problem I have is that the pages are trimmed only on the right-bottom. Can you help me with that? – Gabriele Jul 11 '17 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 '17 at 11:38
  • Unfortunately I'm not allowed to do that. :-( – Gabriele Jul 11 '17 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}
touhami
  • 19,520
  • Thank you. I have the same problem as with the crop package. I solved with Steven B. Segletes solution. – Gabriele Jul 11 '17 at 13:33
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

  • This. with some calibration, works for me. Thanks al lot! – Gabriele Jul 11 '17 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. – Gabriele Jul 11 '17 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. – Steven B. Segletes Jul 11 '17 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. – Gabriele Jul 11 '17 at 14:48
  • @GabrieleNicolardi That's a neat technique – Steven B. Segletes Jul 11 '17 at 14:56