> 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-tw/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）來執行外部程式，並擷取通常會輸出到終端機視窗的任何文字。例如，假設你有興趣使用以下工具將一些 DVI 檔案轉換為 SVG： `dvisvgm` 公用程式，但你想不起各種命令列選項？如果能快速排版出一份選項清單來喚回記憶，不是很好嗎？如果是這樣，請繼續閱讀——但如果你想先看看實際運作情況，請前往這個 [作品展示專案範例](https://www.overleaf.com/latex/examples/using-luatex-to-run-software-installed-on-overleafs-servers/nnvfvnfhzwsy) 看看它是怎麼做到的。

![Overleaf 專案的截圖](/files/2d6543ab27f7789c36194edee58f423200ebdad3)

### 使用 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` 其內容會被插入到主要 TeX 文件中，並使用 `\verbatiminput{command.txt}`。重要的是，你必須將任何擷取到的文字以逐字材料插入，因為像 `$, #, \, _, ^` 等字元可能會出現在匯入的文字中。

歡迎自行修改 `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-tw/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.
