module MaRuKu::Out::Latex

Functions for exporting to Latex

Constants

ENTITY_TABLE
SAFE_CHARS
TexHeaders
XML_TABLE

The following is a conversion chart for html elements, courtesy of text2html

Public Class Methods

init_entity_table() click to toggle source

create hash @@entity_to_latex

# File lib/maruku/output/to_latex_entities.rb, line 69
        def Latex.init_entity_table
#               $stderr.write "Creating entity table.."
#               $stderr.flush
                doc = Document.new XML_TABLE
                doc.elements.each("//char") do |c| 
                        num =  c.attributes['num'].to_i
                        name =  c.attributes['name']
                        package =  c.attributes['package']
                        
                        convert =  c.attributes['convertTo']
                        convert.gsub!(%r@DOUBLEQUOT/,'"')
                        convert.gsub!(%r@QUOT/,"'")
                        convert.gsub!(%r@GT/,">")
                        convert.gsub!(%r@LT/,"<")
                        convert.gsub!(%r@AMP/,"&")
                        convert.freeze
                        
                        e = LatexEntity.new
                        e.html_num = num
                        e.html_entity = name
                        e.latex_string = convert
                        e.latex_packages = package ? package.split : []
                        
                        ENTITY_TABLE[num] = e
                        ENTITY_TABLE[name] = e
                end
#               $stderr.puts "..done."
        end
need_entity_table() click to toggle source
# File lib/maruku/output/to_latex_entities.rb, line 64
def Latex.need_entity_table
        Latex.init_entity_table if ENTITY_TABLE.empty?
end

Public Instance Methods

array_to_latex(array, join_char='') click to toggle source
# File lib/maruku/output/to_latex.rb, line 554
def array_to_latex(array, join_char='')
        e = []
        array.each do |c|
                method = c.kind_of?(MDElement) ? 
                   "to_latex_#{c.node_type}" : "to_latex"
                
                if not c.respond_to?(method)
        #             raise "Object does not answer to #{method}: #{c.class} #{c.inspect[0,100]}"
                        next
                end
                
                h =  c.send(method)
                
                if h.nil?
                        raise "Nil html for #{c.inspect} created with method #{method}"
                end
                
                if h.kind_of?Array
                        e = e + h
                else
                        e << h
                end
        end
        
        # puts a space after commands if needed
        # e.each_index do |i|
        #     if e[i] =~ /\\\w+\s*$/ # command
        #             if (s=e[i+1]) && s[0] == ?\ # space
        #                     e[i]  = e[i] + "\\ "
        #             end
        #     end
        # end
        
        e.join(join_char)
end
children_to_latex() click to toggle source

Convert each child to html

# File lib/maruku/output/to_latex.rb, line 550
def children_to_latex
        array_to_latex(@children)
end
latex_color(s, command='color') click to toggle source

Title: Input format for colors Output: latex, html Related: code_background_color

Admissible formats:

green
#abc
#aabbcc

# color[named]{name}

# color[rgb]{1,0.2,0.3}

# File lib/maruku/output/to_latex.rb, line 176
def latex_color(s, command='color')
        if s =~ %r^\#(\w\w)(\w\w)(\w\w)$/
                r = $1.hex; g = $2.hex; b=$3.hex
                # convert from 0-255 to 0.0-1.0
                r = r / 255.0; g = g / 255.0; b = b / 255.0; 
                "\\#{command}[rgb]{%0.2f,%0.2f,%0.2f}" % [r,g,b]
        elsif s =~ %r^\#(\w)(\w)(\w)$/
                r = $1.hex; g = $2.hex; b=$3.hex
                # convert from 0-15 to 0.0-1.0
                r = r / 15.0; g = g / 15.0; b = b / 15.0; 
                "\\#{command}[rgb]{%0.2f,%0.2f,%0.2f}" % [r,g,b]
        else  
                "\\#{command}{#{s}}"
        end
end
latex_escape(source) click to toggle source

the ultimate escaping (is much better than using verb)

# File lib/maruku/output/to_latex.rb, line 344
def latex_escape(source)
        s=""; 
        
        source.each_byte do |b| 
                if b == \ \
                        s << '~'
                elsif SAFE_CHARS.include? b
                        s << b
                else
                        s += "\\char%d" % b 
                end
        end
        s
end
to_latex_abbr() click to toggle source
# File lib/maruku/output/to_latex.rb, line 500
def to_latex_abbr
        children_to_latex
end
to_latex_cell() click to toggle source
# File lib/maruku/output/to_latex.rb, line 457
def to_latex_cell; children_to_latex end
to_latex_code() click to toggle source

Attribute: latex_use_listings Scope: document Output: latex Summary: Support for `listings` package. Related: code_show_spaces, code_background_color, lang, code_lang

If the `latex_use_listings` attribute is specified, then code block are rendered using the `listings` package. Otherwise, a standard `verbatim` environment is used.

  • If the `lang` attribute for the code block has been specified, it gets passed to the `listings` package using the `lstset` macro. The default lang for code blocks is specified through the `code_lang` attribute.

    \lstset{language=ruby}

    Please refer to the documentation of the `listings` package for supported languages.

    If a language is not supported, the `listings` package will emit a warning during the compilation. Just press enter and nothing wrong will happen.

  • If the `code_show_spaces` is specified, than spaces and tabs will be shown using the macro:

    \lstset{showspaces=true,showtabs=true}
  • The background color is given by `code_background_color`.

# File lib/maruku/output/to_latex.rb, line 251
def to_latex_code;
        raw_code = self.raw_code
        
        if get_setting(:latex_use_listings)
                @doc.latex_require_package('listings')
                        
                s = "\\lstset{columns=fixed,frame=shadowbox}"

                if get_setting(:code_show_spaces) 
                        s+= "\\lstset{showspaces=true,showtabs=true}\n"
                else
                        s+= "\\lstset{showspaces=false,showtabs=false}\n"
                end
                
                color = latex_color get_setting(:code_background_color)
                
                s+= "\\lstset{backgroundcolor=#{color}}\n" 
                
                s+= "\\lstset{basicstyle=\\ttfamily\\footnotesize}\n" 
                
                
                lang = self.attributes[:lang] || @doc.attributes[:code_lang] || '{}'
                if lang
                        s += "\\lstset{language=#{lang}}\n"
                end
                
                "#{s}\n\\begin{lstlisting}\n#{raw_code}\n\\end{lstlisting}"
        else
                "\\begin{verbatim}#{raw_code}\\end{verbatim}\n"
        end
end
to_latex_definition() click to toggle source
# File lib/maruku/output/to_latex.rb, line 483
def to_latex_definition                
        terms = self.terms
        definitions = self.definitions
        
        s = ""
        terms.each do |t|
                s +="\n\\item[#{t.children_to_latex}] "
        end

        definitions.each do |d|
                s += "#{d.children_to_latex} \n"
        end
        
        s
end
to_latex_definition_list() click to toggle source

Definition lists ###

# File lib/maruku/output/to_latex.rb, line 476
def to_latex_definition_list
        s = "\\begin{description}\n"
        s += children_to_latex
        s += "\\end{description}\n"
        s
end
to_latex_div() click to toggle source
# File lib/maruku/output/to_latex.rb, line 521
        def to_latex_div
                type = self.attributes[:class]
                id = self.attributes[:id]
                case type
                  when %r^un_(\w*)/
                        s = "\\begin{u#{$1}}"
#                       s += "[#{@children[0].send('children_to_latex')}]"
                        @children.delete_at(0)
                        s += "\n" + children_to_latex
                        s += "\\end{u#{$1}}\n"
                  when %r^num_(\w*)/
                        s = "\\begin{#{$1}}"
