4

I use the preview package to generate some pictures from the equation environment. I need the width and height of each picture. I guess the preview package does some black magic and, at some point, those values must be known (?). Is it possible to save them, sequentially, in a text file?

\documentclass{article}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{equation}
\begin{document}
\begin{equation}
  x^2 = 2
\end{equation}
\begin{equation}
  \int_0^x \sin t\, dt = 0
\end{equation}
\end{document}
cjorssen
  • 10,032
  • 4
  • 36
  • 126

1 Answers1

3

Digging into preview.sty documented code, I found that adding some material to the \pr@ship@end hook is the way to do it (what I want is nearly the same as the auctex option does). The dimensions I was looking for are those of \pr@box.

Since I need the dimensions to be in centimeter, I used a conversion macro given in an other answer.

\documentclass{article}
\usepackage{amsmath}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{equation*}
\makeatletter
% Conversion utility (https://tex.stackexchange.com/a/37317/8425)
\begingroup
  \catcode `P=12  % digits and punct. catcode
  \catcode `T=12  % digits and punct. catcode
  \lowercase{%
  \def\x{\def\rem@pt##1.##2PT{##1\ifnum##2>\z@.##2\fi}}}
     \expandafter\endgroup\x%
\def\strip@pt{\expandafter\rem@pt\the}
\def\convertto#1#2{\strip@pt\dimexpr #2*65536/\number\dimexpr 1#1\relax\relax}
% Answer to the question
\newwrite\file
\immediate\openout\file=snippet-list.txt
\g@addto@macro\pr@ship@end{%
  \immediate\write\file{%
    \convertto{cm}{\the\dimexpr\ht\pr@box+\dp\pr@box\relax}
    \convertto{cm}{\the\wd\pr@box}}}
\begin{document}
Test
\begin{equation*}
  x^2 = 2
\end{equation*}
Test
\begin{equation*}
  \int x^2\, dx = \frac{x^3}{2}
\end{equation*}
\closeout\file
\end{document}

I get a snippet-list.txt with

0.42175 12.12537
0.86165 12.12537
cjorssen
  • 10,032
  • 4
  • 36
  • 126