Skip to content

Instantly share code, notes, and snippets.

@ilyakava
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ilyakava/e6fe7345f6f1c6c19e2d to your computer and use it in GitHub Desktop.
Save ilyakava/e6fe7345f6f1c6c19e2d to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# About:
# there are 2 common conversions that I find myself doing:
# markdown -> html for online blog posts with mathjax
# markdown -> pdf for pdfs via pandoc
# unfortunately, if the markdown contains LaTeX, then there are different ways to escape the
# delimiters, as well as the matrix new row symbol. http://docs.mathjax.org/en/latest/tex.html#tex-and-latex-math-delimiters
# usage: `tex2jax_delim true infile.md outfile.md`
# true means that regular latex delimiters ("$$" and "$") will be converted to
# the mathjax way of escaping ("\\\\[" and "\\\\(") for markdown that will be converted
# to html.
# false means that the mathjax way of escaping in markdown that will be converted to html
# will instead be escaped to regular latex. If you go this route, you might also want to do
# `pandoc -fmarkdown-implicit_figures paper.markdown -o test.pdf` when converting the markdown
# to pdf so that the alt text within image links is left off the same way that online
# markdown-to-html posts will not visibly render this
mark_to_pdf_str = ARGV[0]
PDF_TO_MARKDOWN = mark_to_pdf_str.match /true/i
infile = ARGV[1]
outfile = ARGV[2]
def single_dollars(line)
if PDF_TO_MARKDOWN
frags = line.split "$"
if frags.count % 2 == 1
# iterate through evens
frags.each_with_index.map { |f, i| i % 2 == 1 ? "\\\\(#{f}\\\\)" : f }.join('')
else
line
end
else
front = line.gsub "\\\\(", "$"
front.gsub "\\\\)", "$"
end
end
# unfortunately necessary because of the interference with single dollar
def double_dollars(line)
if PDF_TO_MARKDOWN
frags = line.split "$$"
if frags.count % 2 == 1
# iterate through evens
frags.each_with_index.map { |f, i| i % 2 == 1 ? "\\\\[#{f}\\\\]" : f }.join('')
else
line
end
else
front = line.gsub "\\\\[", "$$"
front.gsub "\\\\]", "$$"
end
end
def matrix(line)
frags = line.split "{bmatrix}"
if frags.count % 2 == 1
# iterate through evens
frags.each_with_index.map { |f, i| i % 2 == 1 ? "{bmatrix}#{matrix_new_row(f)}{bmatrix}" : f }.join('')
else
line
end
end
# new rows in matricies
def matrix_new_row(line)
choices = ['\\\\', "\\\\\\\\\\"]
split_on, join_with = (PDF_TO_MARKDOWN ? choices : choices.reverse)
frags = line.split split_on
if frags.count % 2 == 1
frags.join join_with
else
line
end
end
inf = File.open(infile, 'r')
contents = inf.read
out_string = matrix(single_dollars(double_dollars(contents)))
outf = File.open(outfile, 'w')
outf.puts out_string
inf.close
outf.close
puts "converted #{(PDF_TO_MARKDOWN ? 'pdf to markdown' : 'markdown to pdf')} latex syntax"
@ilyakava
Copy link
Author

curl https://gist.githubusercontent.com/ilyakava/e6fe7345f6f1c6c19e2d/raw/0c31c9d61a0c23238744edd2f6d024b805c902fa/tex2jax_delim > ~/bin/tex2jax_delim; chmod ugo+x ~/bin/tex2jax_delim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment