我怎样才能将代码从 Mathematica 转换为文本并将其放入这样的枚举中?

我怎样才能将代码从 Mathematica 转换为文本并将其放入这样的枚举中?

我有一份来自 Mathematica 的列表

{{{1, 1, 1}, {1, 1, 1}}, {{1, 1, 1}, {1, 1, 2}}, {{1, 1, 1}, {1, 2, 
1}}, {{1, 1, 1}, {1, 2, 2}}, {{1, 1, 1}, {2, 1, 1}}, {{1, 1, 
1}, {2, 1, 2}}, {{1, 1, 1}, {2, 2, 1}}, {{1, 1, 1}, {2, 2, 2}}, {{1, 1, 2}, {1, 1, 1}}, {{1, 1, 2}, {1, 1, 2}}, {{1, 1, 2}, {1, 2, 1}}, {{1, 1, 2}, {1, 2, 2}}, {{1, 1, 2}, {2, 1, 1}}, {{1, 1, 2}, {2, 1, 2}}, {{1, 1, 2}, {2, 2, 1}}, {{1, 1, 2}, {2, 2, 2}}, {{1, 2, 1}, {1, 1, 1}}, {{1, 2, 1}, {1, 1, 2}}, {{1, 2, 1}, {1, 2, 1}}, {{1, 2, 1}, {1, 2, 2}}, {{1, 2, 1}, {2, 1, 1}}, {{1, 2, 1}, {2, 1, 2}}, {{1, 2, 1}, {2, 2, 1}}, {{1, 2, 1}, {2, 2, 2}}, {{1, 2, 2}, {1, 1, 1}}, {{1, 2, 2}, {1, 1, 2}}, {{1, 2, 2}, {1, 2, 1}}, {{1, 2, 2}, {1, 2,2}}, {{1, 2, 2}, {2, 1, 1}}, {{1, 2, 2}, {2, 1, 2}}, {{1, 2, 2}, {2, 2, 1}}, {{1, 2, 2}, {2, 2, 2}}, {{2, 1, 1}, {1, 1, 1}}, {{2, 1, 1}, {1, 1, 2}}, {{2, 1, 1}, {1, 2, 1}}, {{2, 1, 1}, {1, 2, 2}}, {{2, 1, 1}, {2, 1, 1}}, {{2, 1, 1}, {2, 1, 2}}, {{2, 1, 1}, {2, 2, 1}}, {{2, 1, 1}, {2, 2, 2}}, {{2, 1, 2}, {1, 1, 1}}, {{2, 1, 2}, {1, 1, 2}}, {{2, 1, 2}, {1, 2, 1}}, {{2, 1, 2}, {1, 2, 2}}, {{2, 1, 2}, {2, 1, 1}}, {{2, 1, 2}, {2, 1, 2}}, {{2, 1, 2}, {2, 2, 1}}, {{2, 1, 2}, {2, 2, 2}}, {{2, 2, 1}, {1, 1, 1}}, {{2, 2, 1}, {1, 1, 2}}, {{2, 2, 1}, {1, 2, 1}}, {{2, 2, 1}, {1, 2, 2}}, {{2, 2, 1}, {2, 1, 1}}, {{2, 2, 1}, {2, 1, 2}}, {{2, 2, 1}, {2, 2, 1}}, {{2, 2, 1}, {2, 2, 2}}, {{2, 2, 2}, {1, 1, 1}}, {{2, 2, 2}, {1, 1, 2}}, {{2, 2, 2}, {1, 2, 1}}, {{2, 2, 2}, {1, 2, 2}}, {{2, 2, 2}, {2, 1, 1}}, {{2, 2, 2}, {2, 1, 2}}, {{2, 2, 2}, {2, 2, 1}}, {{2, 2, 2}, {2, 2, 2}}}

我想将该列表转换为以下形式

在此处输入图片描述

我手工试了一下。

\documentclass[12pt,a4paper]{article}
\usepackage{enumitem}
\begin{document}
\begin{enumerate}[label=\arabic*)]
\item $(1, 1, 1), (1, 1, 1). $ \item $ (1, 1, 1), (1, 1, 2). $ \item $ (1, 1, 1), (1, 2, 
    1). $ \item $ (1, 1, 1), (1, 2, 2). $ \item $ (1, 1, 1), (2, 1, 1). $ \item $ (1, 1, 
    1), (2, 1, 2). $ \item $ (1, 1, 1), (2, 2, 1). $ \item $ (1, 1, 1), (2, 2, 
    2). $ \item $ (1, 1, 2), (1, 1, 1). $ \item $ (1, 1, 2), (1, 1, 2). $ \item $ (1, 1, 
    2), (1, 2, 1). $ \item $ (1, 1, 2), (1, 2, 2). $ \item $ (1, 1, 2), (2, 1, 
    1). $ \item $ (1, 1, 2), (2, 1, 2). $ \item $ (1, 1, 2), (2, 2, 1). $ \item $ (1, 1, 
    2), (2, 2, 2). $ \item $ (1, 2, 1), (1, 1, 1). $ \item $ (1, 2, 1), (1, 1, 
    2). $ \item $ (1, 2, 1), (1, 2, 1). $ \item $ (1, 2, 1), (1, 2, 2). $ \item $ (1, 2, 
    1), (2, 1, 1). $ \item $ (1, 2, 1), (2, 1, 2). $ \item $ (1, 2, 1), (2, 2, 
    1). $ \item $ (1, 2, 1), (2, 2, 2). $ \item $ (1, 2, 2), (1, 1, 1). $ \item $ (1, 2, 
    2), (1, 1, 2). $ \item $ (1, 2, 2), (1, 2, 1). $ \item $ (1, 2, 2), (1, 2, 
    2). $ \item $ (1, 2, 2), (2, 1, 1). $ \item $ (1, 2, 2), (2, 1, 2). $ \item $ (1, 2, 
    2), (2, 2, 1). $ \item $ (1, 2, 2), (2, 2, 2). $ \item $ (2, 1, 1), (1, 1, 
    1). $ \item $ (2, 1, 1), (1, 1, 2). $ \item $ (2, 1, 1), (1, 2, 1). $ \item $ (2, 1, 
    1), (1, 2, 2). $ \item $ (2, 1, 1), (2, 1, 1). $ \item $ (2, 1, 1), (2, 1, 
    2). $ \item $ (2, 1, 1), (2, 2, 1). $ \item $ (2, 1, 1), (2, 2, 2). $ \item $ (2, 1, 
    2), (1, 1, 1). $ \item $ (2, 1, 2), (1, 1, 2). $ \item $ (2, 1, 2), (1, 2, 
    1). $ \item $ (2, 1, 2), (1, 2, 2). $ \item $ (2, 1, 2), (2, 1, 1). $ \item $ (2, 1, 
    2), (2, 1, 2). $ \item $ (2, 1, 2), (2, 2, 1). $ \item $ (2, 1, 2), (2, 2, 
    2). $ \item $ (2, 2, 1), (1, 1, 1). $ \item $ (2, 2, 1), (1, 1, 2). $ \item $ (2, 2, 
    1), (1, 2, 1). $ \item $ (2, 2, 1), (1, 2, 2). $ \item $ (2, 2, 1), (2, 1, 
    1). $ \item $ (2, 2, 1), (2, 1, 2). $ \item $ (2, 2, 1), (2, 2, 1). $ \item $ (2, 2, 
    1), (2, 2, 2). $ \item $ (2, 2, 2), (1, 1, 1). $ \item $ (2, 2, 2), (1, 1, 
    2). $ \item $ (2, 2, 2), (1, 2, 1). $ \item $ (2, 2, 2), (1, 2, 2). $ \item $ (2, 2, 
    2), (2, 1, 1). $ \item $ (2, 2, 2), (2, 1, 2). $ \item $ (2, 2, 2), (2, 2, 1). $ \item $ (2, 2, 2), (2, 2, 2)$.
\end{enumerate} 
\end{document}

如果列表太长,对我来说会很困难。我该如何将其转换为文本?

答案1

这是一个基于 LuaLaTeX 的解决方案,它利用了 Lua 的string.substring.gsub函数。该解决方案的主要要求是长字符串中不能有任何换行符。

在此处输入图片描述

% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{enumitem}

\usepackage{luacode} % for 'luacode' env.
\begin{luacode}
s = "{{{1, 1, 1}, {1, 1, 1}}, {{1, 1, 1}, {1, 1, 2}}, {{1, 1, 1}, {1, 2, 1}}, {{1, 1, 1}, {1, 2, 2}}, {{1, 1, 1}, {2, 1, 1}}, {{1, 1, 1}, {2, 1, 2}}, {{1, 1, 1}, {2, 2, 1}}, {{1, 1, 1}, {2, 2, 2}}, {{1, 1, 2}, {1, 1, 1}}, {{1, 1, 2}, {1, 1, 2}}, {{1, 1, 2}, {1, 2, 1}}, {{1, 1, 2}, {1, 2, 2}}, {{1, 1, 2}, {2, 1, 1}}, {{1, 1, 2}, {2, 1, 2}}, {{1, 1, 2}, {2, 2, 1}}, {{1, 1, 2}, {2, 2, 2}}, {{1, 2, 1}, {1, 1, 1}}, {{1, 2, 1}, {1, 1, 2}}, {{1, 2, 1}, {1, 2, 1}}, {{1, 2, 1}, {1, 2, 2}}, {{1, 2, 1}, {2, 1, 1}}, {{1, 2, 1}, {2, 1, 2}}, {{1, 2, 1}, {2, 2, 1}}, {{1, 2, 1}, {2, 2, 2}}, {{1, 2, 2}, {1, 1, 1}}, {{1, 2, 2}, {1, 1, 2}}, {{1, 2, 2}, {1, 2, 1}}, {{1, 2, 2}, {1, 2,2}}, {{1, 2, 2}, {2, 1, 1}}, {{1, 2, 2}, {2, 1, 2}}, {{1, 2, 2}, {2, 2, 1}}, {{1, 2, 2}, {2, 2, 2}}, {{2, 1, 1}, {1, 1, 1}}, {{2, 1, 1}, {1, 1, 2}}, {{2, 1, 1}, {1, 2, 1}}, {{2, 1, 1}, {1, 2, 2}}, {{2, 1, 1}, {2, 1, 1}}, {{2, 1, 1}, {2, 1, 2}}, {{2, 1, 1}, {2, 2, 1}}, {{2, 1, 1}, {2, 2, 2}}, {{2, 1, 2}, {1, 1, 1}}, {{2, 1, 2}, {1, 1, 2}}, {{2, 1, 2}, {1, 2, 1}}, {{2, 1, 2}, {1, 2, 2}}, {{2, 1, 2}, {2, 1, 1}}, {{2, 1, 2}, {2, 1, 2}}, {{2, 1, 2}, {2, 2, 1}}, {{2, 1, 2}, {2, 2, 2}}, {{2, 2, 1}, {1, 1, 1}}, {{2, 2, 1}, {1, 1, 2}}, {{2, 2, 1}, {1, 2, 1}}, {{2, 2, 1}, {1, 2, 2}}, {{2, 2, 1}, {2, 1, 1}}, {{2, 2, 1}, {2, 1, 2}}, {{2, 2, 1}, {2, 2, 1}}, {{2, 2, 1}, {2, 2, 2}}, {{2, 2, 2}, {1, 1, 1}}, {{2, 2, 2}, {1, 1, 2}}, {{2, 2, 2}, {1, 2, 1}}, {{2, 2, 2}, {1, 2, 2}}, {{2, 2, 2}, {2, 1, 1}}, {{2, 2, 2}, {2, 1, 2}}, {{2, 2, 2}, {2, 2, 1}}, {{2, 2, 2}, {2, 2, 2}}}"
s = s:sub ( 2, -2 )
s = s:gsub ( "%{%{([%d%s%,]*)%}, %{([%d%s%,]*)%}%},?" , "\\item $(%1)$, $(%2)$." )
\end{luacode}

\begin{document}

\begin{enumerate}[label=\arabic*),noitemsep]
\directlua{ tex.sprint ( s ) }
\end{enumerate}

\end{document}

答案2

这是实现此目的的一种方法。

创建脚本

将您的 Mathematica 输出存储为 list.txt:

{{{1, 1, 1}, {1, 1, 1}}, {{1, 1, 1}, {1, 1, 2}}, {{1, 1, 1}, {1, 2, 
1}}, {{1, 1, 1}, {1, 2, 2}}, {{1, 1, 1}, {2, 1, 1}}, {{1, 1, 
1}, {2, 1, 2}}, {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 1, 1}}, {{2, 2, 1}, {2, 1, 2}}, {{2, 2, 1}, {2, 2, 1}}, {{2, 2, 1}, {2, 2, 2}}, {{2, 2, 2}, {1, 1, 1}}, {{2, 2, 2}, {1, 1, 2}}, {{2, 2, 2}, {1, 2, 1}}, {{2, 2, 2}, {1, 2, 2}}, {{2, 2, 2}, {2, 1, 1}}, {{2, 2, 2}, {2, 1, 2}}, {{2, 2, 2}, {2, 2, 1}}, {{2, 2, 2}, {2, 2, 2}}}

