mixedmath

Explorations in math and programming
David Lowry-Duda



Typesetting tables in latex is annoying. In practice, I usually have the tabular data in some programming-friendly format and need to put it in latex. I forget how to do this nicely, but it's extremely simple using the tabulate package in python.

import tabulate
from itertools import batched

# Data
A = [
  1, 2, 3, 4, 5, 6, 7, 10, 11, 14, 15, 17, 19, 21,
  23, 26, 31, 34, 35, 39, 41, 47, 51, 55, 59, 71,
  79, 87, 95, 111, 119, 131, 143, 159, 167, 191,
  215, 231, 239, 287, 311, 359, 479, 551, 671, 719
]

print(len(A))  # 46

# We have to choose the number of columns. I choose 12 here.
data = list(batched(A, 12))
print(tabulate.tabulate(data, tablefmt="latex_booktabs"))

This outputs

{rrrrrrrrrrrr}
\toprule
   1 &   2 &   3 &   4 &   5 &   6 &   7 &  10 &  11 &  14 &  15 &  17 \\
  19 &  21 &  23 &  26 &  31 &  34 &  35 &  39 &  41 &  47 &  51 &  55 \\
  59 &  71 &  79 &  87 &  95 & 111 & 119 & 131 & 143 & 159 & 167 & 191 \\
 215 & 231 & 239 & 287 & 311 & 359 & 479 & 551 & 671 & 719 &  & \\
\bottomrule

The point is that this can be done programmatically and easily (even though in practice one might choose to keep or not keep the top and bottom rules).


Leave a comment

Info on how to comment

To make a comment, please send an email using the button below. Your email address won't be shared (unless you include it in the body of your comment). If you don't want your real name to be used next to your comment, please specify the name you would like to use. If you want your name to link to a particular url, include that as well.

bold, italics, and plain text are allowed in comments. A reasonable subset of markdown is supported, including lists, links, and fenced code blocks. In addition, math can be formatted using $(inline math)$ or $$(your display equation)$$.

Please use plaintext email when commenting. See Plaintext Email and Comments on this site for more. Note also that comments are expected to be open, considerate, and respectful.

Comment via email