2

So, Let's say I have outputs of the following form:

enter image description here

How would one insert this output into latex? I have tried using verbatim but I didn't like the result I wanted.

Paul Gessler
  • 29,607
Rodrigo
  • 491
  • 3
  • 9
  • 1
    It depends on how these tables are produced in Python. Some libraries (like pandas) have functionality to generate LaTeX code. If the library that you have doesn't offer export to LaTeX or to some intermediate format that can further be transformed into LaTeX (like csv for example) then the only option is to write the table in LaTeX by hand or maybe to write some Python code to generate the LaTeX code for you if you have access to the code that produced the current output. – Marijn Jan 26 '22 at 20:15
  • I have access to the code, but its quite big and this tables were formatted by me (with appropriate print commands). Is there a solution for this or are pictures the way to go? – Rodrigo Jan 26 '22 at 20:34
  • 1
    If you wrote the print commands then you can also write a bit of code around it for a tabular header, cell separators and line endings. I can show a small example below. – Marijn Jan 26 '22 at 20:43

2 Answers2

5

If you control the print statements creating the current output then you can also write print statements to generate LaTeX code. Simple example:

Python:

headers = ["x 1","x 2","x 3","x 4","x 5","bbar"]
data = dict()
data["x 4"] = [1,2,3,1,0,9]
data["x 5"] = [3,2,2,0,1,15]
data["z"] = [1,9,3,0,0,0]

textabular = f"l|{'r'*len(headers)}" texheader = " & " + " & ".join(headers) + "\\" texdata = "\hline\n" for label in sorted(data): if label == "z": texdata += "\hline\n" texdata += f"{label} & {' & '.join(map(str,data[label]))} \\\n"

print("\begin{tabular}{"+textabular+"}") print(texheader) print(texdata,end="") print("\end{tabular}")

Generated LaTeX code:

\begin{tabular}{l|rrrrrr}
 & x 1 & x 2 & x 3 & x 4 & x 5 & bbar\\
\hline
x 4 & 1 & 2 & 3 & 1 & 0 & 9 \\
x 5 & 3 & 2 & 2 & 0 & 1 & 15 \\
\hline
z & 1 & 9 & 3 & 0 & 0 & 0 \\
\end{tabular}

Output:

enter image description here

Marijn
  • 37,699
  • This would be cool for a small task... but considering I have more like 50 outputs like the one I posted, this get a bit tricky and too much work, but that's a good way to solve the problem! – Rodrigo Jan 26 '22 at 21:05
  • 1
    @roro the idea is that if the data is organized in Python in a similar way (for example your two tables above have exactly the same size and structure, only different numbers and slightly different rownames) then you can use the same Python code to parse this. For example in the above you only need to change the three lines defining the data dictionary and then the code below, unchanged, will generate the second table. Depending on how complex your data is you might need to add in some extra Python logic. – Marijn Jan 26 '22 at 21:09
  • 1
    @roro More than 50 such tables would suggest using a function to populate the data but you haven't given any details on how the data is determined. I don't know pythontex; I use the sagetex package for Python (plus a CAS). See here or here for some examples. I would think pythontex can do this as well. – DJP Jan 26 '22 at 22:45
  • @Marijn I think you could prepend ‘r’ before string in Python, e.g. r”\hline”, to avoid multiple escaping of backslashes. – Celdor Jan 27 '22 at 00:50
  • @Celdor you are right (although it wouldn't combine nicely with f"" I guess), I decided not to spend too much effort on making the Python code clean and readable :) – Marijn Jan 27 '22 at 06:49
  • 1
    Both r and f work together but you are right it may not be worth the effort :). I understand I am probably in the wrong place pointing at something of little importance but I always consider using r"" to avoid multiple backslashes. – Celdor Jan 27 '22 at 08:37
1

Expanding on the above answers that use print statements for generating the Latex code: there is a Python library called latextable for doing exactly this https://github.com/JAEarly/latextable

There's also guidance on how to use it here: https://towardsdatascience.com/how-to-create-latex-tables-directly-from-python-code-5228c5cea09a

Full disclosure - I am the author of this library.

  • 1
    This seems like an interesting solution. Maybe you can improve the answer here by showing (with a code example and a screenshot of the output) how the library can be applied in the current case? – Marijn Jan 19 '23 at 15:50