pythonlatexampersand

Ampersand in Python to LaTeX


I want to auto-generate a LaTeX document using a string generated by Python. The document contains a table in LaTeX:

\begin{table}
\begin{tabular}{|c|c|}
\hline
Index & Value \\
\hline
{{ }}
\end{tabular}
\end{table}

In the place holder {{ }} I enter a string generated by Python:

content = "1 & 3.0 \\\ \hline\n 2 & 5.0 \\\ \hline\n"

I use the chevron package and the chevron.render to enter the string content to the TEX file.

The results I get is: What I get

That is, I get "amp;" printed in each cell. The "amp;" refers to the character & which is used to separated column cell in LaTeX tables.

I want that the table will look like: What I want

Playing with it, the problem source is chevron, replacing & with amp;, probably to escape the & in HTML and XML documents.

I tried taking the full file as a string and using Python string.replace("amp;", " & "). This seems to work, but this is not a "textbook solution". Is there a neater way to solve this?


Solution

  • I'm not familiar with chevron, but it looks like it is escaping characters which would have special meaning in HTML. This is likely to cause confusion when the output is LaTeX, because its escaping needs are completely different.

    Also, it looks like chevron is an implementation of mustache, and its documentation says

    All variables are HTML escaped by default. If you want to return unescaped HTML, use the triple mustache: {{{name}}}.

    Maybe that is something to try.