Module: PdfRenderer::Helpers::LatexHelper

Defined in:
lib/pdf_renderer/helpers/latex_helper.rb

Overview

Contains methods for escaping strings for LaTeX.

Constant Summary collapse

BS =
"\\\\"
BACKSLASH =
"#{BS}textbackslash{}"
HAT =
"#{BS}textasciicircum{}"
TILDE =
"#{BS}textasciitilde{}"

Instance Method Summary collapse

Instance Method Details

#latex_escape(s) ⇒ Object Also known as: l

Escapes the string, so it is suitable for LaTeX input. This method is aliased as l.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/pdf_renderer/helpers/latex_helper.rb', line 12

def latex_escape(s)
  quote_count = 0
  s.to_s.
    gsub(/([{}_$&%#])/, "__LATEX_HELPER_TEMPORARY_BACKSLASH_PLACEHOLDER__\\1").
    gsub(/\\/, BACKSLASH).
    gsub(/__LATEX_HELPER_TEMPORARY_BACKSLASH_PLACEHOLDER__/, BS).
    gsub(/\^/, HAT).
    gsub(/~/, TILDE).
    gsub(/"/) do
      quote_count += 1
      quote_count.odd? ? %{"`} : %{"'}
    end
end