Jose,
what's the recommended way to grab the stdout from an external program
(here dvipng)? dvipng outputs this to stdout:
This is dvipng 1.0 Copyright 2002-2004 Jan-?e Larsson
[1 depth=1 height=8] [2 depth=5 height=14] [3 depth=5 height=14] [4
depth=5 height=14] [5 depth=4 height=13] [6 depth=4 height=8] [7
depth=4 height=13] ...
and I want to manipulate that output and shove it into a .metrics
file:
Snippet 1 1 8
Snippet 2 5 14
Snippet 3 5 14
...
Any clues?
Current script attached. Run it so:
python lyxpreview2png.py 0lyxpreview.tex 128 000000 faf0e6
--
Angus
\batchmode
%% LyX 1.4.0cvs created this file. For more info, see http://www.lyx.org/.
%% Do not edit unless you really know what you are doing.
\makeatletter
[EMAIL PROTECTED]/home/angus/docs//}}
\makeatother
\documentclass[english]{article}
\usepackage{ae}
\usepackage{aecompl}
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}
\usepackage{a4wide}
\usepackage{graphicx}
\usepackage{amssymb}
\makeatletter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% LyX specific LaTeX commands.
%% Print a warning encased in an fbox.
\def\lyxstripprefix#1>{}
\newcommand{\warnNotFound}[1]{%
\def\lyxtempfilename{#1}%
\fbox{Could not find
\ttfamily\expandafter\lyxstripprefix\meaning\lyxtempfilename!}%
\typeout{Could not find \lyxtempfilename!}%
}
%% Input the file if it exists, using \input, else print a warning
\newcommand{\inputOrWarn}[1]{%
\IfFileExists{#1}{\input{#1}}{\warnNotFound{#1}}}
%% Bold symbol macro for standard LaTeX users
\providecommand{\boldsymbol}[1]{\mbox{\boldmath $#1$}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% User specified LaTeX commands.
%% Preempt the definition of \SetFigFont in the
%% xfig-generated pstex_t files by setting it to "do nothing".
\gdef\SetFigFont#1#2#3#4#5{}
\usepackage{babel}
\makeatother
\def\lyxlock{}
\newcommand{\Vector}[1]{\boldsymbol{#1}}
\newcommand{\dd}[2]{\frac{\partial#1}{\partial#2}}
\newcommand{\Peclet}{\textrm{Pe}}
\newcommand{\Nusselt}{\textrm{Nu}}
\usepackage[active,delayed,dvipng,showlabels,lyx]{preview}
\begin{document}
\begin{preview}
$x$
\end{preview}
\begin{preview}
$A=A\left(x,t\right)$
\end{preview}
\begin{preview}
$\delta\ell=\delta\ell\left(t\right)$
\end{preview}
\begin{preview}
$U_{x}\left(t\right)$
\end{preview}
\begin{preview}
$\Vector{V}=V_{x}\Vector{i}$
\end{preview}
\begin{preview}
$\rho$
\end{preview}
\begin{preview}
$\Vector{V}_{0}=V_{x0}\Vector{i}$
\end{preview}
\begin{preview}
$\rho_{0}$
\end{preview}
\begin{preview}
\begin{equation}
\dot{m}_{\mathrm{vol}}=\dot{m}_{1}-\dot{m}_{2}+S\delta T\label{eq:mass
conservation}\end{equation}
\end{preview}
\begin{preview}
$\delta T$
\end{preview}
\begin{preview}
$\dot{m}_{\mathrm{vol}}$
\end{preview}
\begin{preview}
$\dot{m}_{1}-\dot{m}_{2}$
\end{preview}
\begin{preview}
$S$
\end{preview}
\begin{preview}
$m_{\mathrm{vol}}=\rho_{0}A_{0}\delta\ell$
\end{preview}
\begin{preview}
$_{0}$
\end{preview}
\begin{preview}
\begin{eqnarray*}
\dot{m}_{\mathrm{vol}} & = &
\delta\ell\dd{}{t}\left(\rho_{0}A_{0}\right)+\rho_{0}A_{0}\dd{\delta\ell}{t}\\
& = & \delta\ell\dd{}{t}\left(\rho_{0}A_{0}\right)+2\rho_{0}A_{0}U_{x}\end{eqnarray*}
\end{preview}
\begin{preview}
$\partial\delta\ell/\partial t=2U_{x}$
\end{preview}
\begin{preview}
$\dot{m}_{2}=\rho_{2}A_{2}\left(V_{x2}-U_{x2}\right)$
\end{preview}
\begin{preview}
$\rho_{2}$
\end{preview}
\begin{preview}
$V_{x2}$
\end{preview}
\begin{preview}
$U_{x2}$
\end{preview}
\begin{preview}
$U_{x2}\Vector{i}$
\end{preview}
\begin{preview}
$\dot{m}_{1}=\rho_{1}A_{1}\left(V_{x1}+U_{x1}\right)$
\end{preview}
\begin{preview}
$-U_{x1}\Vector{i}$
\end{preview}
\begin{preview}
\begin{equation}
\delta\ell\dd{}{t}\left(\rho_{0}A_{0}\right)+2\rho_{0}A_{0}U_{x}+\rho_{2}A_{2}\left(V_{x2}-U_{x2}\right)-\rho_{1}A_{1}\left(V_{x1}+U_{x1}\right)=SA_{0}\delta\ell\label{eq:mass
balance}\end{equation}
\end{preview}
\begin{preview}
\begin{eqnarray*}
\rho_{1}A_{1}V_{x1} & = & \rho_{0}A_{0}V_{x0}-\frac{\delta\ell}{2}\dd{}{x}\left(\rho
V_{x}A\right)\\
\rho_{2}A_{2}V_{x2} & = & \rho_{0}A_{0}V_{x0}+\frac{\delta\ell}{2}\dd{}{x}\left(\rho
V_{x}A\right)\\
\therefore\rho_{2}A_{2}V_{x2}-\rho_{1}A_{1}V_{x1} & = & \delta\ell\dd{}{x}\left(\rho
V_{x}A\right)\end{eqnarray*}
\end{preview}
\begin{preview}
$U_{x}$
\end{preview}
\begin{preview}
\begin{eqnarray*}
\rho_{1}A_{1}U_{x} & = &
U_{x}\left(\rho_{0}A_{0}-\frac{\delta\ell}{2}\dd{}{x}\left(\rho A\right)\right)\\
\rho_{2}A_{2}U_{x} & = &
U_{x}\left(\rho_{0}A_{0}+\frac{\delta\ell}{2}\dd{}{x}\left(\rho A\right)\right)\\
\therefore-\left(\rho_{2}A_{2}+\rho_{1}A_{1}\right)U_{x} & = &
-2\rho_{0}A_{0}U_{x}\end{eqnarray*}
\end{preview}
\begin{preview}
\[
\delta\ell\dd{}{t}\left(\rho_{0}A_{0}\right)+2\rho_{0}A_{0}U_{x}+\delta\ell\dd{}{x}\left(\rho
V_{x}A\right)-2\rho_{0}A_{0}U_{x}=SA_{0}\delta\ell\]
\end{preview}
\begin{preview}
$\delta\ell$
\end{preview}
\begin{preview}
\begin{equation}
\dd{}{t}\left(\rho A\right)+\dd{}{x}\left(\rho
V_{x}A\right)=SA\label{eq:expansile}\end{equation}
\end{preview}
\begin{preview}
\begin{equation}
\dd{A}{t}+\dd{V_{x}A}{x}=0\label{eq:incompressible}\end{equation}
\end{preview}
\begin{preview}
$J_{x}=\rho V_{x}$
\end{preview}
\begin{preview}
$J_{x}=\rho V_{x}-D\partial\rho/\partial x$
\end{preview}
\begin{preview}
$D$
\end{preview}
\begin{preview}
\[
\dd{}{t}\left(\rho A\right)+\dd{}{x}\left(A\left(\rho
V_{x}-D\dd{\rho}{x}\right)\right)=SA\]
\end{preview}
\begin{preview}
\[
\rho\left(\dd{A}{t}+\dd{V_{x}A}{x}\right)+A\left(\dd{\rho}{t}+V_{x}\dd{\rho}{x}\right)-\dd{}{x}\left(AD\dd{\rho}{x}\right)=SA\]
\end{preview}
\begin{preview}
\begin{equation}
\dd{\rho}{t}+V_{x}\dd{\rho}{x}=\frac{1}{A}\dd{}{x}\left(AD\dd{\rho}{x}\right)+S\label{eq:ade}\end{equation}
\end{preview}
\begin{preview}
$L$
\end{preview}
\begin{preview}
$u_{0}$
\end{preview}
\begin{preview}
$L^{2}/\kappa$
\end{preview}
\begin{preview}
$\kappa$
\end{preview}
\begin{preview}
$D\rightarrow\kappa$
\end{preview}
\begin{preview}
\begin{equation}
\dd{c}{t^{*}}+\Peclet
V_{x}^{*}\dd{c}{x^{*}}=\frac{1}{A^{*}}\dd{}{x^{*}}\left(A^{*}\dd{c}{x^{*}}\right)+S^{*}\label{eq:ade
nd diffusion}\end{equation}
\end{preview}
\begin{preview}
$c=\rho/\rho_{0}$
\end{preview}
\begin{preview}
$t^{*}=tD/L^{2}$
\end{preview}
\begin{preview}
$V_{x}^{*}=V_{x}/u_{0}$
\end{preview}
\begin{preview}
$x^{*}=x/L$
\end{preview}
\begin{preview}
$A^{*}=A/L^{2}$
\end{preview}
\begin{preview}
$S^{*}=SL^{2}/D$
\end{preview}
\begin{preview}
$\Peclet=u_{0}L/D$
\end{preview}
\begin{preview}
$L/u_{0}$
\end{preview}
\begin{preview}
\begin{equation}
\dd{c}{t^{*}}+V_{x}^{*}\dd{c}{x^{*}}=\frac{1}{\Peclet}\frac{1}{A^{*}}\dd{}{x^{*}}\left(A^{*}\dd{c}{x^{*}}\right)+S^{*}\label{eq:ade
nd advection}\end{equation}
\end{preview}
\begin{preview}
$t^{*}=tu_{0}/L$
\end{preview}
\begin{preview}
$S^{*}=SL/u_{0}$
\end{preview}
\begin{preview}
$\Omega$
\end{preview}
\begin{preview}
$\Gamma$
\end{preview}
\begin{preview}
\[
c\left(x^{*},0\right)=c_{0}\left(x^{*}\right)\qquad\textrm{in }\overline{\Omega}\]
\end{preview}
\begin{preview}
$c_{0}\left(x^{*}\right)$
\end{preview}
\begin{preview}
$\overline{\Omega}=\Omega+\Gamma$
\end{preview}
\begin{preview}
\[
c=c_{D}\qquad\textrm{on }\Gamma_{D}\]
\end{preview}
\begin{preview}
\[
\dd{c}{x^{*}}+\Nusselt\left(c-\tilde{c}\right)=q^{*}\qquad\textrm{on }\Gamma_{N}\]
\end{preview}
\begin{preview}
$\Gamma=\Gamma_{D}+\Gamma_{N}$
\end{preview}
\begin{preview}
$\Gamma_{D}$
\end{preview}
\begin{preview}
$\Gamma_{N}$
\end{preview}
\begin{preview}
$c_{D}$
\end{preview}
\begin{preview}
$\tilde{c}$
\end{preview}
\begin{preview}
$\Nusselt\geq0$
\end{preview}
\begin{preview}
$q^{*}$
\end{preview}
\begin{preview}
$\Nusselt$
\end{preview}
\begin{preview}
$\Nusselt=q^{*}=0$
\end{preview}
\begin{preview}
\[
\dd{c}{x^{*}}-\Peclet\,
V_{x}^{*}c+\Nusselt\left(c-\tilde{c}\right)=q^{*}\qquad\textrm{on }\Gamma_{N}\]
\end{preview}
\begin{preview}
$q^{*}=0$
\end{preview}
\begin{preview}
$\Nusselt=O(1)$
\end{preview}
\end{document}
#! /usr/bin/env python
# file lyxpreview2png.py
# This file is part of LyX, the document processor.
# Licence details can be found in the file COPYING.
#
# author Angus Leeming
# with much advice from David Kastrup, [EMAIL PROTECTED]
#
# Full author contact details are available in file CREDITS
# This script takes a LaTeX file and generates a collection of
# png image files, one per previewed snippet.
# Pre-requisites:
# * A latex executable;
# * preview.sty;
# * dvipng.
# preview.sty and dvipng are part of the preview-latex project
# (http://preview-latex.sourceforge.net/).
# preview.sty can alternatively be obtained from
# CTAN/macros/latex/contrib/supported/preview.
# This script takes four arguments:
# TEXFILE: the name of the .tex file to be converted.
# DPI: a scale factor, passed to dvipng.
# FG_COLOR: the foreground color as a hexadecimal string, eg '000000'.
# BG_COLOR: the background color as a hexadecimal string, eg 'faf0e6'.
# Decomposing TEXFILE's name as DIR/BASE.tex, this script will,
# if executed successfully, leave in DIR:
# * a (possibly large) number of image files with names
# like BASE[0-9]+.png
# * a file BASE.metrics, containing info needed by LyX to position
# the images correctly on the screen.
import os, re, string, sys
# Pre-compiled regular expressions.
hexcolor_re = re.compile("^[0-9a-fA-F]{6}$")
latex_file_re = re.compile("\.tex$")
preview_re = re.compile("^Preview: [ST]")
preview_version_re = re.compile("^Package: preview")
def usage(prog_name):
return "Usage: %s <latex file> <dpi> <fg color> <bg color>\n"\
"\twhere the colors are hexadecimal strings, eg 'faf0e6'"\
% prog_name
def error(message):
sys.stderr.write(message + '\n')
sys.exit(1)
def find_exe(candidates, path):
for prog in candidates:
for directory in path:
full_path = os.path.join(directory, prog)
if os.access(full_path, os.X_OK):
return full_path
return None
def find_exe_or_terminate(candidates, path):
exe = find_exe(candidates, path)
if exe == None:
error("Unable to find executable from '%s'" % string.join(candidates))
return exe
def run_command(prog_name, prog_call):
pid = os.fork()
if pid == 0:
os.execvp(prog_name, prog_call)
error("%s failed" % prog_name)
pid, exit_status = os.waitpid(pid, 0)
return exit_status == 0
def make_texcolor(hexcolor):
# Test that the input string contains 6 hexadecimal chars.
if not hexcolor_re.match(hexcolor):
error("Cannot convert color '%s'" % hexcolor)
red = float(string.atoi(hexcolor[0:2], 16)) / 255.0
green = float(string.atoi(hexcolor[2:4], 16)) / 255.0
blue = float(string.atoi(hexcolor[4:6], 16)) / 255.0
return "rgb %f %f %f" % (red, green, blue)
def main(argv):
# Parse and manipulate the command line arguments.
if len(argv) != 5:
error(usage(argv[0]))
dir, latex_file = os.path.split(argv[1])
if len(dir) != 0:
os.chdir(dir)
dpi = int(argv[2])
fg_color = make_texcolor(argv[3])
bg_color = make_texcolor(argv[4])
# External programs used by the script.
path = string.split(os.getenv("PATH"), os.pathsep)
latex = find_exe_or_terminate(["pplatex", "latex2e", "latex"], path)
dvipng = find_exe_or_terminate(["dvipng"], path)
# Compile the latex file.
latex_call = [latex, latex_file]
if not run_command(latex, latex_call):
error("%s failed to compile %s" % (latex, latex_file))
# Run the dvi file through dvipng.
dvi_file = latex_file_re.sub(".dvi", latex_file)
dvipng_call = [dvipng, "-T", "tight", "-depth", "-height", \
"-D", "%d" % dpi, \
"-fg", fg_color, "-bg", bg_color, dvi_file]
if not run_command(dvipng, dvipng_call):
error("%s failed to generate images from %s" % (dvipng, dvi_file))
# Extract metrics info from the latex log file.
## log_file = latex_file_re.sub(".log", latex_file)
## metrics_file = latex_file_re.sub(".metrics", latex_file)
## if not extract_metrics_info(log_file, metrics_file):
## error("Failed to extract metrics info from %s" % log_file)
if __name__ == "__main__":
main(sys.argv)