r"""Creates a source code listing:

        \begin{codelisting}[hbtp]
        inner_elements
        \caption[caption]{\label{label}caption}
        \end{codelisting}

    and returns the list containing the pandoc elements.

    Args:
        inner_elements: A list of inner pandoc elements, usually a
                        code block and potentially outputs etc.
        caption:        The caption to be used. Will be used below code and in
                        list of code listings.
        label:          The label to use.
        shortcaption:   A short caption to be used in the list of code listings.
                        If None, the normal caption will be used.
        above:          The caption is placed above (True) or below (False)
                        the code listing.

    Returns:
        A list of elements for this codelisting.

def make_codelisting(inner_elements, caption, label, *,
                     shortcaption=None, above=True):
    r"""Creates a source code listing:

        \begin{codelisting}[hbtp]
        inner_elements
        \caption[caption]{\label{label}caption}
        \end{codelisting}

    and returns the list containing the pandoc elements.

    Args:
        inner_elements: A list of inner pandoc elements, usually a
                        code block and potentially outputs etc.
        caption:        The caption to be used. Will be used below code and in
                        list of code listings.
        label:          The label to use.
        shortcaption:   A short caption to be used in the list of code listings.
                        If None, the normal caption will be used.
        above:          The caption is placed above (True) or below (False)
                        the code listing.

    Returns:
        A list of elements for this codelisting.
    """
    begin = pf.RawBlock(r'\begin{codelisting}[hbtp]', format='tex')
    end = pf.RawBlock(r'\end{codelisting}', format='tex')

    if not shortcaption:
        shortcaption = caption
    cap_begin = f'\\caption[{shortcaption}]{{\\label{{{label}}}'
    caption_elem = pf.RawBlock(cap_begin + caption + '}', format='tex')
    if above:
        return [begin, caption_elem] + inner_elements + [end]
    return [begin] + inner_elements + [caption_elem, end]