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 = ('\\documentclass{standalone}',
        '\\usepackage[margin=1in]{geometry}',
        '\\usepackage[hang,small,bf]{caption}', '\\usepackage{tikz}',
        '\\usepackage{braket}',
        '\\usetikzlibrary{backgrounds,shadows.blur,fit,decorations.pathreplacing,shapes}'
        )
    init = ('\\begin{document}',
        '\\begin{tikzpicture}[scale=0.8, transform shape]')
    gate_style = (
        '\\tikzstyle{basicshadow}=[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 += '\\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]
\\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 += (
        """]
\\tikzstyle{none}=[inner sep=0pt,outer sep=-.5pt,minimum height=0.5cm+1pt]
\\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{
shadowed/.style={preaction={transform canvas={shift={(0.5pt,-0.5pt)}}, draw=gray, opacity=0.4}},
}
"""
    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))
