<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def fix_filename(path):
 r"""Fix filenames for use in LaTeX. Latex has problems if there are one or more points in the filename, thus 'abc.def.jpg' will be changed to '{abc.def}.jpg' Args ---- filename : str The filen name to be changed. Returns ------- str The new filename. Examples -------- '{foo.bar}.pdf' '/etc/local/{foo.bar}.pdf' '/etc/local/foo.bar.baz/document.pdf' '\detokenize{/etc/local/foo.bar.baz/foo~1/document.pdf}' """


    path_parts = path.split('/' if os.name == 'posix' else '\\')
    dir_parts = path_parts[:-1]

    filename = path_parts[-1]
    file_parts = filename.split('.')

    if len(file_parts) > 2:
        filename = '{' + '.'.join(file_parts[0:-1]) + '}.' + file_parts[-1]

    dir_parts.append(filename)
    fixed_path = '/'.join(dir_parts)

    if '~' in fixed_path:
        fixed_path = r'\detokenize{' + fixed_path + '}'

    return fixed_path