3

Is there a way (script, python library, site online, etc) that helps translate a 'pretty printed table', for example:

+-------+------------------+-------+------+------------------+
|movieId|   damp_avg_rating|count_r| sum_r|         average_r|
+-------+------------------+-------+------+------------------+
|    318|3.1538461538461537|    251|1107.0| 4.410358565737051|
|    356| 3.008403361344538|    257|1074.0| 4.178988326848249|
|   2571|2.9454545454545453|    230| 972.0| 4.226086956521739|
+-------+------------------+-------+------+------------------+

Into a latex table? something like

\begin{table*}[h]
  \caption{my caption}
  \label{tab:mylabel}
  \begin{tabular}{rll}
    \toprule
    movieId & damp_avg_rating & count_r & sum_r & average_r\\
    \midrule
    318 & number & number & number & number \\
    356 & number  & number & number & number \\
    2571 & number  & number & number & number \\
    \bottomrule
  \end{tabular}
\end{table*}
BrunoSE
  • 33

5 Answers5

5

Yes, no problem: just copy such tabular material in an orgmode file and export it to LaTeX:

   \begin{tabular}{|l|l|l|l|l|}
   \hline
   movieId & damp\_avg\_rating & count\_r & sum\_r & average\_r \\
   \hline
   318 & 3.1538461538461537 & 251 & 1107.0 & 4.410358565737051 \\
   356 & 3.008403361344538 & 257 & 1074.0 & 4.178988326848249 \\
   2571 & 2.9454545454545453 & 230 & 972.0 & 4.226086956521739 \\
   \hline
   \end{tabular}

Orgmode is a mode of an editor called emacs. Even if you only need it to export tabular materials, it will take some hours to learn that. By the way, there is an emacs site of stackexchange.

Keks Dose
  • 30,892
  • +1. I would use the r column type, though, in order to mimic the look of the pretty-printed-ASCII version. – Mico May 03 '22 at 05:55
3

To get from the ASCII-art version of the table to a basic LaTeX-like version, I suggest you replace all instances of +-------+------------------+-------+------+------------------+ with \hline, replace the four interior copies of | per row with &, get rid of the exterior copies of |, affix \\ at the end of each line, escape all instances of _ (underscore) as \_, and encase the remaining material in \begin{tabular}{|r|r|r|r|r|} and \end{tabular}.

Compare the look of the first and second table in the screenshot posted below.

I would like to hope that you would wish go a bit further than implement just the basic set of changes that are needed to make the tabular material compilable in a LaTeX document. For instance, you could decide to give the table a more open and inviting "look" by getting rid of all vertical rules and replace all instances of \hline with the rule-drawing macros (here: \toprule, \midrule, and \bottomrule) of the booktabs package. You could also decide that showing fewer than 15 or 16 decimal digits in 2 of the 5 columns would be doing your readers a huge favor. In the bottom table of the following screenshot, I've employed the siunitx package and its S column type to perform automatic rounding to three decimal digits.

enter image description here

\documentclass{article} % or some other suitable document class
\usepackage[T1]{fontenc}
\usepackage{array,booktabs}
\usepackage[group-digits=false]{siunitx}

\begin{document} \begin{table}

%% ASCII-art form \begin{verbatim} +-------+------------------+-------+------+------------------+ |movieId| damp_avg_rating|count_r| sum_r| average_r| +-------+------------------+-------+------+------------------+ | 318|3.1538461538461537| 251|1107.0| 4.410358565737051| | 356| 3.008403361344538| 257|1074.0| 4.178988326848249| | 2571|2.9454545454545453| 230| 972.0| 4.226086956521739| +-------+------------------+-------+------+------------------+ \end{verbatim}

%% minimally adapted \begin{tabular}{|r|r|r|r|r|} \hline movieId & damp_avg_rating & count_r & sum_r & average_r \ \hline 318 & 3.1538461538461537 & 251 & 1107.0 & 4.410358565737051 \ 356 & 3.008403361344538 & 257 & 1074.0 & 4.178988326848249 \ 2571 & 2.9454545454545453 & 230 & 972.0 & 4.226086956521739 \ \hline \end{tabular}

