       depths = []
       start = null
       labels = {}
+      links = {}
       messages = []
       figurecount = 0
       r = /\\(sub)*section\s*{((?:[^{}]|{[^{}]*})*)}|\\bibliography/g

            for label in graphic.labels
              figures[label] = figure

  formatLabel = (label) ->
    "``#{label.replace /^fig:/, ''}''"


        message.body = message.body
        .replace /\\ref\s*{([^{}]*)}/g, (match, ref) ->
          if ref of labels
            if labels[ref] of links
              "\\href{#{links[labels[ref]]}}{#{labels[ref]}}"
            else
              console.warn "\\ref{#{ref}} used before \\label{#{ref}} defined, so no link to the corresponding message; you might add it manually"
              labels[ref]
          else if ref of figures
            attach.push figures[ref]
            #formatLabel figures[ref].labels[figures[p1].labels.length-1]
