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 # Original LaTeX file made by Mats Bengtsson, 17/8 1997
13 IDENTIFICATION="$NAME $VERSION"
15 echo "$IDENTIFICATION" 1>&2
22 # - option to remove output of lily
24 # 0.9 - Trap Lilypond abort
25 # - Replaced "\usepackage[T1]{fontenc}" with
26 # \usepackage[latin1]{inputenc} (takk, Mats)
27 # - Removed "()" around "\LilyIdString" (Janne didn't want it)
29 # 0.8 - Trap Lilypond segmentation fault
30 # - Function for cleanup
32 # - More human-readable variables
33 # - Some logics concerning rc-files
35 # - Improved Lilypond error checking
36 # - Output orientation (landscape...). Overrides mudela file
37 # variable orientation="landscape";
38 # - Paper width and heigth put into variables (only A4!)
39 # - Adjusted top margin to default.....
42 # - Include more papersizes.
43 # - Manual page.......
45 # - LILYINCLUDE update
48 # - handle LILYINCLUDE
52 # - Source rc-files, if present. Files are:
53 # /usr/local/share/lilyrc /etc/lilyrc $HOME/.lilyrc ./.lilyrc
54 # - tee output from Lilypond
55 # - Handles margins for A4 paper (equal on both sides)
56 # - new option -s (--separate) for one LaTeX run per file,
57 # else all files are run together
60 # - More useful ("two-level") debug.
61 # - The Q&D hack to find file names and not handling \include
62 # is replaced by grabbing output file names from Lilypond.
63 # = Detects multiple output files - adds them when running
65 # - Works with multiple input files - no matter if they are
66 # (a mix of) input to or output from Lilypond.
69 # - Still no margins handling.
70 # - We have to discuss how to handle multiple output files
71 # from Lilypond - 'ly2dvi standchen' gives a rather odd
75 # - Always exit after printing help info
77 # - Changes to ensure for more strict grep'ing of parameters
78 # Thanks to from G.B.Stott@bolton.ac.uk
79 # - More efficient use of sed -e 's///' -e 's///'
80 # Thanks to Johan Vromans <jvromans@squirrel.nl> and GBS
81 # - Ask tex for location of titledefs.tex (Thanks to JV)
82 # - Accept only exact match of "\def\mudelacomposer{"
83 # (or whatever mudela* defined in titledefs.tex)
84 # - Even more efficient use of sed (Thanks to JV)
85 # - Default file name for single output file implemented.
86 # - Moved help into function - finally included from 0.1.jcn1
89 # - Still doesn't handle \include
90 # - The Q&D for finding output file name from the sequence of
91 # \paper \output \midi \output really needs to be looked at.
92 # I have improved it a lot, but it's only capable of finding
93 # one (the last) file name.
94 # Well, I have to rewrite this entirely to handle \include,
96 # - Still no margins handling.
99 # - Some lines of output from lilypond do NOT start
100 # at first character position, therefore I have removed "^"
101 # in sed'ing and grep'ing.
104 # - add "Creator: " line to output
107 # - multiple input files to make score from several files
108 # (extra files assumed to be Lilypond output)
109 # - cp dvi-file instead of mv, to allow for xdvi reload
110 # - check for illegal long options
111 # - put in pt in text width, if not given
112 # - put in \nonstopmode in LaTeX file
113 # - restored LaTeX exit value check
116 # - temporarily omit LaTeX exit value check
117 # - remove ALL temporary files
120 # - fix for solaris - included from 0.1.jcn1
121 # - long option hack - included from 0.1.jcn1 - modified
122 # - moved help into function - NOT included from 0.1.jcn1 yet
123 # - various improvements - included from 0.1.jcn1
124 # - find mudela definitions from titledefs.tex
125 # - find papersize from lilypond output file (mudelapapersize),
126 # overridden by option '-p size' or '--papersize=size'
127 # - option -l lang or --language=lang overrides
128 # lilypond output file definition (mudelalanguage)
129 # - textwidth from lilypond output file (mudelapaperlinewidth)
134 # - moved help into function
140 $debug_echo "("$LF")("$FN")("$LOGFILE")"
141 if [ "$KEEP_LY2DVI_OUTPUT" != "Y" ]
143 [ -n "$LF" -a -f "$LF" ] && rm -f $LF
144 [ -n "$LOGFILE" -a -f "$LOGFILE" ] && rm -f $LOGFILE
145 [ -n "$FN" ] && rm -f $FN.*
153 Generate dvi file from mudela or lilypond output
154 Usage: $0 [options] file[s]
157 -D, --debug set debug mode
158 -O, --orientation= set orientation (landscape or portrait (default))
159 -o, --output= set output directory
160 -h, --help this help text
161 -k, --keep keep LaTeX file
162 -l, --language= give LaTeX language (babel)
163 -p, --papersize= give LaTeX papersize (eg. a4paper)
164 -s, --separate run all files separately through LaTeX
166 files may be (a mix of) input to or output from lilypond(1)
170 # Trap function (cleanup)
176 [ -z "$TMP" ] && TMP=/tmp
179 $debug_echo $IDENTIFICATION": temporary directory "$TMP" not found, set to /tmp"
182 LOGFILE=$TMP/lilylog.$$ # Logfile for lilypond
183 PWIDTH=600; # Width of A4 paper!
184 PHEIGTH=845; # Heigth of A4 paper!
188 for D in /usr/local/share/ /etc/ $HOME/. ./.
191 [ -f $RCfile ] && . $RCfile
196 unset ORIENTATION LANGUAGE PAPERSIZE
198 # Keywords defined in titledefs.tex
200 TF=`kpsewhich -n tex tex titledefs.tex`
203 TF=/usr/lib/texmf/tex/lilypond/titledefs.tex
208 MU_DEF=`egrep "^.newcommand...mudela" $TF | \\
209 sed -e 's/^.newcommand...//' -e 's/\\}.*$//'`
214 MU_DEF="mudelatitle mudelasubtitle mudelacomposer \
215 mudelaarranger mudelainstrument"
223 # All files in one LaTeX run
227 # Find command line options and switches
229 # "x:" x takes argument
231 switches="DO:hkl:o:Kp:s\?"
234 # ugh, "\-" is a hack to support long options
235 # while getopts \-:$options$switches O
236 # must be in double quotes for bash-2.0
237 while getopts "\-:$options$switches" O
239 $debug_echo "O: \`$O'"
240 $debug_echo "arg: \`$OPTARG'"
243 [ $debug_echo = echo ] && set -x
278 $debug_echo "long option: \`$OPTARG'"
281 [ $debug_echo = echo ] && set -x
292 LANGUAGE=`echo $OPTARG | sed -e s/"^.*="//`
295 PAPERSIZE=`echo $OPTARG | sed -e s/"^.*="//`
298 ORIENTATION=`echo $OPTARG | sed -e s/"^.*="//`
301 OUTPUTDIR=`echo $OPTARG | sed -e s/"^.*="//`
307 echo $0": illegal option -- "$OPTARG;
314 shift `expr $OPTIND - 1`
321 $debug_echo $IDENTIFICATION": No input file name given"
329 BN=`basename $File .tex`
331 if [ "$KEEP_LY2DVI_OUTPUT" != "Y" ]
339 # paper size (PAPERSIZE, overridden by command line option -p)
340 # paper orientation (ORIENTATION, overridden by option -o)
341 # language (LANGUAGE, overridden by option -l)
345 -e 's/\\\\def\\\\mudelapapersize{\([^}]*\).*$/fPSZ=\1;/p' \\
346 -e 's/\\\\def\\\\mudelaorientation{\([^}]*\).*$/fORI=\1;/p' \\
347 -e 's/\\\\def\\\\mudelalanguage{\([^}]*\).*$/fLNG=\1;/p' \\
348 -e 's/\\\\def\\\\mudelapaperlinewidth{\([^}]*\).*$/TWN=\1;/p' \\
350 if [ -z "$PAPERSIZE" ]
354 if [ -n "$PAPERSIZE" ]
359 if [ -z "$ORIENTATION" ]
363 if [ -n "$ORIENTATION" ]
365 if [ -z "$PAPEROPT" ]
367 PAPEROPT=$ORIENTATION
369 PAPEROPT=$PAPEROPT,$ORIENTATION
373 if [ -n "$PAPEROPT" ]
375 PAPER="["$PAPEROPT"]"
378 if [ -z "$LANGUAGE" ]
382 if [ -n "$LANGUAGE" ]
384 LLNG="\usepackage["$LANGUAGE"]{babel}"
406 $debug_echo "Text width = "$TW
408 TWp=`echo $TW | sed -e 's/\..*$//'`
410 if [ "$ORIENTATION" = "landscape" ]
414 MARG=`expr $PWp - $TWp`
415 MARG=`expr $MARG / 2`"pt"
417 # Geometry: /var/lib/texmf/latex/geometry/geometry.dvi
423 % Creator: $IDENTIFICATION
424 % Automatically generated from $IF, $NOW
426 \documentclass$PAPER{article}
429 \usepackage{geometry}
430 %\usepackage[T1]{fontenc}
431 \usepackage[latin1]{inputenc}
432 %\addtolength{\oddsidemargin}{-1cm}
433 %\addtolength{\topmargin}{-1cm}
434 \setlength{\textwidth}{$TW}
435 %\geometry{width=$TW, left=$MARG, top=1cm}
436 \geometry{width=$TW, left=$MARG}
442 # Include \def\mudela-definitions
446 LL=`egrep '^\\\\def.'$L'{' $OF`
449 LLL=`echo $LL | sed -e 's/}.*$//' -e 's/.*{//'`
452 echo "\\"$L'{'$LLL'}%' >> $LF
466 \vfill\hfill{\LilyIdString}
479 [ -n "$OUTPUTDIR" ] && RESULT="$OUTPUTDIR/$RESULT"
491 $IDENTIFICATION: dvi file name is $RESULT
496 # ugh. GF is side-effect.
498 # should check for LILYINCLUDE
499 for lypath in "." `echo $LILYINCLUDE| sed 's/:/ /g'`
501 if [ -f "$lypath/$1" ]
507 if [ -f "$lypath/$1.ly" ]
513 $debug_echo $IDENTIFICATION": Input file "$GF" not found"
514 echo $NAME": Input file "$GF" not found" 1>&2
518 # Loop through all files
528 # Check whether the file is input to or output from lilypond
531 OP=`echo $L1 | grep "^% Creator: GNU LilyPond"`
535 # OK - it's the output from lilypond.
537 # Get lilypond source file name
540 IFL=`grep mudelafilename $OF`
543 IF=`echo $IFL | sed -e 's/.*{//' -e 's/}*.$//'`
545 # Check if source file exists
549 $debug_echo $IDENTIFICATION": Mudela file not found."
553 $debug_echo $IDENTIFICATION": Mudela file name not found."
558 # I have to assume this is the lilypond input file
559 # Find output file name, if defined
564 # Grab output file names
566 $debug_echo "lilypond "$IF
568 lilypond $IF 2>&1 | tee $LOGFILE
569 OF=`egrep '^TeX output to ' $LOGFILE | \\
570 sed -e 's/TeX output to//' -e 's/\.\.\.//'`
571 $debug_echo "==> "$OF
572 STATUS=`egrep -i "error|segmentation|abort" $LOGFILE`
574 if [ ! -z "$STATUS" ]
580 # "Spin through" all the files
584 $debug_echo "--- "$File
586 # Check if output file is generated
590 $debug_echo $IDENTIFICATION": hmm, I could not find the output file "$File
594 # Is this the first file?
609 LILY_OUTPUT_FILES="$LILY_OUTPUT_FILES $OF"
619 if [ $KEEP_LILY_OUTPUT = N ]
621 rm $LILY_OUTPUT_FILES