\bigskip %% more extensively adapted \begin{tabular}{@{} S[table-format=4.0] S[table-format=1.3,round-mode=places,round-precision=3] S[table-format=3.0] S[table-format=4.0,round-mode=places,round-precision=0] S[table-format=1.3,round-mode=places,round-precision=3] @{}} \toprule {movieId} & {damp_avg_rating} & {count_r} & {sum_r} & {average_r} \ \midrule 318 & 3.1538461538461537 & 251 & 1107.0 & 4.410358565737051 \ 356 & 3.008403361344538 & 257 & 1074.0 & 4.178988326848249 \ 2571 & 2.9454545454545453 & 230 & 972.0 & 4.226086956521739 \ \bottomrule \end{tabular} \end{table} \end{document}

Mico
  • 506,678
3

If you are an Emacs user, the table-generate-source function can convert the table to latex code. (but it can also be convert to html, latex, cals, wiki or mediawiki formats). Just type M-x table-generate-source while the cursor is in the table and choose the format and destination of the converted table.

Emacs screenshot

gigiair
  • 1,812
2

Since you named Python, suppose you have table.txt, containing:

+-------+------------------+-------+------+------------------+
|movieId|   damp_avg_rating|count_r| sum_r|         average_r|
+-------+------------------+-------+------+------------------+
|    318|3.1538461538461537|    251|1107.0| 4.410358565737051|
|    356| 3.008403361344538|    257|1074.0| 4.178988326848249|
|   2571|2.9454545454545453|    230| 972.0| 4.226086956521739|
+-------+------------------+-------+------+------------------+

you can create tableout.txt with Python this way (eliminating the lines beginning with + and substituting _ with \_):

tabin = open('table.txt', 'r')
tabout = open('tableout.txt','w')

for line in tabin: if line[0] != '+': line = line.replace("", "\") tabout.write(line)

tabin.close() tabout.close()

and then use tableout.txt as input with csvsimple-l3:

\documentclass{article}
\usepackage{csvsimple-l3}
\usepackage{booktabs}

\begin{document} \begin{table}[h] \csvreader[ tabular=rrrrr, separator=pipe, no head, before first line=\\toprule, late after first line=\\midrule, late after last line=\\bottomrule ]{tableout.txt}{}{\csvcolii & \csvcoliii &\csvcoliv & \csvcolv & \csvcolvi} \end{table} \end{document}

enter image description here

You can also write the entire table with Python and than include it in your LaTeX document.

Python code:

tabin = open('table.txt', 'r')
tabout = open('tableout.tex','w')

tabout.write("\begin{tblr}{colspec={*5{r}}, hlines, vlines}\r\n")

for line in tabin: if line[0] != '+': #replace _ with _ line = line.replace("", "\") #remove fisrt and last | and add \ at the end line = line[1:-2] + " \\" + "\r\n" #replace remaining | with & line = line.replace("|", " & ") tabout.write(line)

tabout.write("\end{tblr}")

tabin.close() tabout.close()

LaTeX code:

\documentclass{article}
\usepackage{tabularray}

\begin{document} \begin{table}[h] \input{tableout.tex} \end{table} \end{document}

Result:

enter image description here

CarLaTeX
  • 62,716
2

You can use pandoc wich will return this latex code:

\begin{longtable}[]{@{}
  >{\raggedright\arraybackslash}p{(\columnwidth - 8\tabcolsep) * \real{0.1111}}
  >{\raggedright\arraybackslash}p{(\columnwidth - 8\tabcolsep) * \real{0.2639}}
  >{\raggedright\arraybackslash}p{(\columnwidth - 8\tabcolsep) * \real{0.1111}}
  >{\raggedright\arraybackslash}p{(\columnwidth - 8\tabcolsep) * \real{0.0972}}
  >{\raggedright\arraybackslash}p{(\columnwidth - 8\tabcolsep) * \real{0.2639}}@{}}
\toprule()
\endhead
movieId & damp\_avg\_rating & count\_r & sum\_r &
\begin{minipage}[t]{\linewidth}\raggedright
\begin{verbatim}
    average_r
\end{verbatim}
\end{minipage} \\
318 356 2571 & 3.1538461538461537 3.008403361344538 2.9454545454545453 &
251 257 230 & 1107.0 1074.0 972.0 & 4.410358565737051 4.178988326848249
4.226086956521739 \\
\bottomrule()
\end{longtable}
DG'
  • 21,727