18

I have this django template which I use to generate LaTeX files

\documentclass[11pt]{report}

\begin{document}
\begin{table}
    \centering
    \begin{tabular}{lcr}
    \hline
    {% for col in head %}
        \textbf{ {{col}} }
        {% if not forloop.last %}
           &
        {% endif %}
    {% endfor %} 
    \\
    \hline
    {% for row in table %}
        {% for cell in row %}

            {% if not forloop.last %} 
               &
            {% endif %}
        {% endfor %}
        \\
    {% endfor %}
    \hline
    \end{tabular}
    \caption{Simple Phonebook}
    \label{tab:phonebook}
\end{table}

\end{document}

But my no of columns are very large so they can contain any special characters in them. I am getting error while generating pdf file.

How can I escape all the text in all columns?

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
user196264097
  • 877
  • 2
  • 13
  • 23

2 Answers2

30

Alex's answer including suggestions in code, if you want to copy-paste:

import re

def tex_escape(text):
    """
        :param text: a plain text message
        :return: the message escaped to appear correctly in LaTeX
    """
    conv = {
        '&': r'\&',
        '%': r'\%',
        '$': r'\$',
        '#': r'\#',
        '_': r'\_',
        '{': r'\{',
        '}': r'\}',
        '~': r'\textasciitilde{}',
        '^': r'\^{}',
        '\\': r'\textbackslash{}',
        '<': r'\textless{}',
        '>': r'\textgreater{}',
    }
    regex = re.compile('|'.join(re.escape(str(key)) for key in sorted(conv.keys(), key = lambda item: - len(item))))
    return regex.sub(lambda match: conv[match.group()], text)

See Easiest way to replace a string using a dictionary of replacements? for replacement approach.

Mark
  • 18,730
  • 7
  • 107
  • 130
5

Something like this should do:

CHARS = {
    '&':  r'\&',
    '%':  r'\%', 
    '$':  r'\$', 
    '#':  r'\#', 
    '_':  r'\letterunderscore{}', 
    '{':  r'\letteropenbrace{}', 
    '}':  r'\letterclosebrace{}',
    '~':  r'\lettertilde{}', 
    '^':  r'\letterhat{}', 
    '\\': r'\letterbackslash{}',
}

print("".join([CHARS.get(char, char) for char in "&%$#_{}~^\\"]))

Create you own template filter to filter your variables

[edit]:

This was the special characters for ConText, for LaTex, adapt with:

\& \% \$ \# \_ \{ \} \textasciitilde{} \^{} \textbackslash{}
Alex
  • 247
  • 1
  • 13