# HG changeset patch
# User J. H. Palmieri <palmieri@math.washington.edu>
# Date 1252098516 25200
# Node ID 6dcae7b42e3224bb52b506f51b0f1539c049883f
# Parent  76f8dec0779bc99474ecaffb9d30317dc1d8372a
change dollar signs to backticks

diff --git a/doc/common/conf.py b/doc/common/conf.py
--- a/doc/common/conf.py
+++ b/doc/common/conf.py
@@ -1,4 +1,4 @@
-import sys, os
+import sys, os, re
 SAGE_ROOT = os.environ['SAGE_ROOT']
 SAGE_DOC = os.path.join(SAGE_ROOT, 'devel/sage/doc')
 
@@ -269,8 +269,66 @@
     skip_nested = str(obj).find("sage.misc.misc") != -1 and name.find("MainClass.NestedClass") != -1
     return skip or skip_nested
         
+def process_dollars(app, what, name, obj, options, docstringlines):
+    r"""
+    Replace dollar signs with backticks.
+
+    More precisely, do a regular expression search.  Replace a plain
+    dollar sign ($) by a backtick (`).  Replace an escaped dollar sign
+    (\$) by a dollar sign ($).  Don't change a dollar sign preceded or
+    followed by a backtick (`$ or $`), because of strings like
+    "``$HOME``".  Don't make any changes on lines starting with
+    spaces, because those are indented and hence part of a block of
+    code or examples.
+
+    This also doesn't replaces dollar signs enclosed in curly braces,
+    to avoid nested math environments, such as ::
+
+      $f(n) = 0 \text{ if $n$ is prime}$
+
+    Thus the above line would get changed to
+
+      `f(n) = 0 \text{ if $n$ is prime}`
+    """
+    s = "\n".join(docstringlines)
+    if s.find("$") == -1:
+        return
+    # Indices will be a list of pairs of positions in s, to search between.
+    # If the following search has no matches, then indices will be (0, len(s)).
+    indices = [0]
+    # This searches for "$blah$" inside a pair of curly braces --
+    # don't change these, since they're probably coming from a nested
+    # math environment.  So for each match, search to the left of its
+    # start and to the right of its end, but not in between.
+    for m in re.finditer(r"{[^{}$]*\$([^{}$]*)\$[^{}$]*}", s):
+        indices[-1] = (indices[-1], m.start())
+        indices.append(m.end())
+    indices[-1] = (indices[-1], len(s))
+
+    # regular expression for $ (not \$, `$, $`, and only on a line
+    # with no leading whitespace).
+    dollar = re.compile(r"""^ # beginning of line
+                            ([^\s] # non whitespace
+                            .*?)? # non-greedy match any non-newline characters
+                            (?<!`|\\)\$(?!`) # $ with negative lookbehind and lookahead
+                            """, re.M | re.X)
+    # regular expression for \$
+    slashdollar = re.compile(r"\\\$")
+    for start, end in indices:
+        while dollar.search(s, start, end):
+            m = dollar.search(s, start, end)
+            s = s[:m.end()-1] + "`" + s[m.end():]
+        while slashdollar.search(s, start, end):
+            m = slashdollar.search(s, start, end)
+            s = s[:m.start()] + "$" + s[m.end():]
+    # now save results in docstringlines
+    lines = s.split("\n")
+    for i in range(len(lines)):
+        docstringlines[i] = lines[i]
+
 def setup(app):
     app.connect('autodoc-process-docstring', process_docstring_cython)
     app.connect('autodoc-process-docstring', process_directives)
     app.connect('autodoc-process-docstring', process_docstring_module_title)
+    app.connect('autodoc-process-docstring', process_dollars)
     app.connect('autodoc-skip-member', skip_NestedClass)
diff --git a/sage/combinat/crystals/tensor_product.py b/sage/combinat/crystals/tensor_product.py
--- a/sage/combinat/crystals/tensor_product.py
+++ b/sage/combinat/crystals/tensor_product.py
@@ -182,19 +182,19 @@
 
     .. math::
 
