def gen_reference(reference_id, reference_text, text='', references_list=None):
    """Creates a in-file reference.

    A reference is contained by:
      - An ID (reference_id)
      - A text that is displayed later in the document (reference_text)
      - An optional text (text) that is displayed just before the id.

    Since the reference_text is displayed later in the document (usually at
    the end) all reference_text's can be collected in a list.

    Args:
      reference_id (string): This is a unique id to of the reference.
      reference_text (string): This is the text that should be displayed
        later in the document.
      text (string, optional): It's displayed just before the ID.
      reference_list (list of strings, optional): The reference text is
        added to this list and then the list is returned.

    Returns:
      string: The id and (optional) text.
      list of strings: Updated list with references for later use.

    """
    if text is '':
        md_str = ' [{}]'.format(reference_id)
    elif reference_id is '':
        reference_id = text
        md_str = '[{}][]'.format(reference_id)
    else:
        md_str = '[{}][{}]'.format(text, reference_id)
    ref = '[{}]: {}\n'.format(reference_id, reference_text)
    if not references_list:
        references_list = []
    references_list.append(ref)
    return md_str, references_list
