> 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/in-depth-articles/52-using-luatex-to-run-tools-and-utilities-installed-on-overleaf-s-servers.md).

# Using LuaTeX to run tools and utilities installed on Overleaf’s servers

## Overleaf’s servers: running and executing software

Overleaf’s powerful servers compile your project in a sandbox based on [Docker’s Linux container technology](https://www.docker.com/). Every project gets its own sandbox, which ensures that any code we execute as part of compiling the project is isolated from other projects—and this is the first of several layers of protection that we use to compile projects safely. For TeX users, this means you can safely access and use tools and utilities contained on our servers—but how? In this short post we show how to use a Lua script (via LuaTeX) to execute an external program and capture any text that would usually be output to a terminal window. For example, suppose you are interested to convert some DVI files into SVG using the `dvisvgm` utility but you can’t recall the various command-line options? Wouldn’t it be nice to quickly typeset a list of them to refresh your memory? If so, read on—but if you prefer to see this in action, hop over to this [gallery project example](https://www.overleaf.com/latex/examples/using-luatex-to-run-software-installed-on-overleafs-servers/nnvfvnfhzwsy) to see how it’s done.

![Screenshot of an Overleaf project](/files/C21pKlsMgv3yJI3cXfJH)

### Using LuaTeX to run an external program via a Lua script

Because LuaTeX has the Lua scripting language embedded into the TeX engine it becomes almost trivial to write a short script that not only executes an external program but also captures, and saves to a file, any text which that program would otherwise output to a terminal window. Once that text is saved to a file you can use the `verbatim` package to import it into your main TeX document. Here is the entire code needed to do this with 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}
```

The key line in the above code is `local sout=runcommand("dvisvgm --help")` which executes `dvisvgm` with the command-line option `--help` to instruct `dvisvgm` to list all of its command-line options. The help text is captured and saved to a text file called `command.txt`. After the Lua code within `\directlua{...}` has finished executing, the content of `command.txt` is inserted into the main TeX document using `\verbatiminput{command.txt}`. It is important that you insert any captured text as verbatim material because characters such as `$, #, \, _, ^` etc. might be present within the imported text.

Feel free to amend the code in `runcommand("dvisvgm --help")` and explore executing other programs—such as GhostScript—or any TeX Live software you might be interested to use.


---

# 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/in-depth-articles/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.
