3 # Script to make a LaTeX file for Lilypond
5 # Written by Jan Arne Fagertun <Jan.A.Fagertun@energy.sintef.no>
6 # Sat Nov 22 22:26:43 CET 1997
8 # $Id: ly2dvi.sh,v 1.16 1998/05/08 21:18:34 janaf Exp $
10 # Original LaTeX file made by Mats Bengtsson, 17/8 1997
15 IDENTIFICATION="$NAME $VERSION"
17 echo "$IDENTIFICATION" 1>&2
20 # prevent orphaned "Lily is here" strings
26 # - robustification: give \nonstopmode as LaTeX arg; no hanging if no TeX file.
27 # - robustification: notice failed cp.
30 # - more papersizes (thanks Han-Wen) - don't expect LaTeX to accept all...
31 # - -W,--Width= : set paper width (points)
32 # - -H,--Height= : set paper height (points)
33 # - -H and -W is used only when an unknown papersize is to be specified,
34 # and has to be combined with a papersize option known to LaTeX
35 #NB! - -F,--headers= : name of additional LaTeX headers input file.
36 #NB! Changed from -H to -F
37 # - -d,--dependencies : passed to lilypond
42 # - vertical margins (for a4), same on both sides
43 # - textheight from lilypond output file (mudelapapertextheight)
45 # - mudelapiece, mudelaopus
48 # - -K,--keeplilypond : Keep lilypond output files (default delete)
49 # - -k,--keeply2dvi : Keep ly2dvi output files (default delete)
50 # - -L,--landscape : Set landscape orientation
51 # - -N,--nonumber : Turn off page numbering (\pagestyle{empty})
52 # - Could not reinsert "\usepackage[T1]{fontenc}" because
53 # "los-toros" won't work with it
54 # - Ekstra LaTeX headers from input file
56 # - option to remove output of lily
58 # - Trap Lilypond abort
59 # - Replaced "\usepackage[T1]{fontenc}" with
60 # \usepackage[latin1]{inputenc} (takk, Mats)
61 # - Removed "()" around "\LilyIdString" (Janne didn't want it)
62 # 0.8 - Trap Lilypond segmentation fault
63 # - Function for cleanup
65 # - More human-readable variables
66 # - Some logics concerning rc-files
68 # - Improved Lilypond error checking
69 # - Output orientation (landscape...). Overrides mudela file
70 # variable orientation="landscape";
71 # - Paper width and height put into variables (only A4!)
72 # - Adjusted top margin to default.....
75 # - Include more papersizes.
76 # - Manual page.......
78 # - LILYINCLUDE update
81 # - handle LILYINCLUDE
85 # - Source rc-files, if present. Files are:
86 # /usr/local/share/lilyrc /etc/lilyrc $HOME/.lilyrc ./.lilyrc
87 # - tee output from Lilypond
88 # - Handles margins for A4 paper (equal on both sides)
89 # - new option -s (--separate) for one LaTeX run per file,
90 # else all files are run together
93 # - More useful ("two-level") debug.
94 # - The Q&D hack to find file names and not handling \include
95 # is replaced by grabbing output file names from Lilypond.
96 # = Detects multiple output files - adds them when running
98 # - Works with multiple input files - no matter if they are
99 # (a mix of) input to or output from Lilypond.
102 # - Still no margins handling.
103 # - We have to discuss how to handle multiple output files
104 # from Lilypond - 'ly2dvi standchen' gives a rather odd
108 # - Always exit after printing help info
110 # - Changes to ensure for more strict grep'ing of parameters
111 # Thanks to from G.B.Stott@bolton.ac.uk
112 # - More efficient use of sed -e 's///' -e 's///'
113 # Thanks to Johan Vromans <jvromans@squirrel.nl> and GBS
114 # - Ask tex for location of titledefs.tex (Thanks to JV)
115 # - Accept only exact match of "\def\mudelacomposer{"
116 # (or whatever mudela* defined in titledefs.tex)
117 # - Even more efficient use of sed (Thanks to JV)
118 # - Default file name for single output file implemented.
119 # - Moved help into function - finally included from 0.1.jcn1
122 # - Still doesn't handle \include
123 # - The Q&D for finding output file name from the sequence of
124 # \paper \output \midi \output really needs to be looked at.
125 # I have improved it a lot, but it's only capable of finding
126 # one (the last) file name.
127 # Well, I have to rewrite this entirely to handle \include,
129 # - Still no margins handling.
132 # - Some lines of output from lilypond do NOT start
133 # at first character position, therefore I have removed "^"
134 # in sed'ing and grep'ing.
137 # - add "Creator: " line to output
140 # - multiple input files to make score from several files
141 # (extra files assumed to be Lilypond output)
142 # - cp dvi-file instead of mv, to allow for xdvi reload
143 # - check for illegal long options
144 # - put in pt in text width, if not given
145 # - put in \nonstopmode in LaTeX file
146 # - restored LaTeX exit value check
149 # - temporarily omit LaTeX exit value check
150 # - remove ALL temporary files
153 # - fix for solaris - included from 0.1.jcn1
154 # - long option hack - included from 0.1.jcn1 - modified
155 # - moved help into function - NOT included from 0.1.jcn1 yet
156 # - various improvements - included from 0.1.jcn1
157 # - find mudela definitions from titledefs.tex
158 # - find papersize from lilypond output file (mudelapapersize),
159 # overridden by option '-p size' or '--papersize=size'
160 # - option -l lang or --language=lang overrides
161 # lilypond output file definition (mudelalanguage)
162 # - textwidth from lilypond output file (mudelapaperlinewidth)
167 # - moved help into function
173 if [ "$KEEP_LY2DVI_OUTPUT" != "Y" ]
175 [ -n "$LatF" -a -f "$LatF" ] && rm -f $LatF
176 [ -n "$LOGFILE" -a -f "$LOGFILE" ] && rm -f $LOGFILE
177 [ -n "$FN" ] && rm -f $FN.*
178 for F in *$$* $TMP/*$$*
183 if [ "$KEEP_LILY_OUTPUT" != "Y" ]
185 for F in $LILY_OUTPUT_FILES
187 [ -f $F ] && rm -f $F
196 Generate dvi file from mudela or lilypond output
197 Usage: $0 [options] file[s]
200 -D,--debug set debug mode
201 -F,--headers= name of additional LaTeX headers file
202 -H,--Height= set paper height (points) (see manual page)
203 -K,--keeplilypond keep lilypond output files
204 -L,--landscape set landscape orientation
205 -N,--nonumber switch off page numbering
206 -O,--orientation= set orientation (obsolete - use -L instead)
207 -W,--Width= set paper width (points) (see manual page)
208 -d,--dependencies tell lilypond make a dependencies file
209 -h,--help this help text
210 -k,--keeply2dvi keep ly2dvi output files
211 -l,--language= give LaTeX language (babel)
212 -o,--output= set output directory
213 -p,--papersize= give LaTeX papersize (eg. a4)
214 -s,--separate run all files separately through LaTeX
216 files may be (a mix of) input to or output from lilypond(1)
220 # Trap function (cleanup)
226 [ -z "$TMP" ] && TMP=/tmp
229 $debug_echo $IDENTIFICATION": temporary directory "$TMP" not found, set to /tmp"
232 LOGFILE=$TMP/lilylog.$$ # Logfile for lilypond
233 PAPERSIZE=a4 # Default papersize name
234 PWIDTH=597 # Default paperwidth
235 PHEIGHT=845 # Default paperheight
236 PNUM="%" # Page numbering on
237 LILYOPTS="" # Options to lilypond
372 echo $0": unknown papersize -- "$PAPERSIZE
380 for D in /usr/local/share/ /etc/ $HOME/. ./.
383 [ -f $RCfile ] && . $RCfile
389 unset ORIENTATION LANGUAGE PAPERSIZE LATEXHF
391 # Keywords defined in titledefs.tex
393 TF=`kpsewhich -n tex tex titledefs.tex`
396 TF=/usr/lib/texmf/tex/lilypond/titledefs.tex
401 MU_DEF=`egrep "^.newcommand...mudela" $TF | \\
402 sed -e 's/^.newcommand...//' -e 's/\\}.*$//'`
407 MU_DEF="mudelatitle mudelasubtitle mudelacomposer \
408 mudelaopus mudelaarranger mudelapiece mudelainstrument"
416 # All files in one LaTeX run
420 # Find command line options and switches
422 # "x:" x takes argument
424 switches="DF:H:KLNO:W:dhkl:o:p:s\?"
427 # ugh, "\-" is a hack to support long options
428 # while getopts \-:$options$switches O
429 # must be in double quotes for bash-2.0
430 while getopts "\-:$options$switches" O
432 $debug_echo "O: \`$O'"
433 $debug_echo "arg: \`$OPTARG'"
436 [ $debug_echo = echo ] && set -x
449 ORIENTATION=landscape
455 PNUM="\pagestyle{empty}"
461 LILYOPTS=$LILYOPTS" -d"
488 $debug_echo "long option: \`$OPTARG'"
491 PHEIGHT=`echo $OPTARG | sed -e s/"^.*="//`
494 PWIDTH=`echo $OPTARG | sed -e s/"^.*="//`
497 LILYOPTS=$LILYOPTS" -d"
500 [ $debug_echo = echo ] && set -x
504 LATEXHF=`echo $OPTARG | sed -e s/"^.*="//`
517 ORIENTATION=landscape
520 LANGUAGE=`echo $OPTARG | sed -e s/"^.*="//`
523 PNUM="\pagestyle{empty}"
526 ORIENTATION=`echo $OPTARG | sed -e s/"^.*="//`
529 OUTPUTDIR=`echo $OPTARG | sed -e s/"^.*="//`
532 PAPERSIZE=`echo $OPTARG | sed -e s/"^.*="//`
538 echo $0": illegal option -- "$OPTARG;
545 shift `expr $OPTIND - 1`
552 $debug_echo $IDENTIFICATION": No input file name given"
558 # Include \def\mudela-definitions
563 # LL=`egrep '^\\\\def.'$L'{' $OF`
564 LL=`egrep '^\\\\def.'$L'{' $File`
567 LL=`egrep '^\\\\def.'$L'{' $OF`
572 LLL=`echo $LL | sed -e 's/}.*$//' -e 's/.*{//'`
576 echo "$SS\\"$L'{'$LLL'}%' >> $LatF
585 BN=`basename $File .tex`
587 if [ "$KEEP_LY2DVI_OUTPUT" != "Y" ]
595 # paper size (PAPERSIZE, overridden by command line option -p)
596 # paper orientation (ORIENTATION, overridden by option -o)
597 # language (LANGUAGE, overridden by option -l)
601 -e 's/\\\\def\\\\mudelalanguage{\([^}]*\).*$/fLNG=\1;/p' \\
602 -e 's/\\\\def\\\\mudelalatexheaders{\([^}]*\).*$/fLHF=\1;/p' \\
603 -e 's/\\\\def\\\\mudelaorientation{\([^}]*\).*$/fORI=\1;/p' \\
604 -e 's/\\\\def\\\\mudelapaperlinewidth{\([^}]*\).*$/TWN=\1;/p' \\
605 -e 's/\\\\def\\\\mudelapapertextheight{\([^}]*\).*$/THN=\1;/p' \\
606 -e 's/\\\\def\\\\mudelapapersize{\([^}]*\).*$/fPSZ=\1;/p' \\
616 [ -f $LATEXHF ] && LLHF="\input{$LATEXHF}"
619 if [ -z "$PAPERSIZE" ]
623 if [ -n "$PAPERSIZE" ]
629 if [ -z "$ORIENTATION" ]
633 if [ -n "$ORIENTATION" ]
635 if [ -z "$PAPEROPT" ]
637 PAPEROPT=$ORIENTATION
639 PAPEROPT=$PAPEROPT,$ORIENTATION
643 if [ -n "$PAPEROPT" ]
645 PAPER="["$PAPEROPT"]"
648 if [ -z "$LANGUAGE" ]
652 if [ -n "$LANGUAGE" ]
654 LLNG="\usepackage["$LANGUAGE"]{babel}"
676 $debug_echo "Text width = "$TW
678 TWp=`echo $TW | sed -e 's/\..*$//'`
697 $debug_echo "Text height = "$TH
699 THp=`echo $TH | sed -e 's/\..*$//'`
701 if [ "$ORIENTATION" = "landscape" ]
706 HMARG=`expr $PWp - $TWp`
707 HMARG=`expr $HMARG / 2`"pt"
708 $debug_echo "Text left = "$HMARG
709 VMARG=`expr $PHp - $THp`
710 VMARG=`expr $VMARG / 2`"pt"
711 $debug_echo "Text top = "$VMARG
713 # Geometry: /var/lib/texmf/latex/geometry/geometry.dvi
719 % Creator: $IDENTIFICATION
720 % Automatically generated from $IF, $NOW
722 \documentclass$PAPER{article}
725 \usepackage{geometry}
726 \usepackage[latin1]{inputenc}
727 %\usepackage[T1]{fontenc}
729 %\addtolength{\oddsidemargin}{-1cm}
730 %\addtolength{\topmargin}{-1cm}
731 %\setlength{\textwidth}{$TW}
732 %\setlength{\textheight}{$TH}
733 %\geometry{width=$TW, left=$HMARG, top=1cm}
734 %\geometry{width=$TW, left=$HMARG}
735 %\geometry{height=$TH, top=$VMARG}
736 % \geometry{body={$PWp pt, $PHp pt}, width=$TW, top=$HMARG, height=$TH, top=$VMARG}
737 \geometry{width=$TW, top=$HMARG, height=$TH, top=$VMARG}
755 \def\theopus{\mudelaopus}% ugh
756 \def\thepiece{\mudelapiece}%
765 \vfill\hfill{\LilyIdString}
771 latex '\nonstopmode \input '$LatF || exit 5
778 [ -n "$OUTPUTDIR" ] && RESULT="$OUTPUTDIR/$RESULT"
780 cp $FN.dvi $RESULT || exit 5
788 $IDENTIFICATION: dvi file name is $RESULT
793 # ugh. GF is side-effect.
795 # should check for LILYINCLUDE
796 for lypath in "." `echo $LILYINCLUDE| sed 's/:/ /g'`
798 if [ -f "$lypath/$1" ]
804 if [ -f "$lypath/$1.ly" ]
810 $debug_echo $IDENTIFICATION": Input file "$GF" not found"
811 echo $NAME": Input file "$GF" not found" 1>&2
815 # Loop through all files
825 # Check whether the file is input to or output from lilypond
828 OP=`echo $L1 | grep "^% Creator: GNU LilyPond"`
832 # OK - it's the output from lilypond.
834 # Get lilypond source file name
837 IFL=`grep mudelafilename $OF`
840 IF=`echo $IFL | sed -e 's/.*{//' -e 's/}*.$//'`
842 # Check if source file exists
846 $debug_echo $IDENTIFICATION": Mudela file not found."
851 $debug_echo $IDENTIFICATION": Mudela file name not found."
857 # I have to assume this is the lilypond input file
858 # Find output file name, if defined
863 # Grab output file names
865 $debug_echo "lilypond "$LILYOPTS $IF
867 lilypond $LILYOPTS $IF 2>&1 | tee $LOGFILE
868 OF=`egrep '^TeX output to ' $LOGFILE | \\
869 sed -e 's/TeX output to//' -e 's/\.\.\.//'`
870 $debug_echo "==> "$OF
871 STATUS=`egrep -i "error|segmentation|abort" $LOGFILE`
873 if [ ! -z "$STATUS" ]
879 # "Spin through" all the files
883 $debug_echo "--- "$File
885 # Check if output file is generated
889 $debug_echo $IDENTIFICATION": hmm, I could not find the output file "$File
893 # Is this the first file?
910 LILY_OUTPUT_FILES="$LILY_OUTPUT_FILES $OF"