Example #1
0
def len_as_printed(s, format='latex'):
    r"""
    Returns the length of s, as it will appear after being math_jax'ed
    """
    lenq = 1
    lendig = 1
    lenpm = 1.5
    lenpar = 0.5
    lenexp = 0.75
    lensub = 0.75
    ## remove all html first since it is not displayed
    ss = re.sub("<[^>]*>", "", s)
    logger.debug("ss=%s" % ss)
    ss = re.sub(" ", "", ss)  # remove white-space
    ss = re.sub("\*", "", ss)  # remove *
    num_exp = s.count("^")  # count number of exponents
    exps = re.findall("\^{?(\d*)", s)  # a list of all exponents
    sexps = "".join(exps)
    num_subs = s.count("_")  # count number of exponents
    subs = re.findall("_{?(\d*)", s)  # a list of all  subscripts
    ssubs = "".join(subs)
    ss = re.sub("\^{?(\d*)}?", "", ss)  # remove exponenents
    logger.debug(join([ss, ssubs, sexps]))
    tot_len = (ss.count(")") + ss.count("(")) * lenpar
    tot_len += ss.count("q") * lenq
    tot_len += len(re.findall("\d", s)) * lendig
    tot_len += len(re.findall("\w", s)) * lenq
    tot_len += (s.count("+") + s.count("-")) * lenpm
    tot_len += num_subs * lensub
    tot_len += num_exp * lenexp
    #
    #tot_len = len(ss)+ceil((len(ssubs)+len(sexps))*0.67)
    return tot_len
Example #2
0
def len_as_printed(s,format='latex'):
    r"""
    Returns the length of s, as it will appear after being math_jax'ed
    """
    lenq=1
    lendig=1
    lenpm=1.5
    lenpar=0.5
    lenexp=0.75
    lensub=0.75
    ## remove all html first since it is not displayed
    ss = re.sub("<[^>]*>","",s)
    logger.debug("ss=%s" % ss)
    ss = re.sub(" ","",ss)    # remove white-space
    ss = re.sub("\*","",ss)    # remove *
    num_exp = s.count("^")    # count number of exponents
    exps = re.findall("\^{?(\d*)",s) # a list of all exponents
    sexps = "".join(exps)
    num_subs = s.count("_")    # count number of exponents
    subs = re.findall("_{?(\d*)",s) # a list of all  subscripts
    ssubs = "".join(subs)
    ss = re.sub("\^{?(\d*)}?","",ss)  # remove exponenents
    logger.debug(join([ss,ssubs,sexps]))
    tot_len=(ss.count(")")+ss.count("("))*lenpar
    tot_len+=ss.count("q")*lenq
    tot_len+=len(re.findall("\d",s))*lendig
    tot_len+=len(re.findall("\w",s))*lenq
    tot_len+=(s.count("+")+s.count("-"))*lenpm
    tot_len+=num_subs*lensub
    tot_len+=num_exp*lenexp
    #
    #tot_len = len(ss)+ceil((len(ssubs)+len(sexps))*0.67)
    return tot_len
def break_line_at(s, brpt=20):
    r"""
    Breaks a line containing math 'smartly' at brpt characters.
    With smartly we mean that we break at + or - but keep brackets
    together
    """
    sl = list()
    stmp = ''
    left_par = 0
    #wmf_logger.debug('Break at line, Input ={0}'.format(s))
    for i in range(len(s)):
        if s[i] == '(':  # go to the matching case
            left_par = 1
        elif s[i] == ')' and left_par == 1:
            left_par = 0
        if left_par == 0 and (s[i] == '+' or s[i] == '-'):
            sl.append(stmp)
            stmp = ''
        stmp = stmp + s[i]
        if i == len(s) - 1:
            sl.append(stmp)
    wmf_logger.debug('sl={0}'.format(sl))

    # sl now contains a split  e.g. into terms in the q-expansion
    # we now have to join as many as fits on the line
    res = list()
    stmp = ''
    for j in range(len(sl)):
        l = len_as_printed(stmp) + len_as_printed(sl[j])
        #wmf_logger.debug("l={0}".format(l))
        if l < brpt:
            stmp = join([stmp, sl[j]])
        else:
            res.append(stmp)
            stmp = sl[j]
        if j == len(sl) - 1:
            res.append(stmp)
    return res