-    f_i(b \otimes b') = \begin{cases}
-    f_i(b) \otimes b' & \text{if $\varepsilon_i(b) \ge \varphi_i(b')$}\\
-    b \otimes f_i(b') & \text{otherwise}
-    \end{cases}
+      f_i(b \otimes b') = \begin{cases}
+      f_i(b) \otimes b' & \text{if $\varepsilon_i(b) \ge \varphi_i(b')$}\\
+      b \otimes f_i(b') & \text{otherwise}
+      \end{cases}
 
     and
 
     .. math::
 
-    e_i(b \otimes b') = \begin{cases}
-    b \otimes e_i(b') & \text{if $\varepsilon_i(b) \le \varphi_i(b')$}\\
-    e_i(b) \otimes b' & \text{otherwise.}
-    \end{cases}
+      e_i(b \otimes b') = \begin{cases}
+      b \otimes e_i(b') & \text{if $\varepsilon_i(b) \le \varphi_i(b')$}\\
+      e_i(b) \otimes b' & \text{otherwise.}
+      \end{cases}
 
     Note that this is the opposite of Kashiwara's convention for tensor
     products of crystals.
diff --git a/sage/combinat/integer_list.py b/sage/combinat/integer_list.py
--- a/sage/combinat/integer_list.py
+++ b/sage/combinat/integer_list.py
@@ -952,7 +952,7 @@
 
     def ceiling(self, i):
         """
-        Returns the maximum part that can appear in the $i^{th}
+        Returns the maximum part that can appear in the $i^{th}$
         position in any list produced.
 
         EXAMPLES::
diff --git a/sage/modular/arithgroup/congroup_gamma.py b/sage/modular/arithgroup/congroup_gamma.py
--- a/sage/modular/arithgroup/congroup_gamma.py
+++ b/sage/modular/arithgroup/congroup_gamma.py
@@ -97,7 +97,7 @@
         
         .. math::
         
-        \prod_{\substack{p \mid N \\ \text{$p$ prime}}}\left(p^{3e}-p^{3e-2}\right).
+          \prod_{\substack{p \mid N \\ \text{$p$ prime}}}\left(p^{3e}-p^{3e-2}\right).
         
         EXAMPLE::
             sage: [Gamma(n).index() for n in [1..19]]
diff --git a/sage/modular/modform/ambient_g0.py b/sage/modular/modform/ambient_g0.py
--- a/sage/modular/modform/ambient_g0.py
+++ b/sage/modular/modform/ambient_g0.py
@@ -31,8 +31,8 @@
     """
     def __init__(self, level, weight):
         r"""
-        Create a space of modular symbols for `\Gamma_0(N)` of given
-        weight defined over `\QQ`.
+        Create a space of modular symbols for $\Gamma_0(N)$ of given
+        weight defined over $\QQ$.
 
         EXAMPLES::
 
diff --git a/sage/modular/modform/numerical.py b/sage/modular/modform/numerical.py
--- a/sage/modular/modform/numerical.py
+++ b/sage/modular/modform/numerical.py
@@ -21,8 +21,7 @@
 
 class NumericalEigenforms(SageObject):
     """
-    numerical_eigenforms(group, weight=2, eps=1e-20,
-                         delta=1e-2, tp=[2,3,5])
+    numerical_eigenforms(group, weight=2, eps=1e-20, delta=1e-2, tp=[2,3,5])
 
     INPUT:
 
diff --git a/sage/rings/padics/padic_capped_relative_element.pyx b/sage/rings/padics/padic_capped_relative_element.pyx
--- a/sage/rings/padics/padic_capped_relative_element.pyx
+++ b/sage/rings/padics/padic_capped_relative_element.pyx
@@ -1567,15 +1567,15 @@
 
         if absprec is None, returns True if self and right are equal to the minimum of their precisions.
 
-        INPUT::
-        
-            - self -- a p-adic element
-            - right -- a p-addic element
-            - absprec -- an integer or None
-            
-        OUTPUT::
-        
-            - boolean -- whether self is equal to right (modulo $p^{\mbox{absprec}}$)
+        INPUT:
+
+        - self -- a p-adic element
+        - right -- a p-addic element
+        - absprec -- an integer or None
+
+        OUTPUT:
+
+        - boolean -- whether self is equal to right (modulo $p^{\mbox{absprec}}$)
 
         EXAMPLES::
 
@@ -2123,7 +2123,7 @@
 
     def residue(self, absprec=1):
         """
-        Reduces this element modulo $p^absprec$.
+        Reduces this element modulo $p^{\mbox{absprec}}$.
         
         INPUT::
         
diff --git a/sage/rings/padics/padic_fixed_mod_element.pyx b/sage/rings/padics/padic_fixed_mod_element.pyx
--- a/sage/rings/padics/padic_fixed_mod_element.pyx
+++ b/sage/rings/padics/padic_fixed_mod_element.pyx
@@ -1062,7 +1062,7 @@
 
     def residue(self, absprec=1):
         r"""
-        Reduces this mod $p^prec$
+        Reduces this mod $p^{\mbox{prec}}$
 
         INPUT::
         
diff --git a/sage/rings/padics/padic_generic.py b/sage/rings/padics/padic_generic.py
--- a/sage/rings/padics/padic_generic.py
+++ b/sage/rings/padics/padic_generic.py
@@ -380,7 +380,7 @@
 
     def teichmuller_system(self):
         r"""
-        Returns a set of teichmuller representatives for the invertible elements of $\Z / p\Z$.
+        Returns a set of teichmuller representatives for the invertible elements of $\ZZ / p\ZZ$.
 
         INPUT:
 
@@ -388,7 +388,7 @@
 
         OUTPUT:
 
-        - list of elements -- a list of teichmuller representatives for the invertible elements of $\Z / p\Z$
+        - list of elements -- a list of teichmuller representatives for the invertible elements of $\ZZ / p\ZZ$
 
         EXAMPLES::
 
diff --git a/sage/rings/padics/pow_computer.pyx b/sage/rings/padics/pow_computer.pyx
--- a/sage/rings/padics/pow_computer.pyx
+++ b/sage/rings/padics/pow_computer.pyx
@@ -534,8 +534,9 @@
 # To speed up the creation of PowComputers with the same m, we might eventually want to copy over data from an existing PowComputer.
 
 def PowComputer(m, cache_limit, prec_cap, in_field = False):
-    """
-    Returns a PowComputer that caches the values $1, m, m^2, \ldots, m^cache_limit$.
+    r"""
+    Returns a PowComputer that caches the values $1, m, m^2, \ldots, m^{C}$,
+    where $C$ is ``cache_limit``.
 
     Once you create a PowComputer, merely call it to get values out.
     
diff --git a/sage/rings/padics/pow_computer_ext.pyx b/sage/rings/padics/pow_computer_ext.pyx
--- a/sage/rings/padics/pow_computer_ext.pyx
+++ b/sage/rings/padics/pow_computer_ext.pyx
@@ -2415,8 +2415,9 @@
         return ZZ_pX_eis_shift_p(self, x, a, n, finalprec)
 
 def PowComputer_ext_maker(prime, cache_limit, prec_cap, ram_prec_cap, in_field, poly, prec_type = "small", ext_type = "u", shift_seed = None):
-    """
-    Returns a PowComputer that caches the values $1, prime, prime^2, \ldots, prime^cache_limit$.
+    r"""
+    Returns a PowComputer that caches the values $1, prime, prime^2, \ldots, prime^{C}$,
+    where $C$ is ``cache_limit``.
 
     Once you create a PowComputer, merely call it to get values out.
     You can input any integer, even if it's outside of the precomputed range.
diff --git a/sage/rings/padics/unramified_extension_generic.py b/sage/rings/padics/unramified_extension_generic.py
--- a/sage/rings/padics/unramified_extension_generic.py
+++ b/sage/rings/padics/unramified_extension_generic.py
@@ -261,7 +261,7 @@
 
     def has_pth_root(self):
         r"""
-        Returns whether or not $\Z_p$ has a primitive $p^{\mbox{th}}$ root of unity.
+        Returns whether or not $\ZZ_p$ has a primitive $p^{\mbox{th}}$ root of unity.
 
         Since adjoining a $p^{\mbox{th}}$ root of unity yields a
         totally ramified extension, self will contain one if and only
@@ -287,7 +287,7 @@
 
     def has_root_of_unity(self, n):
         """
-        Returns whether or not $\Z_p$ has a primitive $n^{\mbox{th}}$
+        Returns whether or not $\ZZ_p$ has a primitive $n^{\mbox{th}}$
         root of unity.
 
         INPUT::
diff --git a/sage/symbolic/expression.pyx b/sage/symbolic/expression.pyx
--- a/sage/symbolic/expression.pyx
+++ b/sage/symbolic/expression.pyx
@@ -4471,10 +4471,10 @@
         return new_Expression_from_GEx(self._parent, g_sinh(self._gobj))
 
     def cosh(self):
-        """
+        r"""
         Return cosh of self.
 
-        We have $\sinh(x) = (e^{x} + e^{-x})/2$. 
+        We have $\cosh(x) = (e^{x} + e^{-x})/2$. 
 
         EXAMPLES::
 
@@ -4516,7 +4516,7 @@
         return new_Expression_from_GEx(self._parent, g_cosh(self._gobj))
 
     def tanh(self):
-        """
+        r"""
         Return tanh of self.
 
         We have $\tanh(x) = \sinh(x) / \cosh(x)$. 
@@ -4889,7 +4889,8 @@
         gamma is a complex function such that gamma(n)
         equals factorial(n-1).
         
-        EXAMPLES:
+        EXAMPLES::
+
             sage: x = var('x')
             sage: x.lgamma()
             lgamma(x)
