如何从 LaTeX 执行 shell 脚本?

发布于 2024-09-10 03:34:48 字数 435 浏览 2 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

无戏配角 2024-09-17 03:34:48

我会做类似以下的事情(部分是受 Roman 建议的启发): 制作你的 LaTeX 文件

\documentclass{article}
\begin{document}
\input{scriptoutput.tex}
\end{document}

并使用生成文件 scriptoutput.tex

/usr/local/bin/my-shell-script.sh > scriptoutput.tex

如果你想让它运行,你可以将其编码在 makefile 中必要时自动。或者,您可以使用 TeX \write18< /code> 命令

\documentclass{article}
\immediate\write18{/usr/local/bin/my-shell-script.sh > scriptoutput.tex}
\begin{document}
\input{scriptoutput.tex}
\end{document}

我认为每次编译文档时都会自动运行 shell 脚本。 \immediate 是确保 LaTeX 遇到命令时运行脚本所必需的,而不是等到输出一页被写入。 (有关发货例程的更多信息,请参阅此问题。)

I would do something like the following (partially motivated by what Roman suggested): make your LaTeX file be

\documentclass{article}
\begin{document}
\input{scriptoutput.tex}
\end{document}

and generate the file scriptoutput.tex using

/usr/local/bin/my-shell-script.sh > scriptoutput.tex

You could encode this in a makefile if you want to have it run automatically when necessary. Alternatively, you could use the TeX \write18 command,

\documentclass{article}
\immediate\write18{/usr/local/bin/my-shell-script.sh > scriptoutput.tex}
\begin{document}
\input{scriptoutput.tex}
\end{document}

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

半暖夏伤 2024-09-17 03:34:48

正如 David 指出的,你可以使用 \write18 调用外部程序,然后使用 \input 结果输出文件。但是,您可能需要使用 \immediate\write18 来确保在调用 \input 之前执行脚本。

或者,如果您使用较新版本的 pdf(la)tex(我认为是 1.40 之后),您可以使用管道输入命令将输出直接通过管道传输到文档中:

\documentclass{article}
\begin{document}
\input{|"/usr/local/bin/my-shell-script.sh"}
\end{document}

对于任一方法,您都需要启用外部程序调用。对于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:

\documentclass{article}
\begin{document}
\input{|"/usr/local/bin/my-shell-script.sh"}
\end{document}

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.

逆夏时光 2024-09-17 03:34:48

您可以在 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.

属性 2024-09-17 03:34:48

在 Ubuntu 11.10 GNU/Linux 上

pdflatex --enable-pipes --shell-escape mytexfile

运行

%...
[This section currently is 
\input{|"wc kmb-box.tex| tr -s ' ' | cut -d' ' -f 4"}
% 2000 characters are allowed here

\input{kmb-box}

%...

良好。即,它使用 wordcount (wc) 来报告文件 kmb-box.tex 中有多少个字符,该文件是文档的一部分(包含在其中)。

(顺便说一句,如果您想要单词而不是字符,只需更改“-f 4”中的数字)

On Ubuntu 11.10 GNU/Linux

pdflatex --enable-pipes --shell-escape mytexfile

with

%...
[This section currently is 
\input{|"wc kmb-box.tex| tr -s ' ' | cut -d' ' -f 4"}
% 2000 characters are allowed here

\input{kmb-box}

%...

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

把昨日还给我 2024-09-17 03:34:48

除非必须在 LaTeX 运行时运行脚本,否则我建议仅使用 make 来运行 LaTeX 和脚本。

我已经使用这种方法来添加文章的字数统计,并包括参考文献的统计数据。

让您的脚本生成一个 .tex 文件并将其包含在您的 LaTeX 源文件中。

下面是我的 Makefile 之一的片段:

TEX =   /usr/texbin/pdflatex
PREVIEW = /usr/bin/open

REPORT  =   SimMon
REPORT_MASTER = $(REPORT).tex

TEX_OPTIONS = -halt-on-error

SimMon:  $(REPORT_MASTER) countRefferedPages
    $(TEX) $(TEX_OPTIONS) $(REPORT_MASTER)
    @$(PREVIEW) $(REPORT).pdf

countRefferedPages: BibTeXPageCount
    cat *.tex | support/BPC/build/Debug/BPC Castle.bib > litteraturTotal.tex

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:

TEX =   /usr/texbin/pdflatex
PREVIEW = /usr/bin/open

REPORT  =   SimMon
REPORT_MASTER = $(REPORT).tex

TEX_OPTIONS = -halt-on-error

SimMon:  $(REPORT_MASTER) countRefferedPages
    $(TEX) $(TEX_OPTIONS) $(REPORT_MASTER)
    @$(PREVIEW) $(REPORT).pdf

countRefferedPages: BibTeXPageCount
    cat *.tex | support/BPC/build/Debug/BPC Castle.bib > litteraturTotal.tex
百善笑为先 2024-09-17 03:34:48

这就是我使用自己的 iexec 包执行此操作的方法

\documentclass{article}
\usepackage{iexec}
\begin{document}
\iexec{/usr/local/bin/my-shell-script.sh}
\end{document}

:不是必需的,我可以这样做:

\iexec[quiet]{/usr/local/bin/my-shell-script.sh}

This is how I do it with my own iexec package:

\documentclass{article}
\usepackage{iexec}
\begin{document}
\iexec{/usr/local/bin/my-shell-script.sh}
\end{document}

When the output is not required, I can do just this:

\iexec[quiet]{/usr/local/bin/my-shell-script.sh}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文