#!/bin/bash # # Syntax: mk_2up [-h] [-p] [-m margin] [-b border] [-x width] [dvi_file [...]] # # Purpose: Creates 2up pdf files for the given dvi files. # The -p option indicates that a postscript file should be generated # instead of a pdf file. # If no input argument is given the most recently changed .dvi file # in the current directory is processed. # Use the -m option to specify a margin around the whole page # (default: 10). # Use the -b option to specify a margin around a single subpage # (default: 0). # Use the -x option to draw a box around each subpage with the given # width (default value: 0). # The -h flag shows the help info for this command. # Created: Mar 15, 2003 by Bart De Schutter # Last revised: Feb 11, 2011 by Bart De Schutter # # See http://www.deschutter.info/util/scripts.html for the latest version of # this script. # Status: public # Category: latex, pdf # # Process input arguments. # margin="10" border="0" box_width="0" psnup_options="" postscript=0 dvips_options="-Ppdf -G0" while getopts :hpm:b:x: option do case "$option" in h) sed '/^$/q;/^#!/d;s/^# //;s/^#//' $0 >&2 exit 1;; p) postscript=1 dvips_options="";; m) margin=$OPTARG;; b) border=$OPTARG;; x) box_width=$OPTARG;; :) echo >&2 echo >&2 "Option -$OPTARG requires an argument." echo >&2 sed '/^$/q;/^#!/d;s/^# //;s/^#//' $0 >&2 echo >&2 exit 1;; \?) echo >&2 echo >&2 "Unknown option: $OPTARG" echo >&2 sed '/^$/q;/^#!/d;s/^# //;s/^#//' $0 >&2 exit 1;; esac done shift $(($OPTIND-1)) if [ $border != 0 ] then psnup_options="$psnup_options -b$border" fi if [ $box_width != 0 ] then psnup_options="$psnup_options -d$box_width" fi # # Define temporary directory and template. # if [[ -d ~/tmp ]] then tmp=~/tmp elif [[ ( -n "$TMPDIR" ) || ( ! -d "$TMPDIR" ) ]] then tmp="$TMPDIR" elif [[ -d /tmp ]] then tmp=/tmp else tmp=`pwd` fi tmp_template="$tmp/${0##*/}__$(date '+%Y_%m_%d__%H_%M_%S')__XXXXXXXXXXXX"\ # # Determine which files should be processed. # declare -a dvi_files # Declare as array. This is necessary to be able to deal # with file names that contain spaces. if [ $# -eq 0 ]; then dvi_files=$( (ls -t *.dvi | head -n 1) 2>/dev/null ) if [ -z "$dvi_files" ]; then echo "No .dvi files found." exit 1 fi else dvi_files=("$@") fi # # Process dvi files. # for fil in "${dvi_files[@]}" do fil="${fil%.dvi}" echo "Processing $fil" tmp_ps_fil=$(mktemp "${tmp_template}") || exit 1 \dvips ${dvips_options} "${fil}.dvi" -o "$tmp_ps_fil" psnup -2 -pa4 -m${margin} $psnup_options "$tmp_ps_fil" "${fil}_2up.ps" \rm "$tmp_ps_fil" 2>/dev/null if [ $postscript -eq 0 ]; then ps2pdf13 -sPAPERSIZE=a4 "${fil}_2up.ps" "${fil}_2up.pdf" \rm "${fil}_2up.ps" 2>/dev/null echo "Created file ${fil}_2up.pdf" else echo "Created file ${fil}_2up.ps" fi echo # or, with pipes: # \dvips -Ppdf -G0 "${fil}.dvi" -o - | \ # psnup -2 -pa4 -m${margin} | \ # ps2pdf13 -sPAPERSIZE=a4 - "${fil}_2up.pdf" done