         message.body = message.body
         .replace /\\ref\s*{([^{}]*)}/g, (match, ref) ->
           if ref of labels
-            labels[ref]
+            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]

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

        if start?
          now = new Date
          body = tex[start...match.index]
          body = body.replace /\\label{([^{}]*)}/g, (match, label) ->
            labels[label] = title2section title
            ''
          console.log "Importing '#{title}'"
          messages.push