用于后期处理的工具是 perl,一种实用的提取和报告语言,如下所示:

list2latex.pl:

#!/usr/bin/perl
use strict;
use warnings;

# ~~~ open list ~~~
open F, "list.txt" or die "can't find list.txt\n";
my @l = <F>;

# ~~~ merging into 1 long string ~~~
my $s = "";

foreach(my $i=0; $i <@l; $i++){
    $s .= $l[$i];
}

# ~~~ some clean up ~~~~
$_ = $s;
s/\n//g;            # no more crlf
s/\{\{\{/\{\{/g;    # removing triplets
s/\}\}\}/\}\}/g;

s/\{/\(/g;
s/\}/\)/g;

$s = $_;

  
# ~~~ now, let's create the items ~~~
my @it = split /\)\)/, $s;

foreach(my $i=0; $i<@it; $i++){
    $_ = "\\item \$$it[$i]).\$ \n";
    s/\(\(/\(/;      
    s/\$,\s/\$/;
      
    print $_;
}

当然,它的作用与Mico 的 luatex 脚本,但更细粒度,因此您可以通过调试器跟踪它并在需要时进行修改。选择一种您有信心的脚本语言。

运行

从命令行:

  • 跑步
  • 重定向到it.txt
>perl list2latex.pl > it.txt

输入它

\documentclass[12pt,a4paper]{article}
\usepackage{enumitem}

\begin{document}
 \begin{enumerate}[label=\arabic*)]
    \input{it.txt}
 \end{enumerate} 
\end{document}

结果

答案3

将代码从 Mathematica 转换为 LaTeX 的最简单的方法是当它仍在 Mathematica 中时。

生成示例列表后

list = Tuples[{1, 2}, {2, 3}]

我们可以使用以下 Mathematica 代码将{{1,1,1},{1,1,1}}列表的每一行格式化为\item $(1,1,1), (1,1,1)$.

StringJoin[StringTemplate["\\item $(``,``,``), (``,``,``)$. "] 
  @@ Flatten[#] & /@ list]

(稍微解释一下发生了什么:StringTemplate["\\item $(``,``,``), (``,``,``)$. "]告诉我们要对列表中每个条目使用的格式。它需要六个输入,因此我们Flatten将条目转换为六个数字的列表,而不是 2 乘 3 的矩阵。我们将/@结果函数 () 应用于每一行,然后将StringJoin它们放在一起。稍后在此答案中,我使用<>,即 的内联形式StringJoin

我们可以获取结果输出并将其复制粘贴到文件中.tex。(如果执行此操作,请务必使用“复制为...”并选择“纯文本”,否则您将获得格式化为 Mathematica 输入的内容。)但是,我们也可以让 Mathematica 将其导出:

Export[NotebookDirectory[] <> "output.tex",
"\\documentclass[12pt,a4paper]{article}
\\usepackage{enumitem}
\\begin{document}
\\begin{enumerate}[label=\\arabic*)]
" <> (StringTemplate["\\item $(``,``,``), (``,``,``)$. "]
  @@ Flatten[#] & /@ list) <> 
"
\\end{enumerate}
\\end{document}", "Text"]

您会注意到,我们必须写双反斜杠来转义每个反斜杠,而将其输入 Mathematica 笔记本中非常麻烦。如果您希望输出文件中包含很多其他内容,则可能需要单独为其编写代码。首先创建一个名为的文件,input.tex其中包含以下内容:

\documentclass[12pt,a4paper]{article}
\usepackage{enumitem}
\begin{document}
\begin{enumerate}[label=\arabic*)]
``
\end{enumerate}
\end{document}

然后,我们可以告诉 Mathematica 获取该文件并将其用作输出的模板:

list = Tuples[{1, 2}, {2, 3}];
formattedList = StringJoin[StringTemplate["\\item $(``,``,``), (``,``,``)$. "] 
  @@ Flatten[#] & /@ list];
texTemplate = StringTemplate[Import[NotebookDirectory[] <> "input.tex", "Text"]];
Export[NotebookDirectory[] <> "output.tex", texTemplate[formattedList], "Text"]

(我一直假设 Mathematica 笔记本保存在您乐意使用的目录中input.texoutput.tex但是,我们当然可以给予 Mathematica 任何其他文件名,包括不依赖于的文件名NotebookDirectory[]。)

相关内容