> For the complete documentation index, see [llms.txt](https://overleaf-pro.ayaka.space/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://overleaf-pro.ayaka.space/latex/zh-cn/shen-ru-wen-zhang/52-using-luatex-to-run-tools-and-utilities-installed-on-overleaf-s-servers.md).

# 使用 LuaTeX 运行 Overleaf 服务器上安装的工具和实用程序

## Overleaf 的服务器：运行并执行软件

Overleaf 强大的服务器会在基于 [Docker 的 Linux 容器技术的](https://www.docker.com/)。每个项目都有自己的沙箱，这确保我们在编译项目过程中执行的任何代码都与其他项目隔离——这也是我们用于安全编译项目的多层保护中的第一层。对于 TeX 用户来说，这意味着你可以安全地访问并使用我们服务器上包含的工具和实用程序——但是怎么做呢？在这篇简短的文章中，我们展示如何使用 Lua 脚本（通过 LuaTeX）来执行外部程序，并捕获通常会输出到终端窗口的任何文本。例如，假设你想使用 `dvisvgm` 实用程序，但又记不起各种命令行选项？如果能快速排版出一个列表来帮助你回忆，不是很好吗？如果是这样，请继续阅读——但如果你更想看看实际效果，请前往这个 [图库项目示例](https://www.overleaf.com/latex/examples/using-luatex-to-run-software-installed-on-overleafs-servers/nnvfvnfhzwsy) 看看它是如何完成的。

![Overleaf 项目的截图](/files/4dd2c3983b09a3225374958fc8d134cdaf32eaed)

### 使用 LuaTeX 通过 Lua 脚本运行外部程序

由于 LuaTeX 将 Lua 脚本语言嵌入到 TeX 引擎中，因此编写一个简短脚本几乎变得轻而易举：它不仅可以执行外部程序，还可以捕获并保存到文件中该程序原本会输出到终端窗口的任何文本。一旦这些文本被保存到文件中，你就可以使用 `verbatim` 包将其导入到你的主 TeX 文档中。以下是使用 LuaTeX 实现这一点所需的全部代码：

```latex

\documentclass{article}
\usepackage{verbatim}
\begin{document}
\directlua{
function runcommand(cmd)
local fout = assert(io.popen(cmd, 'r'))
local str = assert(fout:read('*a'))
fout:close()
return str
end

local sout=runcommand("dvisvgm --help")
local marg = assert(io.open("command.txt","w"))
marg:write(sout)
marg:flush()
marg:close()
}
\verbatiminput{command.txt}
\end{document}
```

上面代码中的关键一行是 `local sout=runcommand("dvisvgm --help")` 它执行 `dvisvgm` 并使用命令行选项 `--help` 以指示 `dvisvgm` 它列出其所有命令行选项。帮助文本被捕获并保存到一个名为 `command.txt`的文本文件中。在 `\directlua{...}` 中的 Lua 代码执行完毕后， `command.txt` 的内容会使用 `\verbatiminput{command.txt}`插入到主 TeX 文档中。重要的是，你要将捕获到的任何文本作为逐字材料插入，因为诸如 `$, #, \\, _, ^` 等字符可能会出现在导入的文本中。

你可以随意修改 `runcommand("dvisvgm --help")` 中的代码，并探索执行其他程序——例如 GhostScript——或任何你感兴趣使用的 TeX Live 软件。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://overleaf-pro.ayaka.space/latex/zh-cn/shen-ru-wen-zhang/52-using-luatex-to-run-tools-and-utilities-installed-on-overleaf-s-servers.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
