如何从 LaTeX 执行 shell 脚本?
我正在尝试在 LaTeX 中执行以下操作:
\documentclass{article}
\begin{document}
\execute{/usr/local/bin/my-shell-script.sh}
\end{document}
想法是在 .tex
时执行 /usr/local/bin/my-shell-script.sh
文档处理并将其输出注入 LaTeX 流。有可能吗?
附言。通过我制作的软件包可能:iexec
< /a>
I'm trying to do the following in LaTeX:
\documentclass{article}
\begin{document}
\execute{/usr/local/bin/my-shell-script.sh}
\end{document}
The idea is to execute /usr/local/bin/my-shell-script.sh
at the moment of .tex
document processing and inject its output into LaTeX stream. Is it possible at all?
PS. It's possible through the package I've made: iexec
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我会做类似以下的事情(部分是受 Roman 建议的启发): 制作你的 LaTeX 文件
并使用生成文件
scriptoutput.tex
如果你想让它运行,你可以将其编码在 makefile 中必要时自动。或者,您可以使用 TeX
\write18< /code> 命令
,
我认为每次编译文档时都会自动运行 shell 脚本。
\immediate
是确保 LaTeX 遇到命令时运行脚本所必需的,而不是等到输出一页被写入。 (有关发货例程的更多信息,请参阅此问题。)I would do something like the following (partially motivated by what Roman suggested): make your LaTeX file be
and generate the file
scriptoutput.tex
usingYou could encode this in a makefile if you want to have it run automatically when necessary. Alternatively, you could use the TeX
\write18
command,and I think that would automatically run the shell script each time you compile the document. The
\immediate
is necessary to ensure that the script is run when LaTeX encounters the command, rather than waiting until a page of output is written. (See this question for more on the shipout routine.)正如 David 指出的,你可以使用
\write18
调用外部程序,然后使用\input
结果输出文件。但是,您可能需要使用\immediate\write18
来确保在调用\input
之前执行脚本。或者,如果您使用较新版本的 pdf(la)tex(我认为是 1.40 之后),您可以使用管道输入命令将输出直接通过管道传输到文档中:
对于任一方法,您都需要启用外部程序调用。对于TeXlive发行版,您需要使用
-shell-escape
选项调用latex,或者对于MikTeX,我相信该选项是-enable-write18
。As David pointed out, you can use
\write18
to call external programs, then\input
the resultant output file. However you will probably want to use\immediate\write18
to make sure the script is executed before calling the\input
.Alternatively, if you use newer versions of pdf(la)tex (after 1.40, I think), you can pipe the output directly into the document, by using a piped input command:
For either method you will need to enable external program calls. For TeXlive distributions, you need to call latex with the
-shell-escape
option, or for MikTeX, I believe the option is-enable-write18
.您可以在 TeX 中执行此操作。这份论文 (PDF) 向您展示了如何在 TeX 中编写和执行病毒。同样的原则也适用于执行 shell 脚本。然而,我认为编写一个 Makefile 更实用,它在 LaTeX 运行之前运行并插入结果。
You can do this in TeX. This paper (PDF) shows you how to write and execute a virus within TeX. The same principles apply for executing a shell script. However in my opinion it is more practicable to write a Makefile, which runs before your LaTeX run and inserts the result.
在 Ubuntu 11.10 GNU/Linux 上
运行
良好。即,它使用 wordcount (wc) 来报告文件 kmb-box.tex 中有多少个字符,该文件是文档的一部分(包含在其中)。
(顺便说一句,如果您想要单词而不是字符,只需更改“-f 4”中的数字)
On Ubuntu 11.10 GNU/Linux
with
works nicely. ie, this uses wordcount (wc) to report how many characters are in the file kmb-box.tex, which is part of (included in) the document.
(btw If you wanted words rather than characters, just change the number in "-f 4")
除非必须在 LaTeX 运行时运行脚本,否则我建议仅使用
make
来运行 LaTeX 和脚本。我已经使用这种方法来添加文章的字数统计,并包括参考文献的统计数据。
让您的脚本生成一个
.tex
文件并将其包含在您的 LaTeX 源文件中。下面是我的 Makefile 之一的片段:
Unless it is imperative that the script is run while LaTeX is running I would recommend just using
make
to run LaTeX and you script.I have used that approach to add word counting for articles and including statistics on bibliographic references.
Let your script generate a
.tex
file and include that in you LaTeX source file.Below is a snippet from one of my Makefiles:
这就是我使用自己的
iexec
包执行此操作的方法:不是必需的,我可以这样做:
This is how I do it with my own
iexec
package:When the output is not required, I can do just this: