37

Under UNIX, to my knowledge, any regular file can be made executable with the addition of a shebang:

#! /path/to/executing-program --with --options

How can I apply the same principle to a TeX file without altering syntax rules for the entire document?

$ cat > tmp.tex
#! /usr/bin/env tex --mystery --options
hello, world
\bye
$ chmod +x tmp.tex
$ ./tmp.tex
$ dvi2tty -q tmp.dvi | grep .
hello, world
                                   1
$ logout
37

This is file seanenv.tex:

#! /usr/bin/env pdflatex \catcode35=14 \input
\catcode`\#=6
\documentclass{article}
\begin{document}
Hello, Sean!
\end{document}

After chmod u+x seanenv.tex, I can issue

./seanenv.tex

and the seanenv.pdf file is compiled.

Note that 35 is the character code of #, which must be made into a comment character before TeX rereads the file. After this, # is given back its category code 6.

This is not limited to pdflatex; the same strategy will work with tex and so on.

  • 2
    strange that's what I tried but on cygwin it just hangs, no log no error no termination. I couldn't inject any amount of quoting to make it work. (I somehow doubt you tested cygwin:-) – David Carlisle Dec 8 '14 at 15:29
  • @DavidCarlisle Does Cygwin full support file permissions? I know that the underlying Windows system doesn't, really... – Sean Allred Dec 8 '14 at 15:42
  • See also this additional answer – Sean Allred Dec 8 '14 at 15:58
  • This may work, but is not guaranteed to: The Sheebang line MUST handle only one argument to the command (which is /usr/bin/env in your case) - the handling of more arguments is highly non-portable. – Martin Schröder Dec 10 '14 at 10:19
  • 1
    @MartinSchröder I agree with you; this seems to work on the Unix systems where I tested it (Mac OS X and GNU/Linux). However, env allows env utility argument ..., at least on a POSIX compliant system. – egreg Dec 10 '14 at 11:30
12

It hangs for me as well. Changing the first line to use the path to pdflatex (instead of going via /usr/bin/env) fixes it for me:

#! /bin/pdflatex \catcode35=14 \input

Change /bin/pdflatex if needed to the full path for pdflatex. The command:

which pdflatex

will tell you what this is.

  • 1
    curious for me on cygwin that runs but it always runs plain tex not latex (so it's not picking up the command name correctly to default the format) so finally this works #! /usr/local/texlive/2014/bin/x86_64-cygwin/pdflatex &latex \catcode35=14 \input – David Carlisle Dec 8 '14 at 15:55
  • 1
    Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. Thanks for the additional information :) – Sean Allred Dec 8 '14 at 19:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.