> For the complete documentation index, see [llms.txt](https://ayakaleaf-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://ayakaleaf-pro.ayaka.space/latex/zh-cn/shen-du-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 在 TeX 引擎中嵌入了 Lua 脚本语言，因此编写一个简短的脚本几乎变得轻而易举；这个脚本不仅可以执行外部程序，还可以捕获并保存到文件中该程序原本会输出到终端窗口的任何文本。文本保存到文件后，你就可以使用 `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://ayakaleaf-pro.ayaka.space/latex/zh-cn/shen-du-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.
