<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def write_tex(matrix, version, out, scale=1, border=None, color='black', unit='pt', url=None):
 """\ Serializes the matrix as LaTeX PGF picture. Requires the `PGF/TikZ <https://en.wikipedia.org/wiki/PGF/TikZ>`_ package (i.e. ``\\usepackage{pgf}``) in the LaTeX source. :param matrix: The matrix to serialize. :param int version: The (Micro) QR code version :param out: Filename or a file-like object supporting to write text data. :param scale: Indicates the size of a single module (default: 1 which corresponds to 1 x 1 in the provided unit per module). :param int border: Integer indicating the size of the quiet zone. If set to ``None`` (default), the recommended border size will be used (``4`` for QR Codes, ``2`` for a Micro QR Codes). :param str color: LaTeX color name. The color name is taken at it is, so ensure that it refers either to a default color name or that the color was defined previously. :param unit: Unit of the drawing (default: ``pt``) :param url: Optional URL where the QR Code should point to. Requires the "hyperref" package. Default: ``None``. """

    def point(x, y):
        return '\\pgfqpoint{{{0}{2}}}{{{1}{2}}}'.format(x, y, unit)

    check_valid_scale(scale)
    check_valid_border(border)
    border = get_border(version, border)
    with writable(out, 'wt') as f:
        write = f.write
        write('% Creator:  {0}\n'.format(CREATOR))
        write('% Date:     {0}\n'.format(time.strftime('%Y-%m-%dT%H:%M:%S')))
        if url:
            write('\\href{{{0}}}{{'.format(url))
        write('\\begin{pgfpicture}\n')
        write('  \\pgfsetlinewidth{{{0}{1}}}\n'.format(scale, unit))
        if color and color != 'black':
            write('  \\color{{{0}}}\n'.format(color))
        x, y = border, -border
        for (x1, y1), (x2, y2) in matrix_to_lines(matrix, x, y, incby=-1):
            write('  \\pgfpathmoveto{{{0}}}\n'.format(point(x1 * scale, y1 * scale)))
            write('  \\pgfpathlineto{{{0}}}\n'.format(point(x2 * scale, y2 * scale)))
        write('  \\pgfusepath{stroke}\n')
        write('\\end{{pgfpicture}}{0}\n'.format('' if not url else '}'))