#                       s += "[#{@children[0].send('children_to_latex')}]"
                        @children.delete_at(0)
                        s += "\n\\label{#{id}}\\hypertarget{#{id}}{}\n"
                        s += children_to_latex
                        s += "\\end{#{$1}}\n"
                  when %r^proof/
                        s = "\\begin{proof}"
                        @children.delete_at(0)
                        s += "\n" + children_to_latex
                        s += "\\end{proof}\n"
                  else
                        s = children_to_latex
                end
                s
        end
to_latex_divref() click to toggle source
# File lib/maruku/ext/math/to_latex.rb, line 22
 def to_latex_divref
"\\ref{#{self.refid}}"
 end
to_latex_email_address() click to toggle source
# File lib/maruku/output/to_latex.rb, line 416
def to_latex_email_address
        email = self.email
        "\\href{mailto:#{email}}{#{latex_escape(email)}}"
end
to_latex_emphasis() click to toggle source
# File lib/maruku/output/to_latex.rb, line 327
def to_latex_emphasis
        "\\emph{#{children_to_latex}}"
end
to_latex_entity() click to toggle source
# File lib/maruku/output/to_latex_entities.rb, line 28
        def to_latex_entity 
                MaRuKu::Out::Latex.need_entity_table
                
                entity_name = self.entity_name
        
                entity = ENTITY_TABLE[entity_name]
                if not entity
                        maruku_error "I don't know how to translate entity '#{entity_name}' "+
                        "to LaTeX."
                        return ""
                end
                replace = entity.latex_string
                
                entity.latex_packages.each do |p|
                        @doc.latex_require_package p
                end
                
#               if replace =~ /^\\/
#                       replace = replace + " "
#               end
                
                if replace
                        return replace + "{}"
                else
                        tell_user "Cannot translate entity #{entity_name.inspect} to LaTeX."
                        return entity_name
                end
        end
to_latex_eqref() click to toggle source
# File lib/maruku/ext/math/to_latex.rb, line 18
def to_latex_eqref
        "\\eqref{#{self.eqid}}"
end
to_latex_equation() click to toggle source
# File lib/maruku/ext/math/to_latex.rb, line 9
def to_latex_equation
        if self.label
                l =  "\\label{#{self.label}}"
                "\\begin{equation}\n#{self.math.strip}\n#{l}\\end{equation}\n".fix_latex
        else
                "\\begin{displaymath}\n#{self.math.strip}\n\\end{displaymath}\n".fix_latex
        end
end
to_latex_footnote_reference() click to toggle source
# File lib/maruku/output/to_latex.rb, line 460
def to_latex_footnote_reference
        id = self.footnote_id
        f = @doc.footnotes[id]
        if f
                "\\footnote{#{f.children_to_latex.strip}} "
        else
                $stderr.puts "Could not find footnote '#{fid}'"
        end
end
to_latex_head_cell() click to toggle source
# File lib/maruku/output/to_latex.rb, line 456
def to_latex_head_cell; children_to_latex end
to_latex_header() click to toggle source
# File lib/maruku/output/to_latex.rb, line 289
def to_latex_header
        h = TexHeaders[self.level] || 'paragraph'
        
        title = children_to_latex
        if number = section_number
                title = number + title
        end
        
        if id = self.attributes[:id]
                # drop '#' at the beginning
                if id[0,1] == '#' then id = [1,id.size] end
                %Q{\\hypertarget{%s}{}\\%s*{{%s}}\\label{%s}\n\n} % [ id, h, title, id ]
        else
                %Q{\\%s*{%s}\n\n} % [ h, title]
        end
end
to_latex_hrule() click to toggle source
# File lib/maruku/output/to_latex.rb, line 154
def to_latex_hrule; "\n\\vspace{.5em} \\hrule \\vspace{.5em}\n" end
to_latex_image() click to toggle source
# File lib/maruku/output/to_latex.rb, line 504
        def to_latex_image
                id = self.ref_id
                ref = @doc.refs[id]
                if not ref
                        maruku_error "Could not find ref #{id.inspect} for image.\n"+
                                "Available are: #{@docs.refs.keys.inspect}"
