Missing $inserted in python code I use python3 (with code not from me) to insert data from a database (bsddb3) from the program GRAMPS into a LaTeX document (TeX Live 2014) and I receive the following error messages: ! Missing$ inserted.
<inserted text>
$l.1131 ...{Foto_Kurt_Neumann"=1 Kurt Neumann 1915} % ! ==> Fatal error occurred, no output PDF file produced!  The relevant part of the python code is:  self.media_added.add(media_ref.ref) path = media_path_full(self.database, media.get_path()) desc = media.get_description() text = "" notelist = media.get_note_list() for notehandle in notelist: note = self.database.get_note_from_handle(notehandle) text += le(note.get()) if intern: text += "{\\small\\textit{Bild-ID: %s}}" % (media.get_gramps_id()) draft_opt = "" if draft: draft_opt = "draft," tcstar = '' if twocolumn: tcstar = '*' file.write('''%% \\begin{figure%s}[tb!p] \\raggedright%% \\setlength\\fboxrule{.25pt} \\setlength\\fboxsep{.25pt} \\color{rulecolor} \\fbox{%% \\setlength{\\imgwidth}{\\linewidth}%% \\addtolength{\\imgwidth}{-1pt}%% \\includegraphics[%swidth=\\imgwidth,height=1.5\\imgwidth,keepaspectratio]{%s}%% }%% \\color{textcolor}%% \\caption[%s]{%s %s}%% \\label{fig:%s}%% \\end{figure%s}%% ''' % (tcstar, draft_opt, path, le(desc), le(desc), text, media.get_handle(), tcstar))  I have hours trying to solve it now, I do not see where the problem is!? I tried to add the math mode in \caption: \\caption[%s]{$%s %s\$}%%


But run into an even more crypted (related?) issue, I do not know where it comes from.

Runaway argument?
! Paragraph ended before " was complete.
\par


I am totally lost with this error.

Edit

# Latex-Encode
def le(str):
str = re.sub(r'"\b', r'"', str)
str = re.sub(r'\b"', r'"', str)
str = re.sub(r' km\b', r'\\,km', str)
str = re.sub(r'[\.,!\?]"', r'"', str)
str = re.sub(r'"', r'"', str)
str = str.replace("_", "\_")
str = re.sub(r'($[0-9a-e ]+$)', r'\\numberreference{\1}', str)
str = re.sub(r'(http://[\w\d/\._?&=-]+)',r'\\url{\1}', str)
str = str.replace('−','--')
str = str.replace('&','\\&')
str = str.replace("<u>",'\\rufname{')
str = str.replace("</u>","}")
str = str.replace("-)","\"~)")
str = str.replace("S. ","S.~")
str = re.sub(r'\)([a-zA-Z])',r')""\1',str)
str = str.replace("-/","\"~/")
str = str.replace("-","\"=")
str = str.replace("/","\\slash{}")
str = re.sub(r'\\url{([^}]*)"=',r'\\url{\1-',str)
str = re.sub(r'\\url{([^}]*)\\slash{}',r'\\url{\1/',str)
str = re.sub(r'\\url{([^}]*)"=',r'\\url{\1-',str)
str = re.sub(r'\\url{([^}]*)\\slash{}',r'\\url{\1/',str)
str = re.sub(r'\\url{([^}]*)"=',r'\\url{\1-',str)
str = re.sub(r'\\url{([^}]*)\\slash{}',r'\\url{\1/',str)
str = re.sub(r'\\url{([^}]*)"=',r'\\url{\1-',str)
str = re.sub(r'\\url{([^}]*)\\slash{}',r'\\url{\1/',str)
str = re.sub(r'\\url{([^}]*)"=',r'\\url{\1-',str)
str = re.sub(r'\\url{([^}]*)\\slash{}',r'\\url{\1/',str)
str = re.sub(r'\\url{([^}]*)"=',r'\\url{\1-',str)
str = re.sub(r'\\url{([^}]*)\\slash{}',r'\\url{\1/',str)
str = str.replace("#10^00#",'10\\textsuperscript{00}')
str = str.replace(" #LEBENSLAUF#",'~\\pageref{lebenslauf}')
return str
str2 = ''
for c in str:
if ord(c)>127:
str2 += '\mbox{%s}'%c
else:
str2 += c
return str2

• Your python code should look out for underscores (and similar characters) and escape them by putting a backslash in front _ becomes \_. Simply typesetting the caption in math mode won't get you satisfying results. – Johannes_B May 11 '15 at 14:10
• If I add str = str.replace("_", "\_") in the python code, than I get the same runaway argument. I have a bunch of latex ecoding already, see edit. – Til Hund May 11 '15 at 14:31
• Sorry, can't help you now. I gues users with experience in python would also need to see a minimal example that shows the error. It is possible, that your database is faulty, the parser is faulty or ... – Johannes_B May 11 '15 at 14:36
• Could you provide a minimal example of the produced LaTeX code? This way we might be able to see what is wrond in your LaTeX code and then in the Python code. – anderstood May 11 '15 at 14:38
• Please write an answer to your own question then for others who would have the same problem :-) – anderstood May 11 '15 at 14:45

str = re.sub(r'"\b', r'\\glqq{}', str)