# line breaks and word wraps for unpredictable character strings [closed]

I am writing python to parse and process lots n lots of files to build a final report of results from information in the files. I am using python to write the latex file, then converting the latex file to pdf. In my example, you will see how I have to escape a backslash to produce the latex file properly.

There is a section where I \begin{enumerate} and then \item to list certain results. The problem lies in what those results contain - special characters. There can be vertical pipe, less than, greater than, dollar sign, backslashes and others. I do not know in advance what characters will be in the search term. The lines in this section do not break at all and run off the right side of the page.

Here is the start of the python code that generates the latex document code:

report.write("\\documentclass[11pt]{article}\n")
report.write("\\usepackage{listings}\n")
report.write("\\lstset{basicstyle=\\small\\ttfamily,columns=flexible,breaklines=true}\n")
report.write("\\usepackage{hyperref}\n")
report.write("\\usepackage{draftwatermark}\n")
report.write("\\usepackage[dvipsnames]{xcolor}\n")
report.write("\\usepackage{fancyhdr}\n")
# interprets underscores not as subscripts
report.write("\\usepackage{underscore}\n")
# Shove the footers down where they belong
report.write("\\usepackage[textheight=8in,footskip=72pt]{geometry}\n")
report.write("\\usepackage{seqsplit}\n")
report.write("\\pagestyle{fancy}\n")
report.write("\\fancyfoot[L]{\\today}\n")
report.write("\\fancyfoot[C]{\\thepage}\n")
report.write("\\fancyfoot[R]{\\ %s}\n" % dut)
report.write("\\renewcommand{\\headrulewidth}{0pt}\n")     # remove awful top bar
report.write("\\SetWatermarkText{\\textsc{Confidential}}")
report.write("\\SetWatermarkScale{4}")
report.write("\\begin{document}\n")
report.write("\\begin{abstract}\n")


Here is the section in question:

report.write("\\subsection{Searches Run}\n")
report.write("\\begin{enumerate}\n")
for epoch, search_term in my_history:
when = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(int(float(epoch)/1000)))
report.write("\\item \\small \\texttt{ %s %s }  \\normal \n" % (when, search_term) )

report.write("\\item %s {\\raggedright %s \\par} \n" % (when, search_term) )
report.write("\\end{enumerate}\n")


I have tried:

\begin{verbatim}\end{verbatim}
\textt{search term in here}
\begin{lstlisting}\end{lstlisting}


Search terms might be like this:

#sourceUser=" < first.last@somedomain.com > "
#product != partnumber123


What latex can I write to ignore special characters in the search term and have the lines break and wrap to fit on a page?

## closed as off-topic by Joseph Wright♦Sep 5 '16 at 21:49

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question does not fall within the scope of TeX, LaTeX or related typesetting systems as defined in the help center." – Joseph Wright
If this question can be reworded to fit the rules in the help center, please edit the question.

• You can clean up the python codet! For example, for the preamble you can define a raw string, say preamble_string, and then report.write(preamble_string). Similarly, you can print the latex environments in a more pythonesque way and define regular expressions to sanitise all of the strings -- e.g. replace % with \%. To help us help you with your question can you pleas post a full minimal working example that demonstrates your problem. Please post only the latex code that you want fixed as many people on this site only understand latex. – Andrew Apr 19 '16 at 12:14
• I second @Andrew 's comment: have python to the hard work, particularly the sanitizing with regex. One other possibility: you may be able to configure a listings environment to format the code lines in the report.(en.wikibooks.org/wiki/LaTeX/Source_Code_Listings) – Ethan Bolker Apr 19 '16 at 12:30