4

I want to create a LaTeX table as follows in python:

duck    small    dog    small
        medium          medium
        large           large

how would I do that?

The list I have looks like:

lis=['dog',['small','medium','large],'duck',['small','medium','large']]

2 Answers 2

2

I figured this out.

The idea is not to treat this as nested tables but as one table. In this case a table with 4 columns, so you reformat your list as:

lis=[('dog','small','duck','small'),('','medium','','medium'),('','large','','large)]

then use the tabulate package:

from tabulate import tabulate
print(tabulate(lis))

voila:

---  ------  ----  ------
dog  small   duck  small
     medium        medium
     large         large
---  ------  ----  ------
Sign up to request clarification or add additional context in comments.

Comments

1

You can simply combine the second column into one long string with newlines:

from tabulate import tabulate
table = []
table.append(["dog", "\n".join(["small", "medium", "large"])])
table.append(["fish", "\n".join(["wet", "dry", "happy", "sad"])])
table.append(["kangaroo", "\n".join(["depressed", "joyful"])])
print(tabulate(table, ["animal", "categories"], "grid"))

output:

+----------+--------------+
| animal   | categories   |
+==========+==============+
| dog      | small        |
|          | medium       |
|          | large        |
+----------+--------------+
| fish     | wet          |
|          | dry          |
|          | happy        |
|          | sad          |
+----------+--------------+
| kangaroo | depressed    |
|          | joyful       |
+----------+--------------+

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.