def bibtex_width(string): r""" Determine the width of the given string, in relative units. >>> bibtex_width('') 0 >>> bibtex_width('abc') 1500 >>> bibtex_width('ab{c}') 2500 >>> bibtex_width(r"ab{\'c}") 1500 >>> bibtex_width(r"ab{\'c{}}") 1500 >>> bibtex_width(r"ab{\'c{}") 1500 >>> bibtex_width(r"ab{\'c{d}}") 2056 """ from pybtex.charwidths import charwidths width = 0 for token, brace_level in scan_bibtex_string(string): if brace_level == 1 and token.startswith('\\'): for char in token[2:]: if char not in '{}': width += charwidths.get(char, 0) width -= 1000 # two braces else: width += charwidths.get(token, 0) return width
def width(string): r""" Get the width of the typeset string, in relative units. Similar to BibTeX's width$, but does not care about any "special characters". >>> width('') 0 >>> width('abc') 1500 >>> width('ab{c}') 2500 >>> width(r"ab{\'c}") 3278 >>> width(r"ab{\'c{}}") 4278 >>> width(r"ab{\'c{}") 3778 >>> width(r"ab{\'c{d}}") 4834 """ from pybtex.charwidths import charwidths return sum(charwidths.get(char, 0) for char in string)