Exemplo n.º 1
0
def fix_latex_file(latex_file, pdf_output):
    def_re = re.compile(r"(\\newcommandx|\\global\\long\\def)(\\[a-zA-Z]+)")

    tmp = mkstemp()

    changed = False
    macros = []
    for line in open(latex_file, 'r').readlines():
        if not pdf_output and line.startswith("\\documentclass"):
            changed = True
            line += "\\PassOptionsToPackage{draft}{microtype}\n"
        else:
            match = def_re.match(line)
            if match != None:
                macroname = match.group(2)
                if macroname in macros:
                    definecmd = match.group(1)
                    if definecmd == "\\newcommandx":
                        changed = True
                        line = line.replace(definecmd, "\\renewcommandx")
                else:
                    macros.append(macroname)
        tmp.write(line)

    if changed:
        copyfileobj(tmp, open(latex_file, "wb"), 1)

    return changed
def color_pdf(latex_file, bg_color, fg_color):
    use_preview_pdf_re = re.compile("(\s*\\\\usepackage\[[^]]+)((pdftex|xetex)\]{preview})")

    tmp = mkstemp()
    
    fg = ""
    if fg_color != "0.000000,0.000000,0.000000":
        fg = '  \\AtBeginDocument{\\let\\oldpreview\\preview\\renewcommand\\preview{\\oldpreview\\color[rgb]{%s}}}\n' % (fg_color)
    
    success = 0
    try:
        for line in open(latex_file, 'r').readlines():
            match = use_preview_pdf_re.match(line)
            if match == None:
                tmp.write(line)
                continue
            success = 1
            tmp.write("  \\usepackage{color}\n" \
                  "  \\pagecolor[rgb]{%s}\n" \
                  "%s" \
                  "%s\n" \
                  % (bg_color, fg, match.group()))
            continue

    except:
        # Unable to open the file, but do nothing here because
        # the calling function will act on the value of 'success'.
        warning('Warning in color_pdf! Unable to open "%s"' % latex_file)
        warning(`sys.exc_type` + ',' + `sys.exc_value`)

    if success:
        copyfileobj(tmp, open(latex_file,"wb"), 1)

    return success
Exemplo n.º 3
0
def fix_latex_file(latex_file, pdf_output):
    def_re = re.compile(r"(\\newcommandx|\\global\\long\\def)(\\[a-zA-Z]+)")

    tmp = mkstemp()

    changed = False
    macros = []
    for line in open(latex_file, 'r').readlines():
        if not pdf_output and line.startswith("\\documentclass"):
            changed = True
            line += "\\PassOptionsToPackage{draft}{microtype}\n"
        else:
            match = def_re.match(line)
            if match != None:
                macroname = match.group(2)
                if macroname in macros:
                    definecmd = match.group(1)
                    if definecmd == "\\newcommandx":
                        changed = True
                        line = line.replace(definecmd, "\\renewcommandx")
                else:
                    macros.append(macroname)
        tmp.write(line)

    if changed:
        copyfileobj(tmp, open(latex_file,"wb"), 1)

    return changed
def crop_files(pnmcrop, basename):
    t = pipes.Template()
    t.append('%s -left' % pnmcrop, '--')
    t.append('%s -right' % pnmcrop, '--')

    for file in glob.glob("%s*.ppm" % basename):
        tmp = mkstemp()
        new = t.open(file, "r")
        copyfileobj(new, tmp)
        if not new.close():
            copyfileobj(tmp, open(file,"wb"), 1)
Exemplo n.º 5
0
def crop_files(pnmcrop, basename):
    t = pipes.Template()
    t.append('%s -left' % pnmcrop, '--')
    t.append('%s -right' % pnmcrop, '--')

    for file in glob.glob("%s*.ppm" % basename):
        tmp = mkstemp()
        new = t.open(file, "r")
        copyfileobj(new, tmp)
        if not new.close():
            copyfileobj(tmp, open(file, "wb"), 1)
Exemplo n.º 6
0
def legacy_latex_file(latex_file, fg_color, bg_color):
    use_preview_re = re.compile(r"\s*\\usepackage\[([^]]+)\]{preview}")
    fg_color_gr = make_texcolor(fg_color, True)
    bg_color_gr = make_texcolor(bg_color, True)

    tmp = mkstemp()

    success = 0
    try:
        f = open(latex_file, 'r')
    except:
        # Unable to open the file, but do nothing here because
        # the calling function will act on the value of 'success'.
        warning('Warning in legacy_latex_file! Unable to open "%s"' %
                latex_file)
        warning( ` sys.exc_type ` + ',' + ` sys.exc_value `)

    for line in f.readlines():
        if success:
            tmp.write(line)
            continue
        match = use_preview_re.match(line)
        if match == None:
            tmp.write(line)
            continue
        success = 1
        # Package order: color should be loaded before preview
        # Preview options: add the options lyx and tightpage
        tmp.write(r"""
\usepackage{color}
\definecolor{fg}{rgb}{%s}
\definecolor{bg}{rgb}{%s}
\pagecolor{bg}
\usepackage[%s,tightpage]{preview}
\makeatletter
\def\t@a{cmr}
\if\f@family\t@a
\IfFileExists{lmodern.sty}{\usepackage{lmodern}}{\usepackage{ae,aecompl}}
\fi
\g@addto@macro\preview{\begingroup\color{bg}\special{ps::clippath fill}\color{fg}}
\g@addto@macro\endpreview{\endgroup}
\makeatother
""" % (fg_color_gr, bg_color_gr, match.group(1)))

    if success:
        copyfileobj(tmp, open(latex_file, "wb"), 1)

    return success
Exemplo n.º 7
0
def legacy_latex_file(latex_file, fg_color, bg_color):
    use_preview_re = re.compile(r"\s*\\usepackage\[([^]]+)\]{preview}")
    fg_color_gr = make_texcolor(fg_color, True)
    bg_color_gr = make_texcolor(bg_color, True)

    tmp = mkstemp()

    success = 0
    try:
        f = open(latex_file, 'r')
    except:
        # Unable to open the file, but do nothing here because
        # the calling function will act on the value of 'success'.
        warning('Warning in legacy_latex_file! Unable to open "%s"' % latex_file)
        warning(`sys.exc_type` + ',' + `sys.exc_value`)

    for line in f.readlines():
        if success:
            tmp.write(line)
            continue
        match = use_preview_re.match(line)
        if match == None:
            tmp.write(line)
            continue
        success = 1
        # Package order: color should be loaded before preview
        # Preview options: add the options lyx and tightpage
        tmp.write(r"""
\usepackage{color}
\definecolor{fg}{rgb}{%s}
\definecolor{bg}{rgb}{%s}
\pagecolor{bg}
\usepackage[%s,tightpage]{preview}
\makeatletter
\def\t@a{cmr}
\if\f@family\t@a
\IfFileExists{lmodern.sty}{\usepackage{lmodern}}{\usepackage{ae,aecompl}}
\fi
\g@addto@macro\preview{\begingroup\color{bg}\special{ps::clippath fill}\color{fg}}
\g@addto@macro\endpreview{\endgroup}
\makeatother
""" % (fg_color_gr, bg_color_gr, match.group(1)))

    if success:
        copyfileobj(tmp, open(latex_file,"wb"), 1)

    return success
Exemplo n.º 8
0
def fix_latex_file(latex_file):
    documentclass_re = re.compile("(\\\\documentclass\[)(1[012]pt,?)(.+)")

    tmp = mkstemp()

    changed = 0
    for line in open(latex_file, 'r').readlines():
        match = documentclass_re.match(line)
        if match == None:
            tmp.write(line)
            continue

        changed = 1
        tmp.write("%s%s\n" % (match.group(1), match.group(3)))

    if changed:
        copyfileobj(tmp, open(latex_file,"wb"), 1)

    return changed
Exemplo n.º 9
0
def fix_latex_file(latex_file):
    documentclass_re = re.compile("(\\\\documentclass\[)(1[012]pt,?)(.+)")

    tmp = mkstemp()

    changed = 0
    for line in open(latex_file, 'r').readlines():
        match = documentclass_re.match(line)
        if match == None:
            tmp.write(line)
            continue

        changed = 1
        tmp.write("%s%s\n" % (match.group(1), match.group(3)))

    if changed:
        copyfileobj(tmp, open(latex_file,"wb"), 1)

    return changed
def legacy_latex_file(latex_file, fg_color, bg_color, bg_color_gr):
    use_preview_dvi_re = re.compile("(\s*\\\\usepackage\[[^]]+)(dvips\]{preview})")
    use_preview_pdf_re = re.compile("(\s*\\\\usepackage\[[^]]+)(pdftex\]{preview})")

    tmp = mkstemp()

    success = 0
    try:
        for line in open(latex_file, 'r').readlines():
            match = use_preview_dvi_re.match(line)
            if match == None:
                match = use_preview_pdf_re.match(line)
                if match == None:
                    tmp.write(line)
                    continue
                success = 1
                tmp.write("  \\usepackage{color}\n" \
                      "  \\pagecolor[rgb]{%s}\n" \
                      "%s\n" \
                      % (bg_color_gr, match.group()))
                continue

            success = 1
            tmp.write("%stightpage,%s\n" \
                      "  \\AtBeginDocument{\\AtBeginDvi{%%\n" \
                      "  \\special{!userdict begin/bop-hook{//bop-hook exec\n" \
                      "  <%s%s>{255 div}forall setrgbcolor\n" \
                      "  clippath fill setrgbcolor}bind def end}}}\n" \
                      % (match.group(1), match.group(2), fg_color, bg_color))

    except:
        # Unable to open the file, but do nothing here because
        # the calling function will act on the value of 'success'.
        warning('Warning in legacy_latex_file! Unable to open "%s"' % latex_file)
        warning(`sys.exc_type` + ',' + `sys.exc_value`)

    if success:
        copyfileobj(tmp, open(latex_file,"wb"), 1)

    return success