     #  after: sanitized
   sanitized
 
+formatEither = (isTitle, format, text, leaveTeX = false) ->
+  ## LaTeX format is special because it does its own math preprocessing at a
+  ## specific time during its formatting.  Other formats don't touch math.
+  if format == 'latex'
+    [text, math] = formats[format] text, isTitle
+  else
+    [text, math] = preprocessKaTeX text unless leaveTeX
+    if format of formats
+      text = formats[format] text, isTitle
+    else
+      console.warn "Unrecognized format '#{format}'"
+  ## Remove surrounding <P> block caused by Markdown and LaTeX formatters.
+  if isTitle
+    text = text
+    .replace /^\s*<P>\s*/i, ''
+    .replace /\s*<\/P>\s*$/i, ''
+  if leaveTeX
+    text = putMathBack text, math if format == 'latex'
+  else
+    text = postprocessKaTeX text, math
+  sanitize postprocessCoauthorLinks text
+
 @formatBody = (format, body, leaveTeX = false) ->
-  [body, math] = preprocessKaTeX body unless leaveTeX or format == 'latex'
-  if format of formats
-    body = formats[format] body, false
-  else
-    console.warn "Unrecognized format '#{format}'"
-  body = postprocessKaTeX body, math unless leaveTeX
-  sanitize postprocessCoauthorLinks body
+  formatEither false, format, body, leaveTeX
 
 @formatTitle = (format, title, leaveTeX = false) ->
-  [title, math] = preprocessKaTeX title unless leaveTeX or format == 'latex'
-  if format of formats
-    title = formats[format] title, true
-  else
-    console.warn "Unrecognized format '#{format}'"
-  ## Remove surrounding <P> block caused by Markdown and LaTeX formatters.
-  title = title
-  .replace /^\s*<P>\s*/i, ''
-  .replace /\s*<\/P>\s*$/i, ''
-  title = postprocessKaTeX title, math unless leaveTeX
-  sanitize postprocessCoauthorLinks title
+  formatEither true, format, title, leaveTeX
 
 @formatBadFile = (fileId) ->
   """<i class="bad-file">&lt;unknown file with ID #{fileId}&gt;</i>"""

  .replace /\\subsubsection\s*\*?\s*{((?:[^{}]|{[^{}]*})*)}/g, '<h4>$1</h4>'
  .replace /\\paragraph\s*\*?\s*{((?:[^{}]|{[^{}]*})*)}\s*/g, '<p><b>$1</b> '
  .replace /\\footnote\s*{((?:[^{}]|{[^{}]*})*)}/g, '[$1]'
  .replace /\\includegraphics\s*{([^{}]*)}/g, '<img src="$1">'
  .replace /\\begin\s*{(problem|theorem|conjecture|lemma|corollary)}/g, (m, p1) -> "<blockquote><b>#{s.capitalize p1}:</b> "
  .replace /\\end\s*{(problem|theorem|conjecture|lemma|corollary)}/g, '</blockquote>'
  .replace /\\begin\s*{(proof|pf)}/g, '<b>Proof:</b> '

  .replace /\n\n+/g, '\n<p>\n'
  .replace /<p>\s*(<h[1-9]>)/g, '$1'
  .replace /\[DOUBLEBACKSLASH\]/g, '\\\\'
  [tex, math]

@formats =
  markdown: (text, title) ->
