#11498: Improve LaTeXing of strings
-----------------------------+----------------------------------------------
   Reporter:  novoselt       |          Owner:  jason, mpatel, was
       Type:  enhancement    |         Status:  needs_review      
   Priority:  major          |      Milestone:  sage-4.7.1        
  Component:  notebook       |       Keywords:  sd31              
Work_issues:                 |       Upstream:  N/A               
   Reviewer:  John Palmieri  |         Author:  Andrey Novoseltsev
     Merged:                 |   Dependencies:                    
-----------------------------+----------------------------------------------

Comment(by jhpalmieri):

 I happened to take a look at the ticket, so I saw the new patch; mine is
 based on that one.

 The documentation for regular expressions in Python is
 [http://docs.python.org/library/re.html here].  In the code
 {{{
 if s.find('\\verb') != -1:
     import re
     s = re.sub("\\\\verb(.)(.*?)\\1", "\\2", s)
     s = s.replace("\\phantom{x}", " ")
 }}}
 the second and third lines are the ones involving regular expressions.
 Line 3 is a search-and-replace command: it searches the argument s for a
 pattern {{{\verb}}} followed by a single character (that's the {{{.}}}),
 and putting that character in parentheses allows you to refer to it later
 as {{{\1}}}.  Then there are any number of characters (that's the
 {{{.*?}}}, also enclosed in parentheses, so you can refer to it later as
 {{{\2}}}), followed by whatever the first character was (that's the
 {{{\\1}}}).  It replaces all of this with the string in the second set of
 parentheses, {{{\2}}}.

 There is one subtlety in this: the standard way to refer to any string of
 characters is {{{.*}}}, but the problem here is that in a string like
 {{{
 \verb|a|, \verb|b|
 }}}
 if we used {{{.*}}}, then the string \2 would match "a|, \verb|b" --
 everything between the first "|" and the last "|": by default, regular
 expression matches use the longest match possible.  Using {{{.*?}}}
 instead of {{{.*}}} tells it to match the shortest string possible
 instead, so it does two replacements: first \2 matches "a", and then it
 matches "b", turning this into
 {{{
 a, b
 }}}

-- 
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/11498#comment:8>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica, 
and MATLAB

-- 
You received this message because you are subscribed to the Google Groups 
"sage-trac" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sage-trac?hl=en.

Reply via email to