def display_tikz(tikzcode, scale=2.0): preamble = r"""\usepackage{pgfplots} \usepackage{xcolor} \pgfplotsset{compat=newest} """ d = {"src": tikzcode, "extras": preamble, "scale": float(scale) } src = itikz.IMPLICIT_PIC_TMPL.substitute(d) return itikz.fetch_or_compile_svg(src, prefix='pandas2pgfplots-', working_dir=get_tempdir())
def test_fetch_or_compile_svg_good_input(tmpdir, monkeypatch): expected_md5 = "15d53b05d3a27e1545c9a3688be5e3b4" res = itikz.fetch_or_compile_svg(RECTANGLE_TIKZ, 'test_', str(tmpdir)) for ext in 'tex', 'svg': path = tmpdir.join("test_{}.{}".format(expected_md5, ext)) assert os.path.exists(str(path)) for ext in 'log', 'aux', 'pdf': path = tmpdir.join("test_{}.{}".format(expected_md5, ext)) assert not os.path.exists(str(path)) assert isinstance(res, SVG)
def test_fetch_or_compile_svg_bad_input_full_err(tmpdir, capsys): expected_md5 = "361fadf1c712e812d198c4cab5712a79" res = itikz.fetch_or_compile_svg(BAD_TIKZ, 'test_', str(tmpdir), True) for ext in 'tex', 'svg', 'log', 'aux', 'pdf': path = tmpdir.join("test_{}.{}".format(expected_md5, ext)) assert not os.path.exists(str(path)) assert res is None _, err = capsys.readouterr() assert 'error' in err.lower() assert len(err.splitlines()) > 20
def qr(matrices, formater=repr, array_names=True, fig_scale=None, tmp_dir=None, keep_file=None): m = MatrixGridLayout(matrices, extra_rows=[1, 0, 0, 0]) m.array_format_string_list() m.array_of_tex_entries(formater=formater) brown = make_decorator(text_color='brown', bf=True) def qr_dec_known_zeros(WtA, WtW): l_WtA = [(1, 2), [(i, j) for i in range(WtA.shape[0]) for j in range(WtA.shape[0]) if i > j]] l_WtW = [(1, 3), [(i, j) for i in range(WtW.shape[0]) for j in range(WtW.shape[0]) if i != j]] return [l_WtA, l_WtW] for spec in qr_dec_known_zeros(matrices[1][2], matrices[1][3]): #[ [(1,2), [(1,0),(2,0),(2,1)]], [(1,3), [(1,0),(2,0),(2,1), (0,1),(0,2),(1,2)]] ]: m.decorate_tex_entries(*spec[0], brown, entries=spec[1]) red = make_decorator(text_color='red', bf=True) red_rgt = make_decorator(text_color='red', bf=True, move_right=True) m.add_row_above(0, 2, [red(f'v_{i+1}') for i in range(3)] + [red(f'w_{i+1}') for i in range(3)], formater=lambda a: a) m.add_col_left(1, 1, [red_rgt(f'w^t_{i+1}') for i in range(3)], formater=lambda a: a) if array_names: dec = make_decorator(bf=True, delim='$') m.nm_submatrix_locs( 'QR', color='blue', name_specs=[ [(0, 2), 'al', dec('A')], [(0, 3), 'ar', dec('W')], # ---------------------- [(1, 1), 'al', dec('W^t')], [(1, 2), 'al', dec('W^t A')], [(1, 3), 'ar', dec('W^t W')], # ---------------------- [(2, 0), 'al', dec(r'S = \left( W^t W \right)^{-\tfrac{1}{2}}')], [(2, 1), 'br', dec(r'Q^t = S W^t')], [(2, 2), 'br', dec('R = S W^t A')] ]) else: m.nm_submatrix_locs() m.tex_repr(blockseps=r'\noalign{\vskip3mm} ') m_code = m.nm_latexdoc(preamble=preamble + r" \NiceMatrixOptions{cell-space-limits = 2pt}", fig_scale=fig_scale) h = itikz.fetch_or_compile_svg(m_code, prefix='qr_', working_dir=tmp_dir, debug=False, **itikz.build_commands_dict(use_xetex=True, use_dvi=False, crop=True), nexec=1, keep_file=keep_file) return h, m
def ge(matrices, Nrhs=0, formater=repr, pivot_list=None, comment_list=None, variable_summary=None, array_names=None, fig_scale=None, tmp_dir=None, keep_file=None): '''basic GE layout (development version): matrices: [ [None, A0], [E1, A1], [E2, A2], ... ] Nrhs: number of right hand side columns determines the placement of a partition line, if any pivot_list: [ pivot_spec, pivot_spec, ... ] where pivot_spec = [grid_pos, [pivot_pos, pivot_pos, ...]] comment_list: [ txt, txt, ... ] must have a txt entry for each layer. Multiline comments are separated by \\ variable_summary: [ basic, ... ] a list of true/false values specifying whether a column has a pivot or not array_names: list of names for the two columns: [ 'E', ['A','b','I'] ''' extra_cols = None if comment_list is None else 1 extra_rows = None if variable_summary is None else 2 m = MatrixGridLayout(matrices, extra_rows=extra_rows, extra_cols=extra_cols) # compute the format spec for the arrays and set up the entries (defaults to a single partition line) partitions = {} if Nrhs == 0 else {1: [m.mat_col_width[-1] - Nrhs]} m.array_format_string_list(partitions=partitions) m.array_of_tex_entries( formater=formater ) # could overwride the entry to TeX string conversion here if pivot_list is not None: red_box = make_decorator(text_color='red', boxed=True, bf=True) for spec in pivot_list: m.decorate_tex_entries(*spec[0], red_box, entries=spec[1]) if comment_list is not None: m.nm_text(comment_list) if variable_summary is not None: blue = make_decorator(text_color='blue', bf=True) red = make_decorator(text_color='red', bf=True) typ = [] var = [] for (i, basic) in enumerate(variable_summary): if basic: typ.append(red(r'\Uparrow')) var.append(red(f'x_{i+1}')) else: typ.append(blue(r'\uparrow')) var.append(blue(f'x_{i+1}')) m.add_row_below(m.nGridRows - 1, 1, typ, formater=lambda a: a) m.add_row_below(m.nGridRows - 1, 1, var, offset=1, formater=lambda a: a) if array_names is not None: name_specs = mk_ge_names(m.nGridRows, *array_names) else: name_specs = None m.nm_submatrix_locs( 'A', color='blue', name_specs=name_specs ) # this defines the submatrices (the matrix delimiters) m.tex_repr( ) # converts the array of TeX entries into strings with separators and spacers m_code = m.nm_latexdoc(template=GE_TEMPLATE, preamble=preamble, extension=extension, fig_scale=fig_scale) h = itikz.fetch_or_compile_svg(m_code, prefix='ge_', working_dir=tmp_dir, debug=False, **itikz.build_commands_dict(use_xetex=True, use_dvi=False, crop=True), nexec=1, keep_file=keep_file) return h, m