code: def fix_filename(path):
      r
  
      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
docs: 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
      --------
      >>> fix_filename("foo.bar.pdf")
      '{foo.bar}.pdf'
      >>> fix_filename("/etc/local/foo.bar.pdf")
      '/etc/local/{foo.bar}.pdf'
      >>> fix_filename("/etc/local/foo.bar.baz/document.pdf")
      '/etc/local/foo.bar.baz/document.pdf'
      >>> fix_filename("/etc/local/foo.bar.baz/foo~1/document.pdf")
      '\detokenize{/etc/local/foo.bar.baz/foo~1/document.pdf}'