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:

# interprets underscores not as subscripts
# Shove the footers down where they belong
report.write("\\fancyhead{}\n")         # clear out default header - has underline
report.write("\\fancyfoot[R]{\\ %s}\n" % dut)
report.write("\\renewcommand{\\headrulewidth}{0pt}\n")     # remove awful top bar

Here is the section in question:

report.write("\\subsection{Searches Run}\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) )

I have tried:

\textt{search term in here}

Search terms might be like this:

#sourceUser=" < first.last@somedomain.com > " 
#Vendor=ABC | top address
#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.

  • 3
    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