Skip to content

Instantly share code, notes, and snippets.

@caffeineshock
Created April 28, 2012 21:44
Show Gist options
  • Save caffeineshock/2522314 to your computer and use it in GitHub Desktop.
Save caffeineshock/2522314 to your computer and use it in GitHub Desktop.
LaTeX Building Rakefile
# encoding: utf-8
def config
# in this Rakefile we can allow that
$config ||= Hash.new
end
# main TeX file without extension
config['main'] = 'thesis'
# TeX command to invoke: xelatex, pdflatex, etc
config['latex'] = 'xelatex'
# Force the use of options with the latex command
config['latex_options'] = ['-shell-escape']
# BiBTeX command to invoke, or nil if it is not necessary
config['bibtex'] = 'bibtex'
##############################################################################
# This Rakefile is written to simplify the compiling of
# LaTeX documents.
#
# v0.2.0
# Use `pdflatex` and reinvent the `config` method.
#
# v0.1.1
# Published as GitHub Gist, again.
#
# v0.1.0
# Code cleanup. Using `xelatex' as default command.
#
# v0.0.1
# Initial release.
##############################################################################
task :default => [ :pdf ]
desc 'Open the output PDF file'
task :view do
unless Rake::Win32.windows?
invoke 'xdg-open', "#{config['main']}.pdf"
else
raise NotImplementedError
end
end
desc 'Build PDF'
task :pdf => [ :info, :clean ] do
latex '-draftmode', '-halt-on-error', "#{config['main']}.tex"
if config['bibtex']
bibtex config['main']
latex '-draftmode', '-halt-on-error', "#{config['main']}.tex"
end
latex '-halt-on-error', "#{config['main']}.tex"
end
namespace :pdf do
desc 'Build PDF in Draft Mode'
task :draft do
latex '-draftmode', '-halt-on-error', "#{config['main']}.tex"
end
end
desc 'Cleanup'
task :clean do
cleaner = proc { |path| rm_f(path) }
[ 'aux', 'bbl', 'blg', 'pdf', 'toc', 'nav', 'lo*', 'lpc', 'up*', 'out' ].each do |res|
Dir.glob("*.#{res}", &cleaner)
end
end
desc 'Print configuration'
task :info do
puts "Configuration: #{config.inspect}"
has_latex = latex '--version' rescue false
raise 'LaTeX is not installed' unless has_latex
if config['bibtex']
has_bibtex = bibtex '--version' rescue false
raise 'BiBTeX is not installed' unless has_bibtex
end
end
def invoke(command, *arguments)
args = arguments.flatten.map { |arg| "'#{arg}'" }.join(' ')
sh "#{command} #{args}"
end
def latex(*args)
raise unless config['latex']
args.unshift(config['latex_options'])
invoke(config['latex'], args)
end
def bibtex(*args)
raise ArgumentError unless config['bibtex']
invoke(config['bibtex'], args)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment