Пример #1
0
    def info2latex(self):
        image_filename = os.path.join(os.path.dirname(__file__), 'kitten.jpg')

        geometry_options = {"tmargin": "1cm", "lmargin": "10cm"}
        doc = Document(geometry_options=geometry_options)

        with doc.create(Section('The simple stuff')):
            doc.append('Some regular text and some')
            doc.append(italic('italic text. '))
            doc.append('\nAlso some crazy characters: $&#{}')
            with doc.create(Subsection('Math that is incorrect')):
                doc.append(Math(data=['2*3', '=', 9]))

            with doc.create(Subsection('Table of something')):
                with doc.create(Tabular('rc|cl')) as table:
                    table.add_hline()
                    table.add_row((1, 2, 3, 4))
                    table.add_hline(1, 2)
                    table.add_empty_row()
                    table.add_row((4, 5, 6, 7))

        a = np.array([[100, 10, 20]]).T
        M = np.matrix([[2, 3, 4], [0, 0, 1], [0, 0, 2]])

        with doc.create(Section('The fancy stuff')):
            with doc.create(Subsection('Correct matrix equations')):
                doc.append(
                    Math(data=[Matrix(M),
                               Matrix(a), '=',
                               Matrix(M * a)]))

            with doc.create(Subsection('Alignat math environment')):
                with doc.create(Alignat(numbering=False, escape=False)) as agn:
                    agn.append(r'\frac{a}{b} &= 0 \\')
                    agn.extend([Matrix(M), Matrix(a), '&=', Matrix(M * a)])

            with doc.create(Subsection('Beautiful graphs')):
                with doc.create(TikZ()):
                    plot_options = 'height=4cm, width=6cm, grid=major'
                    with doc.create(Axis(options=plot_options)) as plot:
                        plot.append(Plot(name='model', func='-x^5 - 242'))

                        coordinates = [
                            (-4.77778, 2027.60977),
                            (-3.55556, 347.84069),
                            (-2.33333, 22.58953),
                            (-1.11111, -493.50066),
                            (0.11111, 46.66082),
                            (1.33333, -205.56286),
                            (2.55556, -341.40638),
                            (3.77778, -1169.24780),
                            (5.00000, -3269.56775),
                        ]

                        plot.append(
                            Plot(name='estimate', coordinates=coordinates))

            with doc.create(Subsection('Cute kitten pictures')):
                with doc.create(Figure(position='h!')) as kitten_pic:
                    kitten_pic.add_image(image_filename, width='120px')
                    kitten_pic.add_caption('Look it\'s on its back')

        doc.generate_pdf('full', clean_tex=False)
Пример #2
0
            table.add_hline(1, 2)
            table.add_empty_row()
            table.add_row((4, 5, 6, 7))

a = np.array([[100, 10, 20]]).T
M = np.matrix([[2, 3, 4], [0, 0, 1], [0, 0, 2]])

with doc.create(Section('The fancy stuff')):
    with doc.create(Subsection('Correct matrix equations')):
        doc.append(Math(data=[Matrix(M), Matrix(a), '=', Matrix(M * a)]))

    with doc.create(Subsection('Beautiful graphs')):
        with doc.create(TikZ()):
            plot_options = 'height=6cm, width=6cm, grid=major'
            with doc.create(Axis(options=plot_options)) as plot:
                plot.append(Plot(name='model', func='-x^5 - 242'))

                coordinates = [
                    (-4.77778, 2027.60977),
                    (-3.55556, 347.84069),
                    (-2.33333, 22.58953),
                    (-1.11111, -493.50066),
                    (0.11111, 46.66082),
                    (1.33333, -205.56286),
                    (2.55556, -341.40638),
                    (3.77778, -1169.24780),
                    (5.00000, -3269.56775),
                ]

                plot.append(Plot(name='estimate', coordinates=coordinates))
Пример #3
0
section.append(table)

a = np.array([[100, 10, 20]]).T
M = np.matrix([[2, 3, 4], [0, 0, 1], [0, 0, 2]])

math = Math(data=[Matrix(M), Matrix(a), '=', Matrix(M * a)])
equation = Subsection('Matrix equation', data=[math])

section.append(equation)

tikz = TikZ()

axis = Axis(options='height=6cm, width=6cm, grid=major')

plot1 = Plot(name='model', func='-x^5 - 242')
coordinates = [
    (-4.77778, 2027.60977),
    (-3.55556, 347.84069),
    (-2.33333, 22.58953),
    (-1.11111, -493.50066),
    (0.11111, 46.66082),
    (1.33333, -205.56286),
    (2.55556, -341.40638),
    (3.77778, -1169.24780),
    (5.00000, -3269.56775),
]

plot2 = Plot(name='estimate', coordinates=coordinates)

axis.append(plot1)
Пример #4
0
def test_tikz():
    # PGFPlots
    t = TikZ(data=None)
    repr(t)

    a = Axis(data=None, options=None)
    repr(a)

    p = Plot(name=None, func=None, coordinates=None, error_bar=None,
             options=None)
    repr(p)

    opt = TikZOptions(None)
    repr(opt)

    scope = TikZScope(data=None)
    repr(scope)

    c = TikZCoordinate.from_str("(0,0)")
    c = TikZCoordinate(x=0, y=0, relative=False)
    d = c + (0, 1)
    e = c - (0, 1)
    f = (0, 1) + c
    c.distance_to(d)
    repr(c)
    repr(d)
    repr(e)
    repr(f)

    bool(c == (1, 1))
    bool(c == TikZCoordinate(1, 1))
    bool(TikZCoordinate(1, 1, relative=True) == (1, 1))
    bool(TikZCoordinate(1, 1, relative=False) == (1, 1))
    bool(TikZCoordinate(1, 1, relative=True) == TikZCoordinate(1,
                                                               1,
                                                               relative=False))

    # test expected to fail
    try:
        g = TikZCoordinate(0, 1, relative=True) +\
            TikZCoordinate(1, 0, relative=False)
        repr(g)
        raise Exception
    except ValueError:
        pass

    a = TikZNodeAnchor(node_handle=None, anchor_name=None)
    repr(a)

    n = TikZNode(handle=None, options=None, at=None, text=None)
    repr(n)

    p = n.get_anchor_point("north")
    repr(p)

    p = n.get_anchor_point('_180')
    repr(p)

    p = n.west
    repr(p)

    up = TikZUserPath(path_type="edge", options=TikZOptions('bend right'))
    repr(up)

    pl = TikZPathList('(0, 1)', '--', '(2, 0)')
    pl.append((0.5, 0))
    repr(pl)

    # generate a failure, illegal start
    try:
        pl = TikZPathList('--', '(0, 1)')
        raise Exception
    except TypeError:
        pass

    # fail with illegal path type
    try:
        pl = TikZPathList('(0, 1)', 'illegal', '(0, 2)')
        raise Exception
    except ValueError:
        pass

    # fail with path after path
    try:
        pl = TikZPathList('(0, 1)', '--', '--')
        raise Exception
    except ValueError:
        pass

    # other type of failure: illegal identifier after path
    try:
        pl = TikZPathList('(0, 1)', '--', 'illegal')
        raise Exception
    except (ValueError, TypeError):
        pass

    pt = TikZPath(path=None, options=TikZOptions("->"))
    pt.append(TikZCoordinate(0, 1, relative=True))
    repr(pt)

    pt = TikZPath(path=[n.west, 'edge', TikZCoordinate(0, 1, relative=True)])
    repr(pt)

    pt = TikZPath(path=pl, options=None)
    repr(pt)

    dr = TikZDraw(path=None, options=None)
    repr(dr)
Пример #5
0
v = VectorName(name='')

M = np.matrix([[2, 3, 4],
               [0, 0, 1],
               [0, 0, 2]])
m = Matrix(matrix=M, name='', mtype='p', alignment=None)

# Package
p = Package(name='', base='usepackage', options=None)

# PGFPlots
tikz = TikZ(data=None)

a = Axis(data=None, options=None)

p = Plot(name=None, func=None, coordinates=None, options=None)

# Utils
escape_latex(s='')

fix_filename(path='')

dumps_list(l=[], escape=False, token='\n')

bold(s='')

italic(s='')

verbatim(s='', delimiter='|')

