#!/bin/bash # # Syntax: mk_4up [-h] [-p] [-P] [-c] [-m margin] [-b border] [-x width] # [dvi_file [...]] # # Purpose: Creates 4up pdf files for the given dvi files. If no input argument # is given the most recently changed .dvi file in the current # directory is processed. # The -p option indicates that a postscript file should be generated # instead of a pdf file. # mk_4up assumes that pages are in landscape mode, use option -P # for portrait mode. # mk_4up normally uses `row-major' layout. The -c option changes # the order to `column-major', where successive pages are placed in # columns down the paper. # 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" landscape_opt="-l" border="0" box_width="0" psnup_options="" col_mode="" postscript=0 dvips_options="-Ppdf -G0" while getopts :Pphcm:b:x: option do case "$option" in h) sed '/^$/q;/^#!/d;s/^# //;s/^#//' $0 >&2 exit 1;; p) postscript=1 dvips_options="";; P) landscape_opt="";; c) col_mode="-c";; 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 -4 -pa4 -m${margin} $landscape_opt $col_mode $psnup_options \ "$tmp_ps_fil" "${fil}_4up.ps" \rm "$tmp_ps_fil" 2>/dev/null if [ $postscript -eq 0 ]; then ps2pdf13 -sPAPERSIZE=a4 "${fil}_4up.ps" "${fil}_4up.pdf" \rm "${fil}_4up.ps" 2>/dev/null echo "Created file ${fil}_4up.pdf" else echo "Created file ${fil}_4up.ps" fi echo done