#                       $stderr.puts "Could not find id = '#{id}'"
                        ""
                else
                        url = ref[:url]
                        $stderr.puts "Images not supported yet (#{url})"
                        # "{\\bf Images not supported yet (#{latex_escape(url)})}"
                        ""
                end

        end
to_latex_inline_code() click to toggle source
# File lib/maruku/output/to_latex.rb, line 359
def to_latex_inline_code; 
        source = self.raw_code
        
        # Convert to printable latex chars 
        s = latex_escape(source)
        
        color = get_setting(:code_background_color)
        colorspec = latex_color(color, 'colorbox')

        "{#{colorspec}{\\tt #{s}}}"
end
to_latex_inline_math() click to toggle source
# File lib/maruku/ext/math/to_latex.rb, line 5
def to_latex_inline_math
        "$#{self.math.strip}$".fix_latex
end
to_latex_li() click to toggle source
# File lib/maruku/output/to_latex.rb, line 317
def to_latex_li;        
        "\\item #{children_to_latex}\n"  
end
to_latex_li_span() click to toggle source
# File lib/maruku/output/to_latex.rb, line 320
def to_latex_li_span;
        "\\item #{children_to_latex}\n"
end
to_latex_linebreak() click to toggle source
# File lib/maruku/output/to_latex.rb, line 155
def to_latex_linebreak; "\\newline " end
to_latex_ol() click to toggle source
# File lib/maruku/output/to_latex.rb, line 316
def to_latex_ol;        wrap_as_environment('enumerate')               end
to_latex_paragraph() click to toggle source
# File lib/maruku/output/to_latex.rb, line 157
def to_latex_paragraph 
        children_to_latex+"\n\n"
end
to_latex_quote() click to toggle source
# File lib/maruku/output/to_latex.rb, line 315
def to_latex_quote;        wrap_as_environment('quote')               end
to_latex_raw_html() click to toggle source
# File lib/maruku/output/to_latex.rb, line 470
def to_latex_raw_html
        #'{\bf Raw HTML removed in latex version }'
        ""
end
to_latex_strong() click to toggle source
# File lib/maruku/output/to_latex.rb, line 324
def to_latex_strong
        "\\textbf{#{children_to_latex}}"
end
to_latex_table() click to toggle source
# File lib/maruku/output/to_latex.rb, line 422
def to_latex_table
        align = self.align
        num_columns = align.size

        head = @children.slice(0, num_columns)
        rows = []
        i = num_columns
        while i<@children.size
                rows << @children.slice(i, num_columns)
                i+=num_columns
        end
        
        h = {:center=>'c',:left=>'l',:right=>'r'}
        align_string = align.map{|a| h[a]}.join('|')
        
        s = "\\begin{tabular}{#{align_string}}\n"
                
                s += array_to_latex(head, '&') + "\\\\" +"\n"
                
                s += "\\hline \n"
                
                rows.each do |row|
                        s += array_to_latex(row, '&') + "\\\\" +"\n"
                end
                
        s += "\\end{tabular}"
        
        # puts table in its own paragraph
        s += "\n\n"
        
        s
end
to_latex_ul() click to toggle source
# File lib/maruku/output/to_latex.rb, line 307
def to_latex_ul;       
        if self.attributes[:toc]
                @doc.toc.to_latex
        else
                wrap_as_environment('itemize')
        end
end
wrap_as_environment(name) click to toggle source
# File lib/maruku/output/to_latex.rb, line 335
        def wrap_as_environment(name)
"\\begin{#{name}}%
#{children_to_latex}
\\end{#{name}}\n"       
        end
wrap_as_span(c) click to toggle source
# File lib/maruku/output/to_latex.rb, line 331
def wrap_as_span(c)
        "{#{c} #{children_to_latex}}"
end