> 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/ja/xiang-xi-ji-shi/52-using-luatex-to-run-tools-and-utilities-installed-on-overleaf-s-servers.md).

# OverleafのサーバーにインストールされたツールやユーティリティをLuaTeXで実行する

## Overleaf のサーバー：ソフトウェアの実行と起動

Overleaf の強力なサーバーは、〜を基盤としたサンドボックス内であなたのプロジェクトをコンパイルします [Docker の Linux コンテナ技術](https://www.docker.com/)。各プロジェクトには専用のサンドボックスが割り当てられ、プロジェクトのコンパイルの一部として私たちが実行するコードが他のプロジェクトから分離されるようになっています。これは、プロジェクトを安全にコンパイルするために私たちが用いている複数の保護層の最初のものです。TeX ユーザーにとって、これはサーバー上に含まれるツールやユーティリティへ安全にアクセスして使用できることを意味します。しかし、どうやって？この短い記事では、Lua スクリプト（LuaTeX 経由）を使って外部プログラムを実行し、通常はターミナルウィンドウに出力されるテキストを取得する方法を示します。たとえば、 `dvisvgm` ユーティリティを使って DVI ファイルを SVG に変換したいけれど、さまざまなコマンドラインオプションを思い出せないとします。それらの一覧をすばやく組版して記憶を呼び戻せたら便利ではないでしょうか？ もしそうなら、このまま読み進めてください。ただし、実際の動作を見たい場合は、この [ギャラリープロジェクトの例](https://www.overleaf.com/latex/examples/using-luatex-to-run-software-installed-on-overleafs-servers/nnvfvnfhzwsy) を見て、やり方を確認してください。

![Overleaf プロジェクトのスクリーンショット](/files/31a0b1c5ca4b1bfabaf67d422d94c488e7533136)

### Lua スクリプトを使って LuaTeX から外部プログラムを実行する方法

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 コードの実行が完了すると、 `\directlua{...}` の内容が `command.txt` \verbatiminput{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/ja/xiang-xi-ji-shi/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.
