Skip to content

Instantly share code, notes, and snippets.

@stephenroller
Created November 1, 2019 18:43
Show Gist options
  • Save stephenroller/090172120924f13eed2ab9003367de00 to your computer and use it in GitHub Desktop.
Save stephenroller/090172120924f13eed2ab9003367de00 to your computer and use it in GitHub Desktop.
Reformats latex tables so columns align
#!/usr/bin/env python
import sys
data = sys.stdin.read()
lines = data.split('\n')
columnized = [
x.replace(r'\\[-0.25mm]', '').replace(r'\\', '').split('&') for x in lines
]
lengths = []
outrows = []
numcolumns = max(len(row) for row in columnized)
for i, row in enumerate(columnized):
if len(row) != numcolumns:
outrows.append(lines[i])
continue
newrow = [' ' + c.strip() + ' ' for c in row]
outrows.append(newrow)
for i, c in enumerate(newrow):
if len(lengths) <= i or lengths[i] < len(c):
if len(lengths) <= i:
lengths.append(None)
lengths[i] = len(c)
finalrows = []
for r, row in enumerate(outrows):
if type(row) is str:
print(row)
continue
fr = []
for i, c in enumerate(row):
formatstr = "%-" + str(lengths[i]) + "s"
fr.append(formatstr % (c))
print("&".join(fr) + r'\\')
if r == 0:
print(r'\midrule')
@stephenroller
Copy link
Author

input:

\begin{table}[t]
    \small
    \centering
    \begin{tabular}{lllrr}
    \toprule
                &                               &                &Crowdworkers & Experts \\
                \cmidrule(lr){4-4} \cmidrule(lr){5-5}
         Winner &                               &          Loser & Win rate & Win rate\\
    \midrule
        \lultok & \multirow{5}{*}{{\em  beats}} & \lmle~baseline &     57\% &         \\
        \lulseq &   & \lmle~baseline &    *71\% &         \\
     \lultokseq &             & \lmle~baseline &    *82\% &         \\
     \lultokseq &       &        \lultok &    *75\% &         \\
     \lultokseq &                               &        \lulseq &     59\% &         \\
    \midrule
     \lultokseq & \multirow{2}{*}{{\em  beats}} &    \lmle~Nucleus sampling ($p = 0.9$) &     59\% &    *83\%\\
     \lultokseq &        &  \lmle~Beam blocking (4-gram) &     60\% &    *74\%\\
    
    \bottomrule
    \end{tabular}

output:

\begin{table}[t]
    \small
    \centering
    \begin{tabular}{lllrr}
    \toprule
            &                               &                                    & Crowdworkers & Experts  \\
                \cmidrule(lr){4-4} \cmidrule(lr){5-5}
 Winner     &                               & Loser                              & Win rate     & Win rate \\
    \midrule
 \lultok    & \multirow{5}{*}{{\em  beats}} & \lmle~baseline                     & 57\%         &          \\
 \lulseq    &                               & \lmle~baseline                     & *71\%        &          \\
 \lultokseq &                               & \lmle~baseline                     & *82\%        &          \\
 \lultokseq &                               & \lultok                            & *75\%        &          \\
 \lultokseq &                               & \lulseq                            & 59\%         &          \\
    \midrule
 \lultokseq & \multirow{2}{*}{{\em  beats}} & \lmle~Nucleus sampling ($p = 0.9$) & 59\%         & *83\%    \\
 \lultokseq &                               & \lmle~Beam blocking (4-gram)       & 60\%         & *74\%    \\

    \bottomrule
    \end{tabular}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment