I need to send data from Python to Latex code.

The data is tabular data, hence looping in such data in Latex would be tough. So, I thought creating a latex code in Python and adding it there would solve the issue.

I am able to create tabular data with this but along with data it is also adding an amp;

row_data = '''\hline'''+1+'&'+"data1" +'&' + "data2"+'''\\'''+'''\\'''

extra amp; visible

Escape sequence '\' makes the "&" invisible and all the data comes in the first row.

row_data = '''\hline'''+1 +'\&' + "data1" +"\&" +"data2"+'''\\'''+'''\\'''

all data in 1 column

So , how to remove this extra " amp; " ?

Finally the data that is getting passed to the latex code is:

row_data = \hline1&data1&0:01:45\\\hline2&data2&0:01:40\\

I am sending the data to Latex as :

s = Template('my_pdf_tex.tex')
pdf_data ={}
pdf_data['my_data'] = row_data
texfile, texfilename = mkstemp(dir=tmp_folder)
os.write(texfile, render_to_string(s.template,pdf_data))

I am receiving the data in LateX as :

            \textbf{No.} & \textbf{Topic Name} & \textbf{Time}\\
            & \textbf{Grand Total} & \textbf{ {{total}} }\\



Expected Output

  • 1
    Welcome to TeX.SX! This table seems quite simple, so looping with LaTeX sounds reasonable. Anyway, could you please post a minimal compilable example (executable python code as well as the produced LaTeX code)?
    – TeXnician
    Sep 4, 2018 at 13:31
  • By "sending the output to LaTeX", what do you mean? Are you writing the code to a file (with Python) and then open it with your LaTeX-editor, or copy-paste it by hand into your .tex-file, or use print in Python and then copy-paste it? It seems to be a encoding-related problem. Sep 4, 2018 at 14:16
  • I will try to create a replica and send out here . My issue is ... I am using python to send the data where as I am having a tex file already written. To receive the data I am using Jinja coding {{var}} in the LaTeX code. Sep 4, 2018 at 15:30
  • it is hard to help as the error is presumably in the python code which you have not shown and is probably off topic. Looking at your output something is generating & which is the html/xml code for & and unrelated to LaTeX. Sep 4, 2018 at 16:25
  • This question strictly belongs over at Stack Overflow. However, if you need python to render something raw, you can add an r in front of that string: r'\&' should produce the ampersand rendered in LaTeX. Furthermore you should look into python string formatting. I find the .format syntax great in particular Sep 4, 2018 at 19:11

1 Answer 1


If I may suggest a slight alteration to your approach. Here, I will show a version that works with your data/variable and a couple versions which don't work - for comparison. (I simplified the workflow by defining each table row directly into a variable, rather than reading it from a file).



import sys
row_data = '\\hline1&data1&0:01:45\\\\\n'
row_data = row_data + '\\hline2&data2&0:01:40\\\\\n'



% Does work!
\pyc{tabular_str = ""}
\pyc{tabular_str = tabular_str + "\\begin{tabular}{|l|l|r|}\n"}%
\pyc{tabular_str = tabular_str + "\\hline \n"}%
\pyc{tabular_str = tabular_str + "\\textbf{No.} & \\textbf{Topic Name} & \\textbf{Time}\\\\\n"}%
\pyc{tabular_str = tabular_str + row_data}%
\pyc{tabular_str = tabular_str + "\\hline \n"}%
\pyc{tabular_str = tabular_str + "& \\textbf{Grand Total} & \\textbf{ total }\\\\\n"}%
\pyc{tabular_str = tabular_str + "\\hline \n"}%
\pyc{tabular_str = tabular_str + "\\end{tabular}\n"}%
\pyc{print(tabular_str, file=sys.stdout, flush=True)}%

\vskip 2em

% Doesn't work
    \textbf{No.} & \textbf{Topic Name} & \textbf{Time}\\
    \pyc{print(row_data, file=sys.stdout, flush=True)}%
    & \textbf{Grand Total} & \textbf{ total }\\

\vskip 2em

% Also doesn't work
\pyc{print("\\begin{tabular}{|l|l|r|}\n", file=sys.stdout, flush=True)}%
\pyc{print("\\hline \n", file=sys.stdout, flush=True)}%
\pyc{print("\\textbf{No.} & \\textbf{Topic Name} & \\textbf{Time}\\\\\n", file=sys.stdout, flush=True)}%
\pyc{print(row_data, file=sys.stdout, flush=True)}%
\pyc{print("\\hline \n", file=sys.stdout, flush=True)}%
\pyc{print("& \\textbf{Grand Total} & \\textbf{ total }\\\\\n", file=sys.stdout, flush=True)}%
\pyc{print("\\hline \n", file=sys.stdout, flush=True)}%
\pyc{print("\\end{tabular}\n", file=sys.stdout, flush=True)}%



Comparison of printing out a table with PythonTeX

You will immediately notice that all three version display differently. The top one, gives what you want, whereas the other two have display issues. What you cannot see from the image, however, are the error messages that get spit out with the latter two tabulars (this error is for the middle tabular, similar errors appear for the last one):

(pythontex-files-48/48.pytxmcr) (pythontex-files-48/48.pytxpyg)
pythontex-files-48/py_default_default_11.stdout:1: Misplaced \noalign
pythontex-files-48/py_default_default_11.stdout:1: You can't use `\hrule' here 
except with leaders
pythontex-files-48/py_default_default_11.stdout:1: Missing number, treated as z
pythontex-files-48/py_default_default_11.stdout:1: Illegal unit of measure (pt 
48.tex:37: Misplaced \noalign
48.tex:37: You can't use `\hrule' here except with leaders
48.tex:37: Missing number, treated as zero
48.tex:37: Illegal unit of measure (pt inserted)

I will be completely honest with you... I'm not exactly certain which character(s) is printed erroneously. But it appears that printing in the middle of a tabular environment ends up with some character immediately before or after \hline that is incompatible with tabular (and possibly other environments). I am also uncertain whether this is actually an encoding issue (between Python and LaTeX?) or simply the insertion of a special/reserved character.

The way to circumvent this issue is to print the entire table via PythonTeX. I realize this is rather annoying. It's convenient to look at the LaTeX table as you would expect and only print the generated rows. But, after playing around with your example, I cannot find another method that works properly.

As a final note, I could not recreate the exact ampersand & issue that you were having. But I expect that is related to this issue. If my suggested fix (to build tabular entirely as a string before printing it) doesn't work, then I would request you include a full MWE example in your post, including a couple lines of actual (data from the input file).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .