将漂亮的打印表格转换为 Latex 表格格式

将漂亮的打印表格转换为 Latex 表格格式

有没有办法(脚本、python 库、在线网站等)可以帮助翻译“漂亮的打印表格”,例如:

+-------+------------------+-------+------+------------------+
|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|
+-------+------------------+-------+------+------------------+

变成乳胶表?类似

\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*}

答案1

要将表格的 ASCII 艺术版本转换为基本的类 LaTeX 版本,我建议您将所有 实例替换为+-------+------------------+-------+------+------------------+将每行\hline,内部的 四个副本替换为,删除 的外部副本,在每行末尾附加,将所有(下划线) 实例转义为,并将剩余材料包裹在和中。|&|\\_\_\begin{tabular}{|r|r|r|r|r|}\end{tabular}

比较下面发布的屏幕截图中第一个表和第二个表的外观。

我希望您能更进一步,而不仅仅是实施使表格材料可编译到 LaTeX 文档中所需的基本更改。例如,您可以决定通过删除所有垂直规则并将所有实例替换为 booktabs 包的规则绘制宏(\hline此处:\toprule\midrule\bottomrule)来赋予表格更开放和吸引人的“外观”。您还可以决定在 5 列中的 2 列中显示少于 15 或 16 位十进制数字,这将对您的读者大有裨益。在以下屏幕截图的底部表格中,我使用了包siunitx及其S列类型来自动四舍五入到三位十进制数字。

在此处输入图片描述

\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}

答案2

是的,没问题:只需将此类表格材料复制到 orgmode 文件中并将其导出为 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 是 emacs 编辑器的一个模式。即使你只需要它来导出表格材料,也需要花上几个小时来学习。顺便说一下,stackexchange 有一个 emacs 网站。

答案3

如果您是 Emacs 用户,该table-generate-source函数可以将表格转换为 latex 代码。(但也可以转换为、、html或格式)。只需在光标位于表格中时键入内容,然后选择转换后的表格的格式和目标即可。latexcalswikimediawikiM-x table-generate-source

Emacs 截图

答案4

由于您命名了 Python,假设您有table.txt,包含:

+-------+------------------+-------+------+------------------+
|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|
+-------+------------------+-------+------+------------------+

您可以tableout.txt使用 Python 以如下方式创建(删除以 开头的行+_用替换\_):

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()

然后用作tableout.txt输入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}

在此处输入图片描述

您还可以使用 Python 编写整个表格,然后将其包含在您的 LaTeX 文档中。

Python代码:

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 代码:

\documentclass{article}
\usepackage{tabularray}

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

结果:

在此处输入图片描述

相关内容