Turning Latex into PostScript

Introduction

Latex often needs to be run together with dvips, and there is an annoying clutter of files produced. This is even more so when Bibtex referencing is employed. The Linux scripts t2ps and b2ps simplify the generation of PS files from latex source, and delete the clutter afterwards.

If you are targeting some format other than PostScript, then that is possible too. See the section LaTeX/Export To Other Formats in en.wikibooks.org/wiki/LaTeX. My own ps_notes.html may also be of use.

To install these shell scripts, copy them to files named t2ps and b2ps in your bin directory, i.e. to ~/bin/, type chmod +x t2ps b2ps to make them executable, and try them out. (C shell users will also need to type rehash, to update their PATH cache. This just needs to be done once.)

t2ps

This script is sufficient for processing documents without bibliographies.

#!/bin/tcsh
# This script runs latex on the specified .tex file(s).  It then
# runs dvips, provided there are no serious errors during the
# running of latex.  Any number of .tex files may be specified.
#
# Examples:
#     t2ps cv.tex            (process cv.tex)
#     t2ps intro main        (process intro.tex and main.tex)
#     t2ps chapter*.tex      (process chapter*.tex)
#     t2ps doc1 doc1         (process doc1.tex twice)
#     t2ps                   (script will prompt for a file name)
# 
# As may be seen from these examples, the extension '.tex' is optional 
# on the command line, and wildcard characters ('*' and '?') are allowed.  
#
# It is possible to specify a .tex file in a directory other than
# the pwd, but doing so is unconventional and not advisable.

if ($#argv == 0) then
  echo -n "File(s): "
  set filelist = ($<)
else
  set filelist = ($argv[*])
endif

# Strip extensions, if present
set filelist = ($filelist:gr)

# Process each file, checking first that each exists
foreach filename ( $filelist )
  if ( -e $filename.tex && -f $filename.tex ) then
    latex $filename.tex && dvips $filename -o && rm $filename.{dvi,log} 
  else
    echo "t2ps: input file $filename.tex not found"
  endif
end

b2ps

This version runs Bibtex as well as Latex, in order to generate the bibliography afresh.

#!/bin/tcsh
# This script runs latex and bibtex on the specified .tex file(s).  
# It then runs dvips, provided there are no serious errors during the
# running of latex.  Any number of .tex files may be specified.
#
# Examples:
#     b2ps cv.tex            (process cv.tex)
#     b2ps intro main        (process intro.tex and main.tex)
#     b2ps chapter*.tex      (process chapter*.tex)
#     b2ps doc1 doc1         (process doc1.tex twice)
#     b2ps                   (script will prompt for a file name)
# 
# As may be seen from these examples, the extension '.tex' is optional 
# on the command line, and wildcard characters ('*' and '?') are allowed.  
#
# It is possible to specify a .tex file in a directory other than
# the pwd, but doing so is unconventional and not advisable.

if ($#argv == 0) then
  echo -n "File(s): "
  set filelist = ($<)
else
  set filelist = ($argv[*])
endif

# Strip extensions, if present
set filelist = ($filelist:gr)

# Process each file, checking first that each exists
foreach filename ( $filelist )
  if ( -e $filename.tex && -f $filename.tex ) then
    latex $filename.tex && bibtex $filename && \
    latex $filename.tex && latex $filename.tex && \
    dvips $filename -o && rm $filename.{dvi,log,blg,bbl,aux} 
  else
    echo "b2ps: input file $filename.tex not found"
  endif
end

Minor programming points

You may be most familiar with the 'bash' shell, and bash is quite likely to be your default shell. Nevertheless, you may notice, from the first line of each of the scripts, that I specify /bin/tcsh as the processing shell. I use tcsh here simply because I reckon that it parses file paths very neatly — more neatly than bash can. Using tcsh in this way is possible, whatever shell you use as your default.

The scripts listed above deal with the cases of documents without (t2ps) and with (b2ps) bibliographies. They don't help with documents that include an index as well. In that case you will need a command resembling

   rm -f $chapX.{bbl,ind} && latex $chapX && \
   bibtex $chapX && makeindex -s svind.ist $chapX && \
   latex $chapX && latex $chapX && dvips $chapX -o && \
   rm $chapX.{ind,ilg,blg,dvi,log,aux,idx}

Obviously you will need to customize this command.

 

Validate HTML CSS Last changed 2011-03-16 Chris Rennie