# Lists
Пример #6
0
def draw():
    data = request.get_json()
    doc = Document()
    doc.append(Command('fontsize', arguments=['15', '12']))
    doc.append(Command('selectfont'))

    user, intentname = data['intent']['intentName'].split(':')

    if intentname == 'matrix':
        if data.get('operation') == 'matrix_mult':
            commands.append(draw_multiply_matrix(data))
        elif data.get('operation') == 'matrix_inverse':
            commands.append(draw_inverse_matrix(data))
        else:
            commands.append(draw_matrix(data))

    elif intentname == 'random-matrix':
        if data.get('operation') == 'matrix_mult':
            commands.append(draw_random_multiply_matrix(data))
        else:
            commands.append(draw_random_matrix(data))

    elif intentname == 'polynomial':
        f = Polynomial(data.get('coef'))
        op = data.get('operation')
        if op == 'integral':
            commands.append(draw_integral(data, f.parse()))
        elif op == 'derivate':
            commands.append(draw_derivative(data, f.parse()))
        elif op == 'plot':
            with doc.create(TikZ()):
                plot_options = 'height=10cm, width=10cm'
                with doc.create(Axis(options=plot_options)) as plot:
                    plot.append(Plot(func=f.toPlot()))
        else:
            commands.append(draw_polynomial(f.parse()))

    elif intentname == 'trigfunc':
        f = Trig(data.get('trigfunc'), data.get('var'), data.get('auxoper'))
        op = data.get('operation')
        if op == 'integral':
            commands.append(draw_integral(data, f.parse(), render=False))
        elif op == 'derivate':
            commands.append(draw_derivative(data, f.parse(), render=False))
        else:
            commands.append(draw_trig(f.parse()))

    try:
        for c in commands:
            doc.append(c)
            doc.append(NewLine())
            doc.append(NewLine())
        doc.generate_pdf(OUT_PATH, clean_tex=False)
    except Exception as e:
        print(e)

    try:
        requests.get(RENDERER_URL).json()
    except:
        pass

    return "ok"
Пример #7
0
    def __globals(self, doc, options, timPerOpt, maxtime):
        # First global view
        section = Section('Global')
        doc.append(section)
        options.append('GOD')
        with doc.create(TikZ()):
            with doc.create(
                    Axis(
                        options=
                        'xlabel=Instances (ordered wrt to increasing resolution time),'
                        'ylabel=CPU time (s),height=20cm, width=15cm,legend pos=outer north east'
                    )) as plot:
                for opt in options:
                    times = [x for x in timPerOpt[opt] if x <= maxtime]
                    times.sort()
                    coords = []
                    print("%s" % (opt), end=";")
                    for i in range(0, len(times)):
                        coords.append((i, times[i]))
                        print("%d" % (times[i]), end=";")
                    print("\n")
                    plot.append(Plot(name=opt, coordinates=coords))

        # Second zoom
        section = Section('Global + zoom')
        doc.append(section)

        with doc.create(TikZ()):
            with doc.create(
                    Axis(
                        options=
                        'xlabel=Instances (ordered wrt to increasing resolution time),'
                        'ylabel=CPU time (s),height=20cm, width=15cm,legend pos=outer north east'
                    )) as plot:
                for opt in options:
                    times = timPerOpt[opt].copy()
                    times.sort()
                    if maxtime in times:
                        id = times.index(maxtime)
                    else:
                        id = len(times)
                    coords = []
                    for i in range(max(0, id - 30), min(id + 1, len(times))):
                        coords.append((i, times[i]))
                    plot.append(Plot(name=opt, coordinates=coords))
        options.remove('GOD')

        # First global view
        for o1 in range(0, len(options) - 1):
            for o2 in range(o1 + 1, len(options)):
                opt1 = options[o1]
                opt2 = options[o2]

                section = Section('%s vs. %s' % (opt1, opt2))
                doc.append(section)
                subsection = Subsection("1st view")
                doc.append(subsection)

                with doc.create(TikZ()):
                    with doc.create(
                            Axis(options=(
                                'xlabel=time(%s) - time(%s),'
                                'ylabel=CPU time diff. (s),height=15cm, width=15cm,legend pos=outer north east'
                            ) % (opt1, opt2))) as plot:
                        times1 = timPerOpt[opt1]
                        times2 = timPerOpt[opt2]
                        times = [
                            times1[i] - times2[i]
                            for i in range(0, len(times1))
                        ]
                        times.sort()
                        coords1 = []
                        coords2 = []
                        for i in range(0, len(times)):
                            if times[i] < 0:
                                coords1.append((i, -times[i]))
                                coords2.append((i, 0))
                            elif times[i] > 0:
                                coords1.append((i, 0))
                                coords2.append((i, times[i]))
                        plot.append(Plot(name=opt2, coordinates=coords1))
                        plot.append(Plot(name=opt1, coordinates=coords2))

                subsection = Subsection("2nd view")
                doc.append(subsection)
                with doc.create(TikZ()):
                    with doc.create(
                            Axis(options=(
                                'xmode=log, ymode=log, xlabel=%s,'
                                'ylabel=%s,height=15cm, width=15cm,legend pos=outer north east'
                            ) % (opt1, opt2))) as plot:
                        times1 = timPerOpt[opt1]
                        times2 = timPerOpt[opt2]
                        coords = []
                        for i in range(0, len(times1)):
                            coords.append((times1[i], times2[i]))
                        plot.append(
                            Plot(name="Instance",
                                 coordinates=coords,
                                 options='only marks, mark=+'))
                        plot.append(
                            Plot(func="x",
                                 options=("domain=0.001:%s") % (maxtime)))