Writes the Latex header using the settings file.

    The header includes all packages and defines all tikz styles.

    :param dictionary settings: LaTeX settings for document.
    :return: Header of the LaTeX document.
    :rtype: string

def header(settings):
    """
    Writes the Latex header using the settings file.

    The header includes all packages and defines all tikz styles.

    :param dictionary settings: LaTeX settings for document.
    :return: Header of the LaTeX document.
    :rtype: string
    """
    packages = (r"\documentclass[convert={density=300,outext=.png}]{standalone}",
                r"\usepackage[margin=1in]{geometry}",
                r"\usepackage[hang,small,bf]{caption}",
                r"\usepackage{tikz}",
                r"\usepackage{braket}",
                r"\usetikzlibrary{backgrounds,shadows.blur,fit,decorations.pathreplacing,shapes}")

    init = (r"\begin{document}",
            r"\begin{tikzpicture}[scale=0.8, transform shape]")

    gate_style = (r"\tikzstyle{basicshadow}="
                  r"[blur shadow={shadow blur steps=8, shadow xshift=0.7pt, shadow yshift=-0.7pt, shadow scale=1.02}]")

    if not (settings.get('gate_shadow') or settings.get('control', {}).get('shadow')):
        gate_style = ""

    gate_style += r"\tikzstyle{basic}=[draw,fill=white,"
    if settings.get('gate_shadow'):
        gate_style += "basicshadow"
    gate_style += "]\n"

    gate_style += ("\\tikzstyle{{operator}}=[basic,minimum size=1.5em]\n\\tikzstyle{{phase}}=[fill=black,shape=circle,"
                   "minimum size={}"
                   "cm,inner sep=0pt,outer sep=0pt,draw=black").format(settings.get('control', {}).get('size', 0))
    if settings.get('control', {}).get('shadow'):
        gate_style += ",basicshadow"
    gate_style += ("]\n\\tikzstyle{none}=[inner sep=0pt,outer sep=-.5pt,"
                   "minimum height=0.5cm+1pt]\n"
                   "\\tikzstyle{measure}=[operator,inner sep=0pt,minimum "
                   + "height={}cm, minimum width={}cm]\n".format(
                       settings.get('gates', {}).get('MeasureGate', {}).get('height', 0),
                       settings.get('gates', {}).get('MeasureGate', {}).get('width', 0))
                   + "\\tikzstyle{xstyle}=[circle,basic,minimum height=")
    x_gate_radius = min(settings.get('gates', {}).get('XGate', {}).get('height', 0),
                        settings.get('gates', {}).get('XGate', {}).get('width', 0))
    gate_style += ("{x_rad}cm,minimum width={x_rad}cm,inner sep=0pt,{linestyle}]\n").format(
        x_rad=x_gate_radius,
        linestyle=settings.get('lines', {}).get('style', ""))
    if settings.get('gate_shadow'):
        gate_style += ("\\tikzset{\nshadowed/.style={preaction={transform canvas={shift={(0.5pt,-0.5pt)}},"
                       " draw=gray, opacity=0.4}},\n}\n")
    gate_style += "\\tikzstyle{swapstyle}=[inner sep=-1pt, outer sep=-1pt, minimum width=0pt]"
    edge_style = ("\\tikzstyle{edgestyle}=[" + settings.get('lines', {}).get('style', "") + "]")
    return "\n".join(("\n".join(packages), "\n".join(init), gate_style, edge_style))