TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've seen some examples where shell scripts are incorporated into LaTeX documents. However, I haven't seen any example of an entire LaTeX document being incorporated into a shell script. Does anyone have an example of what this would look like? [Not just a reference to the LaTeX file, but actually include the entire document into the script -- from the opening class to the document end.]

I was thinking about having the shell script contain the command line to build the LaTeX document, followed by the entire LaTeX document (incorporated directly into the script), and then a sleep command wile the document builds, and then a latexmk -c command at the end.

share|improve this question
up vote 16 down vote accepted

I will assume that by shell you mean a Unix shell such as bash. You may want to look at the "here document" feature of bash. The simplest you can do is


latex <<theend
Blah blah
echo "Done!"

however this may lead to some problems if the latex processing needs multiple passes. It will also clutter your current directory with bunch of extra files produced by latex, such as log file, .aux file, etc., which may not be what you want.

A better option is to create a temporary directory, extract the LaTeX document into it using cat and to run LaTeX with this document as input. You may want to run LaTeX several times to make sure all cross references are resolved. At the end you just copy the generated .dvi or .pdf file back to your current directory.

An example of such script is below. It uses mktemp to create a temporary directory. As far as I know, mktemp is not available on every Unix, but it should be available on every system that has GNU coreutils.

There are probably better ways how to handle pretty much every step of the process, but this should get you started.


# Create a temporary directory
curdir=$( pwd )
tmpdir=$( mktemp -dt "latex.XXXXXXXX" )

# Set up a trap to clean up and return back when the script ends
# for some reason
clean_up () {
    cd "$curdir"
    [ -d "$tmpdir" ] && rm -rf "$tmpdir"

# Switch to the temp. directory and extract the .tex file
cd $tmpdir
# Quoting the 'THEEND' string prevents $-expansion.
cat > myfile.tex <<'THEEND'
Blah blah \(x^2 + 1\) or $x^2 + 1$.

# If the file extracts succesfully, try to run pdflatex 3 times.
# If something fails, print a warning and exit
if [[ -f 'myfile.tex' ]]
   for i in {1..3}
      if pdflatex myfile.tex
         echo "Pdflatex run $i finished."
         echo "Pdflatex run $i failed."
         exit 2
   echo "Error extracting .tex file"
   exit 1

# Copy the resulting .pdf file to original directory and exit
cp myfile.pdf $curdir
exit 0
share|improve this answer
Yes -- that is exactly what I had in mind. If it is not too much trouble, can you please insert into your example the cat line of code to help deal with the potential problem you mentioned. – lawlist Mar 11 '13 at 16:32
@JanHlavacek But your document cannot use $ for math. – JLDiaz Mar 12 '13 at 8:17
@JLDiaz You say that like it's a bad thing! – Loop Space Mar 12 '13 at 13:47
WOW -- what an awesome script! I'll put the example of dealing with math and dollar signs into the hidden comments so that I don't forget. This opens up a whole world of new possibilities for processing latex documents that I was unaware of, and completely avoids the need to run a sleep command and then latexmk -c, and it is a workaround to the inability of TeXLive on OSX to use the aux_dir and aux-directory options. – lawlist Mar 12 '13 at 16:15
See the zeroth's edit: enclosing the THEEND string in single quotes prevents $ expansion and makes all the quoting unncessary. – Jan Hlavacek Mar 12 '13 at 16:44

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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