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. 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 a wealth of 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 to see how it’s done.
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:
\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.
Overleaf guides
- Creating a document in Overleaf
- Uploading a project
- Copying a project
- Creating a project from a template
- Including images in Overleaf
- Exporting your work from Overleaf
- Working offline in Overleaf
- Using Track Changes in Overleaf
- Using bibliographies in Overleaf
- Sharing your work with others
- Debugging Compilation timeout errors
- How-to guides
LaTeX Basics
- Creating your first LaTeX document
- Choosing a LaTeX Compiler
- Paragraphs and new lines
- Bold, italics and underlining
- Lists
- Errors
Mathematics
- Mathematical expressions
- Subscripts and superscripts
- Brackets and Parentheses
- Fractions and Binomials
- Aligning Equations
- Operators
- Spacing in math mode
- Integrals, sums and limits
- Display style in math mode
- List of Greek letters and math symbols
- Mathematical fonts
Figures and tables
- Inserting Images
- Tables
- Positioning Images and Tables
- Lists of Tables and Figures
- Drawing Diagrams Directly in LaTeX
- TikZ package
References and Citations
- Bibliography management in LaTeX
- Bibliography management with biblatex
- Biblatex bibliography styles
- Biblatex citation styles
- Bibliography management with natbib
- Natbib bibliography styles
- Natbib citation styles
- Bibliography management with bibtex
- Bibtex bibliography styles
Languages
- Multilingual typesetting on Overleaf using polyglossia and fontspec
- International language support
- Quotations and quotation marks
- Arabic
- Chinese
- French
- German
- Greek
- Italian
- Japanese
- Korean
- Portuguese
- Russian
- Spanish
Document structure
- Sections and chapters
- Table of contents
- Cross referencing sections and equations
- Indices
- Glossaries
- Nomenclatures
- Management in a large project
- Multi-file LaTeX projects
- Hyperlinks
Formatting
- Lengths in LaTeX
- Headers and footers
- Page numbering
- Paragraph formatting
- Line breaks and blank spaces
- Text alignment
- Page size and margins
- Single sided and double sided documents
- Multiple columns
- Counters
- Code listing
- Code Highlighting with minted
- Using colours in LaTeX
- Footnotes
- Margin notes
Fonts
Presentations
Commands
Field specific
- Theorems and proofs
- Chemistry formulae
- Feynman diagrams
- Molecular orbital diagrams
- Chess notation
- Knitting patterns
- CircuiTikz package
- Pgfplots package
- Typing exams in LaTeX
- Knitr
- Attribute Value Matrices
Class files
- Understanding packages and class files
- List of packages and class files
- Writing your own package
- Writing your own class
- Tips