![将漂亮的打印表格转换为 Latex 表格格式](https://linux22.com/image/449543/%E5%B0%86%E6%BC%82%E4%BA%AE%E7%9A%84%E6%89%93%E5%8D%B0%E8%A1%A8%E6%A0%BC%E8%BD%AC%E6%8D%A2%E4%B8%BA%20Latex%20%E8%A1%A8%E6%A0%BC%E6%A0%BC%E5%BC%8F.png)
有没有办法(脚本、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
答案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}
结果: