#!/usr/local/bin/ruby
##
# @file latexit.rb
# @author Mitch Richling <http://www.mitchr.me/>
# @Copyright Copyright 2010,2011 by Mitch Richling. All rights reserved.
# @Revision $Revision: 1.10 $
# @SCMdate $Date: 2011/06/25 22:18:21 $
# @brief Typeset a snippet of LaTeX, and display it.@EOL
# @Std Ruby 1.8
#
# Feed it LaTeX code via STDIN or as the first argument, and it will typeset it, crop it, and display it on the
# screen. The ideal tool chain on UNIX/Linux is pdflatex, pdfcrop, & xpdf. On MacOS X, xpdf may be replaced with
# Preview (via open). The best "old school" option is probably latex, dvips, & gv. Other options exist, but the
# results are not as nice. This code is best used via Emacs where a bit of LaTeX may be highlighted and then fed to
# this script for display.
possibleFlows = [[['pdflatex: pdflatex %FILE%.tex'],
["pdfcrop: /bin/sh -c 'pdfcrop %FILE%.pdf %FILE%-c.pdf'"],
['xpdf: xpdf -fullscreen -z page %FILE%-c.pdf',
'acroread: acroread %FILE%-c.pdf',
'open: open %FILE%-c.pdf']],
[['latex: latex %FILE%.tex'],
['dvips: dvips -E -o %FILE%.eps %FILE%.dvi'],
['gv: gv -presentation %FILE%.eps',
'ghostview: ghostview %FILE%.eps']],
[['pdflatex: pdflatex %FILE%.tex'],
['xpdf: xpdf -geometry 1000x300 -z page %FILE%.pdf',
'acroread: acroread %FILE%.pdf',
'open: open %FILE%.pdf']],
[['latex: latex %FILE%.tex'],
['xdvi: xdvi %FILE%.dvi']]
];
cleanFlow = Array.new
cleanFlowPath = Array.new
possibleFlows.each do |flow|
goodFlow = true
flow.each do |flowStep|
cleanFlowStep = nil
flowStep.each do |stepOption|
tmp = stepOption.match(/^([^:]+):/);
tool = tmp[1]
ENV['PATH'].split(':').each do |pathComp|
posBin = pathComp + '/' + tool
if (FileTest.exist?(posBin)) then
cleanFlowStep = posBin
break
end
end
if (cleanFlowStep) then
cleanFlow.push(stepOption)
cleanFlowPath.push(cleanFlowStep)
break
else
puts("WARNING: Results may be substandard. Missing tool: #{tool}")
end
end
if ( !(cleanFlowStep)) then
cleanFlow = Array.new
cleanFlowPath = Array.new
goodFlow = false
break
end
if !(goodFlow) then
break
end
end
if (goodFlow) then
break
end
end
if ( cleanFlow.empty?) then
puts("Could not find enough tools to work!")
else
fileName = 'temp'
Dir.chdir('/tmp/')
system("rm -f /tmp/#{fileName}.pdf")
system("rm -f /tmp/#{fileName}.tex")
system("rm -f /tmp/#{fileName}.aux")
system("rm -f /tmp/#{fileName}.log")
system("rm -f /tmp/#{fileName}.pdf")
system("rm -f /tmp/#{fileName}-p.pdf")
puts("GENERATING TEX...")
open("#{fileName}.tex", "w") do |file|
file.puts('\documentclass{article} \begin{document} \pagestyle{empty} \setlength{\parindent}{0pt} \setlength{\parskip}{10pt}')
if ( (ARGV[0].nil?) || (ARGV[0]=='-') ) then
inText = STDIN.each_line do |line|
file.puts(line.sub(/^\s*;+\s*/, '').sub(/^\s*#+\s*/, ''))
end
else
file.puts(ARGV[0])
end
file.puts('\end{document}')
end
# execute flow: Render, crop, and view
cleanFlow.each_with_index do |commandAndLine, index|
commandPath = cleanFlowPath[index]
command, commandLine = commandAndLine.split(/:\s+/, 2)
puts("Running: #{command.inspect}")
system(commandLine.gsub(command, commandPath).gsub('%FILE%', 'temp'))
end
end
Generated by GNU Enscript 1.6.5.2.