Example #1
0
def formula(quantity):
    """ returns error formula of quantity as latex code

    Args:
        quantity: Quantity object

    Return:
        latex code string of error formula
    """

    assert isinstance(quantity, Quantity)

    if quantity.error_formula is None:
        raise ValueError("quantity '%s' doesn't have an error formula." % quantity.name)

    formula = quantity.error_formula
    if isinstance(formula,str):
        return formula
    else:
        # replace "_err" by sigma function
        sigma = Function("\sigma")
        for var in formula.free_symbols:
            if var.name[-4:] == "_err":
                formula = formula.subs(var, sigma( Symbol(var.name[:-4], **var._assumptions)))
        latex_code = latex(sigma(quantity)) + " = " + latex(formula)

    form_button, form_code = pytex.hide_div('Formula', '$%s$' % (latex_code) , hide = False)
    latex_button, latex_code = pytex.hide_div('LaTex', latex_code)
    res = 'Error Formula for %s<div width=20px/>%s%s<hr/>%s<br>%s' % (
        '$%s$' % latex(quantity), form_button, latex_button, form_code, latex_code)

    return render_latex(res)
Example #2
0
def qtable(*quantities, html=True, maxcols=5):
    """ Represent quantites in a table.

    Args:
        quantities: List of quantity objects.
        html: If True, output will be formatted to be displayable html.
            Else, LaTeX and html code is returned in a tuple.
        maxcols:
            Maximum number of columns. Table will be split.

    Returns:
        String of html code (html=True) or tuple (LaTeX table, html table).
    """

    if len(quantities) == 0:
        return 'No quantities selected.'

    cols = []
    if html:
        if not maxcols:
            maxcols = len(quantities)

        def chunks(l):
            for i in range(0, len(quantities), maxcols):
                yield l[i:i+maxcols]

        html = []
        ltx = []
        for chunk in chunks(quantities):
            l, h = qtable(*chunk, html=False, maxcols=None)
            html.append(h)
            ltx.append(l)

        htmlb, htmlc = pytex.hide_div('Data', ''.join(html), hide = False)
        ltxb, ltxc = pytex.hide_div('LaTeX', ''.join(ltx))

        res = 'Displaying: %s<div width=20px/>%s%s<hr/>%s<br>%s' % (
            ', '.join('$%s$' % latex(q) for q in quantities),
            htmlb, ltxb, htmlc, ltxc)

        return res

    for quant in quantities:
        assert isinstance(quant, Quantity)

        value, error, unit = adjust_to_unit(quant)

        header = quant.longname + ' ' if quant.longname else ''
        header += '$%s \\; \\mathrm{\\left[%s\\right]}$' % (
            latex(quant), latex(unit))

        column = [header]
        if error is None:
            if isinstance(value, np.ndarray):
                column.extend(pytex.align_num_list(value))
            else:
                column.append(pytex.align_num(value))
        else:
            if isinstance(value, np.ndarray):
                column.extend(pytex.format_valerr_list(value,error))
            else:
                column.append(pytex.format_valerr(value,error))
        cols.append(column)

    return (pytex.table_latex(cols), pytex.table_html(cols))