Directory support/runtexshebang
runtexshebang: Lua script running LaTeX document files with TeX-style shebang
Lua script running LaTeX document files with a TeX-style shebang (%#!)
What is a TeX-style shebang (%#!)
In short, a TeX-style shebang (%#!) is a special kind of TeX comment that you include in your TeX/LaTeX document file to tell the operating system's shell how to run the file for the rest of the file:
%#!lualatex foo.tex \documentclass{article} \begin{document} Hello, {\LaTeX} World! Happy {\TeX}ing. \end{document}
If you are using a TeX-style shebang, it must appear on the line that matched 20 lines or less in your LaTeX document, and it has to start with a TeX comment symbol (%) followed by a hash sign (#) and an exclamation mark (!), colloquially known as the bang, hence the name shebang for TeX/LaTeX.
Getting started
- Install
runtexshebang.luain your TeX Live system.
The runtexshebang package is contained in TeX Live 2023 or higher. So, it can be installed through
$ sudo tlmgr install runtexshebang
If you manually install runtexshebang.lua in your TeX Live system, you can install it through
cp runtexshebang.lua /some/where/TEXMFDIST_or_TEXMFLOCAL/scripts/runtexshebang/runtexshebang.lua cd TEXLIVE_BIN_DIRECTORY ln -s ../../TEXMFDIST_or_TEXMFLOCAL/scripts/runtexshebang/runtexshebang.lua runtexshebang
- Make a sample file with a TeX-style shebang.
Make the following LaTeX document.
%#!lualatex foo.tex \documentclass{article} \begin{document} Hello, {\LaTeX} World! Happy {\TeX}ing. \end{document}
- Run the sample file.
runtexshebang foo.tex
Then, it will run lualatex foo.tex. To see which command would be run without running it, use runtexshebang --dry-run foo.tex.
Examples
TeXworks
Settings
- Open "Typesetting" tab in TeXworks Preferences.
- Add the tool configuration "runtexshebang" in "Processing tools" as below.
- Name:
runtexshebang - Program:
runtexshebang - Arguments:
$fullname - Check "View PDF after running" (if necessary)
- Name:
LaTeX document in the internal editor of TeXwork
% !TEX program = is a magic comment of TeXworks. You can set one processing tool as % !TEX program = <your choice of tool configuration>.
% !TEX program = runtexshebang %#! lualatex --synctex=1 foo
LaTeX Workshop: Visual Studio Code
Settings
Nothing!
LaTeX document in Visual Studio Code
% !TEX program = is a magic comment of LaTeX Workshop. You need another magic comment to the target LaTeX document file as below.
% !TEX program = runtexshebang % !TEX options = "%DOC%".tex %#! lualatex --synctex=1 foo
TeXShop
Settings
There the “Engine” settings for the command lines "TeX+dvipdfmx / TeX+dvips+distiller" as follows.
- TeX:
runtexshebang - LaTeX:
runtexshebang
LaTeX document in the internal editor of TeXShop
% !TEX program = is a magic comment of TeXShop.
% !TEX program = runtexshebang %#! lualatex --synctex=1 foo
Applications
What does the PATH variable set in my editor?
%#! echo $PATH
How about general configuration information for the activated TeX Live version and its configuration in my editor?
%#! tlmgr conf
Otherwise
You can run any command line on your OSs. For example, you can use any build tools as follows: make, rake, llmk, latexmk, and so on.
%#! make foo.pdf % #! rake foo.pdf % #! llmk % #! latexmk foo %% and so on.
You can typeset a LaTeX document file in a Docker container as below.
%#! docker run -i --rm --workdir /data --mount type=bind,src=$(pwd)/,dst=/data/ bar/foo lualatex foo
Security
- runtexshebang executes the
%#!line of your document as a shell command line (/bin/sh -con Unix,cmd.exe /con Windows). Runningruntexshebang foo.texis equivalent to typing the contents of its%#!line into your terminal yourself. Treat.texfiles with a%#!line exactly like aMakefileor alatexmkrc: read the first lines before running them, and only use runtexshebang on documents you trust. - Editor integration widens the exposure. Once you have registered runtexshebang as a typesetting tool or engine, every document that carries
% !TEX program = runtexshebangand a%#!line will execute that line when you press "Typeset". Review the%#!line of documents you received from others. runtexshebang printsruntexshebang: running: <command>on stderr (the editor console) before executing it, andruntexshebang --dry-run foo.texprints the line without executing it. - A
%#!line that contains an ASCII control character other than TAB (for example a lone carriage return or an escape sequence) is refused with exit status 2, both when running and with--dry-run, because such characters could make the printed command look different from the one actually run. Non-ASCII bytes are left alone, so UTF-8, ShiftJIS and EUC-JP file names keep working. - runtexshebang must never be added to
shell_escape_commandsintexmf.cnf(or to any other restricted-shell-escape allow list). If it were, any document compiled with the default restricted shell escape could run arbitrary commands via\write18{runtexshebang evil.tex}. It is intentionally absent from that list and is not designed to be called from\write18. It is a build tool that the user invokes, likemake,latexmk,araraorllmk. - runtexshebang loads no Lua code (no
require, nodofile) and reads no configuration file. Its behaviour depends only on its command-line arguments, the first 20 lines of the given file, and the environment (PATH,COMSPEC). - The command is located through your
PATH. On Windows,cmd.exenormally looks in the current directory first, so an executable placed next to the document could shadow a TeX Live program named on the shebang line. runtexshebang setsNoDefaultCurrentDirectoryInExePath=1before running the command to switch that lookup off. - Tampering with
runtexshebang.luaitself requires write access to the TeX Live tree, the same as for any other program shipped with TeX Live; that is covered bytlmgr's signature verification, not by this script. - The exit status of the executed command is passed through, so editors and
makecan detect failures. runtexshebang itself exits 1 on usage or file errors and 2 when no%#!line is found in the first 20 lines. - Only lines that start with
%#!in column 1 are recognised;% #!and%#!are ignored on purpose, which lets you keep alternative command lines in the file as plain comments. A UTF-8 byte order mark before%#!on the first line is ignored, and CRLF line endings are handled on every platform.
References
Commentary
- TeXworks、TeXShop、VSCodeでTeX-style shebangしてみた - Qiita (in Japanese)
Tools supporting %#!
Enjoy Happy TeXing!
License
This program is licensed under the conditions of the LaTeX Project Public License, either version 1.3c of this license or any later version.
Munehiro Yamamoto https://github.com/munepi
Download the contents of this package in one zip archive (66.9k).
RunTeXshebang – A Lua script running LaTeX document files with TeX-style shebang
In short, a TeX-style shebang (%#!) is a special kind of TeX comment that you include in your TeX/LaTeX document file to tell the operating system’s shell how to run the file for the rest of the file:
%#!lualatex foo.tex
\documentclass{article}
\begin{document}
Hello, {\LaTeX} World!
Happy {\TeX}ing.
\end{document}
If you are using a TeX-style shebang, it must appear on the line that matched 20 lines or less in your LaTeX document, and it has to start with a TeX comment symbol (%) followed by a hash sign (#) and an exclamation mark (!), colloquially known as the bang, hence the name shebang for TeX/LaTeX.
runtexshebang executes the %#! line through the system shell, so only use it on documents you trust. It is a build tool invoked by the user and must not be added to shell_escape_commands.
| Package | RunTeXshebang |
| Home page | |
| Bug tracker | |
| Repository | |
| Version | 1.1 2026-10-21 |
| Licenses | The LaTeX Project Public License 1.3c |
| Copyright | 2021–2026 Munehiro Yamamoto |
| Maintainer | Munehiro Yamamoto |
| Contained in | TeX Live as runtexshebang MiKTeX as runtexshebang |
| Topics | Compilation |