Ticket #14382: trac_14382_fix_latex_of_strings.patch

File trac_14382_fix_latex_of_strings.patch, 4.4 KB (added by novoselt, 8 years ago)

Use text-tt

  • sage/misc/latex.py

    # HG changeset patch
    # User Andrey Novoseltsev <novoselt@gmail.com>
    # Date 1364575466 21600
    # Node ID 135918186d1668b461b86886a0fdbd16fed0746f
    # Parent db803a1f856e6dc43a51b830efadf879ffeb3427
    Fix phantom spaces in LaTeX code of strings.
    
    diff --git a/sage/misc/latex.py b/sage/misc/latex.py
    a b  
    320320    # point, and/or ends with "?"
    321321    if re.match(r'(\+|-)?[0-9]*\.?[0-9]*\??$', x):
    322322        return x
    323     # Try to pick a delimiter.
    324     for delimiter in """|"'`#%&,.:;?!@_~^+-/\=<>()[]{}0123456789E""":
    325         if delimiter == "E":
    326             # x is too complicated
    327             return "\\begin{verbatim}\n%s\n\\end{verbatim}\n" % x
    328         if delimiter not in x:
    329             break
    330     wrapper = "\\verb" + delimiter + "%s" + delimiter
    331 
    332     # Strategy:
    333     # 1) break x into lines;
    334     # 2) wrap each line into \verb;
    335     # 3) assemble lines into a left-justified array.
    336    
    337     spacer = r"\phantom{\verb!%s!}"
    338     lines = []
    339     for line in x.split("\n"):
    340         parts = []
    341         nspaces = 0
    342         for part in line.split(" "):
    343             if part == "":
    344                 nspaces += 1
    345                 continue
    346             if nspaces > 0:
    347                 parts.append(spacer % ("x" * nspaces))
    348             nspaces = 1
    349             parts.append(wrapper % part)
    350         # There is also a bug with omitting empty lines in arrays...
    351         line = "".join(parts)
    352         if not line:   
    353             line = spacer % "x"
    354         lines.append(line)
    355     x = "\\\\\n".join(lines)
    356     # If the bugs were fixed, the above block could be just
    357     # x = "\\\\\n".join(wrapper % line for line in x.split("\n"))
     323    # Deal with special characters
     324    char_wrapper = r"{\char`\%s}"
     325    x = "".join(char_wrapper % c if c in "#$%&\^_{}~" else c for c in x)
     326    # Avoid grouping spaces into one
     327    x = x.replace(" ", "{ }")
     328    # And dashes too, since it causes issues for the command line...
     329    x = x.replace("-", "{-}")
     330    # Make it work in math mode, but look like typewriter
     331    line_wrapper = r"\text{\tt %s}"
     332    x = "\\\\\n".join(line_wrapper % line for line in x.split("\n"))       
     333    # Preserve line breaks
    358334    if "\n" in x:
    359335        x = "\\begin{array}{l}\n%s\n\\end{array}" % x
    360336    return x
     
    19461922        """
    19471923        # Get a regular LaTeX representation of x
    19481924        x = latex(x, combine_all=combine_all)
     1925           
     1926        # The following block, hopefully, can be removed in some future MathJax.
     1927        prefix = r"\text{\tt "
     1928        parts = x.split(prefix)
     1929        for i, part in enumerate(parts):
     1930            if i == 0:
     1931                continue    # Nothing to do with the head part
     1932            n = 1
     1933            for closing, c in enumerate(part):
     1934                if c == "{" and part[closing - 1] != "\\":
     1935                    n += 1
     1936                if c == "}" and part[closing - 1] != "\\":
     1937                    n -= 1
     1938                if n == 0:
     1939                    break
     1940            y = part[:closing]
     1941            for delimiter in """|"'`#%&,.:;?!@_~^+-/\=<>()[]{}0123456789E""":
     1942                if delimiter not in y:
     1943                    break
     1944            if delimiter == "E":
     1945                # y is too complicated
     1946                delimiter = "|"
     1947                y = "(complicated string)"
     1948            wrapper = r"\verb" + delimiter + "%s" + delimiter
     1949            spacer = r"\phantom{\verb!%s!}"
     1950            y = y.replace("{ }", " ").replace("{-}", "-")
     1951            for c in r"#$%&\^_{}~":
     1952                char_wrapper = r"{\char`\%s}" % c
     1953                y = y.replace(char_wrapper, c)
     1954            subparts = []
     1955            nspaces = 0
     1956            for subpart in y.split(" "):
     1957                if subpart == "":
     1958                    nspaces += 1
     1959                    continue
     1960                if nspaces > 0:
     1961                    subparts.append(spacer % ("x" * nspaces))
     1962                nspaces = 1
     1963                subparts.append(wrapper % subpart)
     1964            # There is a bug with omitting empty lines in arrays
     1965            if not y:   
     1966                subparts.append(spacer % "x")
     1967            subparts.append(part[closing + 1:])
     1968            parts[i] = "".join(subparts)
     1969        x = "".join(parts)
     1970
    19491971        # In MathJax:
    19501972        #   inline math: <script type="math/tex">...</script>
    19511973        #   displaymath: <script type="math/tex; mode=display">...</script>