r"""
    CommandLine:
        python -m utool.util_latex --test-compile_latex_text --show

    Ignore:
        pdflatex -shell-escape --synctex=-1 -src-specials -interaction=nonstopmode\
            ~/code/ibeis/tmptex/latex_formatter_temp.tex

    Example1:
        >>> # DISABLE_DOCTEST
        >>> from utool.util_latex import *  # NOQA
        >>> import utool as ut
        >>> verbose = True
        >>> #dpath = '/home/joncrall/code/ibeis/aidchallenge'
        >>> dpath = dirname(ut.grab_test_imgpath())
        >>> #ut.vd(dpath)
        >>> orig_fpaths = ut.list_images(dpath, fullpath=True)
        >>> figure_str = ut.get_latex_figure_str(orig_fpaths, width_str='2.4in', nCols=2)
        >>> input_text = figure_str
        >>> pdf_fpath = ut.compile_latex_text(input_text, dpath=dpath,
        >>>                                   verbose=verbose)
        >>> output_pdf_fpath = ut.compress_pdf(pdf_fpath)
        >>> print(pdf_fpath)
        >>> ut.quit_if_noshow()
        >>> ut.startfile(pdf_fpath)

def compile_latex_text(input_text, dpath=None, fname=None, verbose=True,
                       move=True, nest_in_doc=None, title=None,
                       preamb_extra=None):
    r"""
    CommandLine:
        python -m utool.util_latex --test-compile_latex_text --show

    Ignore:
        pdflatex -shell-escape --synctex=-1 -src-specials -interaction=nonstopmode\
            ~/code/ibeis/tmptex/latex_formatter_temp.tex

    Example1:
        >>> # DISABLE_DOCTEST
        >>> from utool.util_latex import *  # NOQA
        >>> import utool as ut
        >>> verbose = True
        >>> #dpath = '/home/joncrall/code/ibeis/aidchallenge'
        >>> dpath = dirname(ut.grab_test_imgpath())
        >>> #ut.vd(dpath)
        >>> orig_fpaths = ut.list_images(dpath, fullpath=True)
        >>> figure_str = ut.get_latex_figure_str(orig_fpaths, width_str='2.4in', nCols=2)
        >>> input_text = figure_str
        >>> pdf_fpath = ut.compile_latex_text(input_text, dpath=dpath,
        >>>                                   verbose=verbose)
        >>> output_pdf_fpath = ut.compress_pdf(pdf_fpath)
        >>> print(pdf_fpath)
        >>> ut.quit_if_noshow()
        >>> ut.startfile(pdf_fpath)
    """
    import utool as ut
    if verbose:
        print('[ut] compile_latex_text')

    if nest_in_doc is None:
        nest_in_doc = 'documentclass' not in input_text
    if nest_in_doc:
        text = make_full_document(input_text, title=title,
                                  preamb_extra=preamb_extra)
    if not dpath:
        dpath = os.getcwd()
    if fname is None:
        fname = 'temp_latex'

    # Create temporary work directly
    work_dpath = join(dpath, '.tmptex')
    ut.ensuredir(work_dpath, verbose=verbose > 1)

    fname_tex = ut.ensure_ext(fname, '.tex')
    fname_pdf = ut.ensure_ext(fname, '.pdf')

    tex_fpath = join(work_dpath, fname_tex)
    pdf_fpath_output = join(work_dpath, fname_pdf)
    ut.write_to(tex_fpath, text)

    with ut.ChdirContext(work_dpath, verbose=verbose > 1):
        # print(text)
        args = ' '.join([
            'lualatex', '-shell-escape', '--synctex=-1', '-src-specials',
            '-interaction=nonstopmode', tex_fpath
        ])
        info = ut.cmd2(args, verbose=verbose > 1)
        if not ut.checkpath(pdf_fpath_output, verbose=verbose > 1):
            print('Error compiling LaTeX')
            ut.print_code(text, 'latex')
            print(info['out'])
            raise RuntimeError('latex failed ')

    if move:
        pdf_fpath = join(dpath, fname_pdf)
        ut.move(pdf_fpath_output, pdf_fpath, verbose=verbose > 1)
    else:
        pdf_fpath = pdf_fpath_output
    return pdf_fpath