2

It want a PDF (or PNG) output of a Latex document on one long page, trimmed without the white borders, in minimal runtime.

The preview and standalone packages have this built in in some way and I would like to use their approach. However, both packages have been written with additional goals in mind and have unwanted side effects on paragraphs, floats, footnotes, itemizations, macros etc. which break my documents in other places.

Where do I need a hint?

I would like to extract the feature of setting the pdf page height in one Latex run at the end of this run - as it is used in standalone or preview but I fail to extract the core functionality from these packages: I am simply not good enough in reading their macros. Here I would appreciate some hint.

What else have I tried and failed with?

  1. I used zref-absolute or \pdfsaveposition to write the document length to a file, read the file again in the next pdflatex run and then set the paper size as required. Works fine but needs two invocations of pdflatex, which doubles the runtime and is unwanted for my use case.

  2. I used pdf-crop on the result. pdf-crop uses ghostscript to pick up the bounding-box (which takes very long) and it removes the PDF annotations embedded into the file, both of which is a show stopper for my use case. I need the annotations and the timing of ghostscript doubles the runtime.

  3. I used pdf.jsand node for PDF->PNG rendering, detecting the bounding box in the PNG, cropping to this box (and for my processing of the annotations). Works, but is very slow.

  4. I used mutool run and its' Javascript interface. I can process the annotations as needed, detect the boundary box, do the PDF to PNG rendering and all of this blazingly fast but I do not manage to properly implement the crop: The PNG always is too large and it looks like the mupdf - Javascript binding either has some form of bug in the implementation of the transformation matrix and did not bind to a suitable cropping method.

So there I am looking at preview and standalone code and trying to extract these probably quite short functionalities...without luck.

3
  • 1
    Is there a reason for not using \usepackage[margin=0cm]{geometry} then?
    – Archange
    Commented Nov 6, 2021 at 12:43
  • Floats and footnotes are tied to page breaking, and you only have one page. Everything else will work inside a minipage with standalone. Actually, so will footnotes. Commented Nov 6, 2021 at 13:42
  • @Archange: The reason is that margin=0cm cuts on the top, left and right margin but leaves the bottom margin in place, as it does not influence paper height. Commented Nov 6, 2021 at 14:43

2 Answers 2

5

You might want to tweak the top and bottom slightly but basically

enter image description here

\documentclass{article}

\begin{document}

\setbox0\vbox{{
\section{test}
some text some text some text some text some text some text some text some text 
some text some text some text some text some text some text some text some text 
some text some text some text some text some text some text some text some text 
some text some text some text some text some text some text some text some text 

some text some text some text some text some text some text some text some text 
some text some text some text some text some text some text some text some text 
some text some text some text some text some text some text some text some text 
some text some text some text some text some text some text some text some text 

\section{ another section}
some text some text some text some text some text some text some text some text 
some text some text some text some text some text some text some text some text 
some text some text some text some text some text some text some text some text 
some text some text some text some text some text some text some text some text 
}}

\pdfpageheight=\ht0
\pdfpagewidth=\wd0
\hoffset-1in
\voffset-1in
\shipout\box0

\end{document}
4
  • I see the idea. Nice. Still opening up further issues. With \section the .aux produces error messages. Similar when there is a figure (not in outer par mode). So taking this as a generic solution is problematic. Any other possibility to tap into the shipout mechanism? The idea as such is great. Thanx. Commented Nov 6, 2021 at 15:32
  • 1
    @Nobody-Knows-I-am-a-Dog well if you want floating figures then you have a multi-page document so not sure what it means to make a png. Commented Nov 6, 2021 at 15:34
  • The goal is: Do the typesetting as if the paper were very long (5m). When this is done: Realize that we only used 1.3m and trim this to a 1.3m PNG. When later several snippets are combined, offer the possibility to have a classical a4paper document layout. Anyhow: The figures are there already in the existing text, so I have to deal with them in some way. Commented Nov 6, 2021 at 16:23
  • 1
    you could redefine the figure environment to be a center environment or a minipage or any other non floating thing. Floats are removed and re-inserted at a page break so having them in a document constrained to one page can't work but of course you can have the syntax of a figure environment. @Nobody-Knows-I-am-a-Dog Commented Nov 6, 2021 at 16:26
2

Not what you asked for, but I don't see the problem with standalone. All floats are implemented as [h], but where else would you put them? \parindent and \parskip are reset by minipage, as are the footnotes.

\documentclass[class=report]{standalone}
\usepackage{amsmath}

\usepackage{lipsum}% MWE only

\begin{document}
\begin{minipage}{5in}
  \parindent=1.5em
  \parskip=0pt

\chapter{Yadda Yadda}

\lipsum[1-2]

\begin{figure}
  \caption{A figure}
\end{figure}

\begin{itemize}
\item Firat
\item Second
\end{itemize}

\begin{table}
  \caption{A Table}
\end{table}

Test\footnote{This is a test}

\begin{equation}
  x=a
\end{equation}

\end{minipage}
\end{document}
3
  • Problems came with details. I have no parskip (tex.stackexchange.com/questions/144862 is a workaround). I have no \begin{equation} (there is a workaround). How would I sneak in my desired documentclass (there is an option for that in standalone). But how would I now sneak in the options for these classes (not sure about this) There is a ...just name the problem. Since I have some 1000 snippets working in a custom template class I got afraid of the number of issues which might pop up. But I will reconsider and make a larger test case . Commented Nov 6, 2021 at 15:01
  • Did a few more test cases. One issue is that there are case where a snippet renders quite differently inside of the document with article class than with standalone. The example uses enumitem and seems to be an instance of a known issue described in tex.stackexchange.com/questions/178539/… and tex.stackexchange.com/questions/542130/… I could solve this somehow but am afraid of similar situations. Commented Nov 7, 2021 at 15:41
  • Both linked cases have to do with the variable width, which is why we use a minipage. to supply a fixed width Commented Nov 7, 2021 at 16:13

You must log in to answer this question.

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