Skip to content

Instantly share code, notes, and snippets.

@itkq
Last active October 27, 2016 14:37
Show Gist options
  • Save itkq/7d12990860ee2d8b9b32a437be278b6a to your computer and use it in GitHub Desktop.
Save itkq/7d12990860ee2d8b9b32a437be278b6a to your computer and use it in GitHub Desktop.
Latexit CLI
#!/usr/bin/env ruby
WORK_DIR = ENV['HOME']+'/tmp/latexit/'
FILE_NAME = 'tmp'
eq = `pbpaste`.strip
if eq.size > 100
puts 'Number of characters should be less than 100'
exit 1
end
latex=<<EOF
\\documentclass[10pt]{article}
\\usepackage{amssymb}
\\usepackage{amsmath}
\\usepackage{bm}
\\usepackage[utf8]{inputenc}
\\pagestyle{empty}
\\begin{document}
\\begin{align*}#{eq}
\\end{align*}
\\end{document}
EOF
File.write(WORK_DIR+FILE_NAME+'.tex', latex)
print 'platex ... '
res = `cd #{WORK_DIR} && \
platex -file-line-error -interaction nonstopmode #{FILE_NAME}.tex 2>&1`
unless $?.exitstatus == 0
puts 'failed'
puts res
exit 1
else
puts 'finished'
end
print 'dvipdfmx ... '
`cd #{WORK_DIR} && \
dvipdfmx #{FILE_NAME}.dvi`
unless $?.exitstatus == 0
puts 'failed'
exit 1
else
puts 'finished'
end
print 'pdfcrop ... '
`cd #{WORK_DIR} && \
pdfcrop #{FILE_NAME}.pdf`
unless $?.exitstatus == 0
puts 'failed'
exit 1
else
puts 'finished'
end
`cd #{WORK_DIR} && \
convert -density 400 -colorspace RGB #{FILE_NAME}-crop.pdf out.png && \
impbcopy out.png`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment