\write18 (LaTeX2e unofficial reference manual (November 2018))

Previous: \wlog, Up: \write [Contents][Index]

#### 27.5.3 `\write18`

Synopsis:

    \write18{shell_command}

Issue a command to the operating system shell. The operating system runs the command and LaTeX’s execution is blocked until that finishes.

This

    \usepackage{graphicx}  % in preamble
      ...
    \newcommand{\fignum}{1}
    \immediate\write18{cd pix && asy figure\fignum}
    \includegraphics{pix\figure\fignum.pdf}

will run `Asymptote` on `pix/figure1.asy`, so that the document can later read in the resulting graphic (see \includegraphics). Like any `\write`, here LaTeX expands macros in shell_command so that `\fignum` is replaced by ‘`1`’.

Another example is that you can automatically run BibTeX at the start of each LaTeX run (see Using BibTeX) by including `\immediate\write18{bibtex8 \jobname}` as the first line of the file. Note that `\jobname` gives the base name of the current file.

You sometimes need to do a multi-step process to get the information that you want. This will insert into the input a list of all PDF files in the current directory.

    \immediate\write18{ls *.pdf > tmp.dat}
    \input{tmp.dat}

The standard behaviour of any `\write` is to wait until a page is being shipped out before expanding the macros or writing to the stream (see \write). But sometimes you want it done now. For this, use `\immediate\write18{shell_command}`.

There are obvious security issues with allowing system commands inside a LaTeX file. If you download a file off the net and it contains commands to delete all your files then you would be annoyed. The standard settings in modern distributions turn off full shell access. You can turn it on, if you are sure the shell commands are safe, by compiling with `latex --shell-escape filename` for TeX Live or MacTeX or `latex --enable-write18 filename` for MiKTeX (see Command line options).

In the place of full shell access, modern distributions by default use a restricted version that allows some commands to work, such as those that run Metafont to generate missing fonts, even if you do not use the `shell-escape` or `enable-write18` option. By default this list of allowed commands is short and features only commands that are under the control of the distribution maintainers (see Command line options).

Some packages need to run external system commands. For instance `sagetex` allows you to have the mathematics software system Sage do calculations or draw graphs and then incorporate that output in your document. For this you must compile with full `\write18` capability turned on.

Unofficial LaTeX2e reference manual