我使用 python3(不是我写的代码)从程序中的数据库(bsddb3)插入数据爷爷放入 LaTeX 文档(TeX Live 2014)中,我收到以下错误消息:
! Missing $ inserted.
<inserted text>
$
l.1131 ...{Foto_Kurt_Neumann"=1 Kurt Neumann 1915}
%
! ==> Fatal error occurred, no output PDF file produced!
Python代码的相关部分是:
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))
我花了几个小时尝试解决它,但我不知道问题出在哪里!?
我尝试添加数学模式\caption
:
\\caption[%s]{$%s %s$}%%
但是遇到了一个更加隐秘(相关?)的问题,我不知道它来自哪里。
Runaway argument?
! Paragraph ended before " was complete.
<to be read again>
\par
我完全被这个错误搞糊涂了。
编辑
# 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
答案1
解决方案如下。我必须添加一行来替换下划线。
str = re.sub(r'"\b', r'\\glqq{}', str)
str = re.sub(r'\b"', r'\\grqq{}', str)
str = re.sub(r'[\.,!\?]"', r'\\grqq{}', str)
str = re.sub(r'"', r'\\textquotedblleft', str)
str = str.replace("_", "\_") # replaces the underscores, thank to Johannes for point it out!