# Minimize automatically the page size of the PDF outputs

I would like to produce the PDF version of the following TEX code without margins. How can I achieve this automaticcaly, even if it needs tricky code (I'm a regular user of Python) ?

I would like to that so as to show examples of small LaTeX codes without wasting storing spaces on my web site.

\documentclass[10pt,a4paper]{article}
\usepackage[utf8x]{inputenc}
\usepackage{ucs}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}

\begin{document}

\section{One document to display}

How to produce an output without any margin.

\noindent
$E = m c^2$ but it seems that we can have $v > c$.

\end{document}

-
Are you asking about a small page/paper size or about small file size? (I don't think getting rid of margins will have a huge impact on file size.) In either case, this question is likely to be a duplicate; in case of the former, take a look at tex.stackexchange.com/questions/674/…, in case of the latter tex.stackexchange.com/questions/2198/… and tex.stackexchange.com/questions/18987/…. –  doncherry Oct 26 '11 at 13:22

I would recommend you use the varwidth package which is similar to the minipage environment, but sets its width to match the narrower natural width based on the content.

This can be used either with the standalone package, or you can directly use the preview package to get (note that the border shown here is from my image capturing):

Here is the MWE using the standalone class:

\documentclass[tightpage]{standalone}
\usepackage{varwidth}
\begin{document}
\begin{varwidth}{\linewidth}
\section{One document to display}

How to produce an output without any margin.

\par\noindent
$E = m c^2$ but it seems that we can have $v > c$.
\end{varwidth}


and using the preview package:

\documentclass{article}
\usepackage[active,tightpage]{preview}
\usepackage{varwidth}
\begin{document}
\begin{preview}
\begin{varwidth}{\linewidth}
\section{One document to display}

How to produce an output without any margin.

\par\noindent
$E = m c^2$ but it seems that we can have $v > c$.
\end{varwidth}
\end{preview}
\end{document}


I suspect that standalone class internally uses the preview package to do something like this, so probably not much difference in this case, but might be useful if you needed to use a different class.

If this is something you are going to do often it is worthwhile automating this solution by using \AtBeginDocument and \AtEndDocument in the preamble to automate this. Here is the same code with this automation in the preamble and using the book class:

\documentclass{book}
\usepackage[active,tightpage]{preview}
\usepackage{varwidth}
\AtBeginDocument{\begin{preview}\begin{varwidth}{\linewidth}}
\AtEndDocument{\end{varwidth}\end{preview}}

\begin{document}
\section{One document to display}

How to produce an output without any margin.

\par\noindent
$E = m c^2$ but it seems that we can have $v > c$.
\end{document}

-
The last solution is great. Thanks a lot ! –  projetmbc Oct 26 '11 at 23:19
I didn't know about the varwidth package until I saw your answer. Great stuff! –  Mico Oct 27 '11 at 21:17

Use the standalone rather than the article document class. Among other things, it will crop rather tightly the output of your (pdf)latex run. E.g., the following modification of your example code (I've mainly stripped out some unneeded \usepackage instructions),

\documentclass[10pt,a4paper]{standalone}
\begin{document}
\section{One document to display}
How to produce output without any margin.

\noindent
$E = m c^2$ but it seems that we can have $v > c$.
\end{document}


will produce the following output (one can't see that the image is cropped tightly, so you'll have to take my word for it):

Addendum: If you want to restrict the width of the pdf file produced with the document class set as standalone, say to the width of a known string of text, you can do so by using the \settowidth command in the preamble. In the example you provided, the relevant string is the content of the section header -- the numeral 1, a space (of width \quad), and the string "One document to display", all set in bold in the font size \Large. Hence, you'd type

\settowidth{\textwidth}{\Large\textbf{1\quad One document to display}}


to set the \textwidth macro.

Obviously, the macro \textwidth should be set to the width of the single longest string of text you anticipate having. I recommend you first comment out the \settowidth instruction in the preamble, optimize the document to your liking, then figure out what the longest string is, and finally make that string the second argument of the \settowidth command. Happy TeXing!

-
Hello. The problem with the standalone package is that there is still an unusefull margin at the right... –  projetmbc Oct 26 '11 at 13:26
I've provided an addendum to show how one can restrict the width of the pdf file, using the command \settowidth. –  Mico Oct 26 '11 at 14:19
Thanks for this amelioration but I would like an automatic solution because all the PDF ouptuts will be generated automatically... –  projetmbc Oct 26 '11 at 16:07
The extra space at the right is caused by the paragraph break which creates a full box \textwidth wide. Simply using \mbox{} around it to avoid a paragraph should work. Also a minipage with a given width should do it. –  Martin Scharrer Oct 26 '11 at 16:31
@FaheemMitha: Try the varwidth option which got added with v1.0. –  Martin Scharrer Nov 12 '12 at 10:30
You could configure your document to have no margins, for example by geometry package commands or using the standalone class, as Mico already suggested.
However if you need class features and cannot use standalone, or if you don't like to change the document, but to crop the margins from the PDF output, you could use the pdfcrop script which can automatically crop the white margins. Here it would be useful to configure empty page style for removing the page numbering as well.