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
20 # - -K,--keeplilypond : Keep lilypond output files (default delete)
21 # - -k,--keeply2dvi : Keep ly2dvi output files (default delete)
22 # - -L,--landscape : Set landscape orientation
23 # - -N,--nonumber : Turn off page numbering (\pagestyle{empty})
24 # - Could not reinsert "\usepackage[T1]{fontenc}" because
25 # "los-toros" won't work with it
26 # - Ekstra LaTeX headers from input file
29 # - option to remove output of lily
30 # 0.9 - Trap Lilypond abort
31 # - Replaced "\usepackage[T1]{fontenc}" with
32 # \usepackage[latin1]{inputenc} (takk, Mats)
33 # - Removed "()" around "\LilyIdString" (Janne didn't want it)
34 # 0.8 - Trap Lilypond segmentation fault
35 # - Function for cleanup
37 # - More human-readable variables
38 # - Some logics concerning rc-files
40 # - Improved Lilypond error checking
41 # - Output orientation (landscape...). Overrides mudela file
42 # variable orientation="landscape";
43 # - Paper width and heigth put into variables (only A4!)
44 # - Adjusted top margin to default.....
47 # - Include more papersizes.
48 # - Manual page.......
50 # - LILYINCLUDE update
53 # - handle LILYINCLUDE
57 # - Source rc-files, if present. Files are:
58 # /usr/local/share/lilyrc /etc/lilyrc $HOME/.lilyrc ./.lilyrc
59 # - tee output from Lilypond
60 # - Handles margins for A4 paper (equal on both sides)
61 # - new option -s (--separate) for one LaTeX run per file,
62 # else all files are run together
65 # - More useful ("two-level") debug.
66 # - The Q&D hack to find file names and not handling \include
67 # is replaced by grabbing output file names from Lilypond.
68 # = Detects multiple output files - adds them when running
70 # - Works with multiple input files - no matter if they are
71 # (a mix of) input to or output from Lilypond.
74 # - Still no margins handling.
75 # - We have to discuss how to handle multiple output files
76 # from Lilypond - 'ly2dvi standchen' gives a rather odd
80 # - Always exit after printing help info
82 # - Changes to ensure for more strict grep'ing of parameters
83 # Thanks to from G.B.Stott@bolton.ac.uk
84 # - More efficient use of sed -e 's///' -e 's///'
85 # Thanks to Johan Vromans <jvromans@squirrel.nl> and GBS
86 # - Ask tex for location of titledefs.tex (Thanks to JV)
87 # - Accept only exact match of "\def\mudelacomposer{"
88 # (or whatever mudela* defined in titledefs.tex)
89 # - Even more efficient use of sed (Thanks to JV)
90 # - Default file name for single output file implemented.
91 # - Moved help into function - finally included from 0.1.jcn1
94 # - Still doesn't handle \include
95 # - The Q&D for finding output file name from the sequence of
96 # \paper \output \midi \output really needs to be looked at.
97 # I have improved it a lot, but it's only capable of finding
98 # one (the last) file name.
99 # Well, I have to rewrite this entirely to handle \include,
101 # - Still no margins handling.
104 # - Some lines of output from lilypond do NOT start
105 # at first character position, therefore I have removed "^"
106 # in sed'ing and grep'ing.
109 # - add "Creator: " line to output
112 # - multiple input files to make score from several files
113 # (extra files assumed to be Lilypond output)
114 # - cp dvi-file instead of mv, to allow for xdvi reload
115 # - check for illegal long options
116 # - put in pt in text width, if not given
117 # - put in \nonstopmode in LaTeX file
118 # - restored LaTeX exit value check
121 # - temporarily omit LaTeX exit value check
122 # - remove ALL temporary files
125 # - fix for solaris - included from 0.1.jcn1
126 # - long option hack - included from 0.1.jcn1 - modified
127 # - moved help into function - NOT included from 0.1.jcn1 yet
128 # - various improvements - included from 0.1.jcn1
129 # - find mudela definitions from titledefs.tex
130 # - find papersize from lilypond output file (mudelapapersize),
131 # overridden by option '-p size' or '--papersize=size'
132 # - option -l lang or --language=lang overrides
133 # lilypond output file definition (mudelalanguage)
134 # - textwidth from lilypond output file (mudelapaperlinewidth)
139 # - moved help into function
145 if [ "$KEEP_LY2DVI_OUTPUT" != "Y" ]
147 [ -n "$LatF" -a -f "$LatF" ] && rm -f $LatF
148 [ -n "$LOGFILE" -a -f "$LOGFILE" ] && rm -f $LOGFILE
149 [ -n "$FN" ] && rm -f $FN.*
150 for F in *$$* $TMP/*$$*
155 if [ "$KEEP_LILY_OUTPUT" != "Y" ]
157 for F in $LILY_OUTPUT_FILES
159 [ -f $F ] && rm -f $F
168 Generate dvi file from mudela or lilypond output
169 Usage: $0 [options] file[s]
172 -D,--debug set debug mode
173 -H,--headers= name of additional LaTeX headers file
174 -K,--keeplilypond keep lilypond output files
175 -L,--landscape set landscape orientation
176 -N,--nonumber switch off page numbering
177 -O,--orientation= set orientation (landscape or portrait (default))
178 -o,--output= set output directory
179 -h,--help this help text
180 -k,--keeply2dvi keep ly2dvi output files
181 -l,--language= give LaTeX language (babel)
182 -p,--papersize= give LaTeX papersize (eg. a4paper)
183 -s,--separate run all files separately through LaTeX
185 files may be (a mix of) input to or output from lilypond(1)
189 # Trap function (cleanup)
195 [ -z "$TMP" ] && TMP=/tmp
198 $debug_echo $IDENTIFICATION": temporary directory "$TMP" not found, set to /tmp"
201 LOGFILE=$TMP/lilylog.$$ # Logfile for lilypond
202 PWIDTH=600; # Width of A4 paper!
203 PHEIGTH=845; # Heigth of A4 paper!
204 PNUM="%" # Page numbering on
208 for D in /usr/local/share/ /etc/ $HOME/. ./.
211 [ -f $RCfile ] && . $RCfile
217 unset ORIENTATION LANGUAGE PAPERSIZE LATEXHF
219 # Keywords defined in titledefs.tex
221 TF=`kpsewhich -n tex tex titledefs.tex`
224 TF=/usr/lib/texmf/tex/lilypond/titledefs.tex
229 MU_DEF=`egrep "^.newcommand...mudela" $TF | \\
230 sed -e 's/^.newcommand...//' -e 's/\\}.*$//'`
235 MU_DEF="mudelatitle mudelasubtitle mudelacomposer \
236 mudelaarranger mudelainstrument"
244 # All files in one LaTeX run
248 # Find command line options and switches
250 # "x:" x takes argument
252 switches="DH:KLNO:hkl:o:p:s\?"
255 # ugh, "\-" is a hack to support long options
256 # while getopts \-:$options$switches O
257 # must be in double quotes for bash-2.0
258 while getopts "\-:$options$switches" O
260 $debug_echo "O: \`$O'"
261 $debug_echo "arg: \`$OPTARG'"
264 [ $debug_echo = echo ] && set -x
274 ORIENTATION=landscape
280 PNUM="\pagestyle{empty}"
307 $debug_echo "long option: \`$OPTARG'"
310 [ $debug_echo = echo ] && set -x
314 LATEXHF=`echo $OPTARG | sed -e s/"^.*="//`
327 ORIENTATION=landscape
330 LANGUAGE=`echo $OPTARG | sed -e s/"^.*="//`
333 PNUM="\pagestyle{empty}"
336 ORIENTATION=`echo $OPTARG | sed -e s/"^.*="//`
339 OUTPUTDIR=`echo $OPTARG | sed -e s/"^.*="//`
342 PAPERSIZE=`echo $OPTARG | sed -e s/"^.*="//`
348 echo $0": illegal option -- "$OPTARG;
355 shift `expr $OPTIND - 1`
362 $debug_echo $IDENTIFICATION": No input file name given"
370 BN=`basename $File .tex`
372 if [ "$KEEP_LY2DVI_OUTPUT" != "Y" ]
380 # paper size (PAPERSIZE, overridden by command line option -p)
381 # paper orientation (ORIENTATION, overridden by option -o)
382 # language (LANGUAGE, overridden by option -l)
386 -e 's/\\\\def\\\\mudelalanguage{\([^}]*\).*$/fLNG=\1;/p' \\
387 -e 's/\\\\def\\\\mudelalatexheaders{\([^}]*\).*$/fLHF=\1;/p' \\
388 -e 's/\\\\def\\\\mudelaorientation{\([^}]*\).*$/fORI=\1;/p' \\
389 -e 's/\\\\def\\\\mudelapaperlinewidth{\([^}]*\).*$/TWN=\1;/p' \\
390 -e 's/\\\\def\\\\mudelapapersize{\([^}]*\).*$/fPSZ=\1;/p' \\
400 [ -f $LATEXHF ] && LLHF="\input{$LATEXHF}"
403 if [ -z "$PAPERSIZE" ]
407 if [ -n "$PAPERSIZE" ]
412 if [ -z "$ORIENTATION" ]
416 if [ -n "$ORIENTATION" ]
418 if [ -z "$PAPEROPT" ]
420 PAPEROPT=$ORIENTATION
422 PAPEROPT=$PAPEROPT,$ORIENTATION
426 if [ -n "$PAPEROPT" ]
428 PAPER="["$PAPEROPT"]"
431 if [ -z "$LANGUAGE" ]
435 if [ -n "$LANGUAGE" ]
437 LLNG="\usepackage["$LANGUAGE"]{babel}"
459 $debug_echo "Text width = "$TW
461 TWp=`echo $TW | sed -e 's/\..*$//'`
463 if [ "$ORIENTATION" = "landscape" ]
467 MARG=`expr $PWp - $TWp`
468 MARG=`expr $MARG / 2`"pt"
470 # Geometry: /var/lib/texmf/latex/geometry/geometry.dvi
476 % Creator: $IDENTIFICATION
477 % Automatically generated from $IF, $NOW
479 \documentclass$PAPER{article}
482 \usepackage{geometry}
483 \usepackage[latin1]{inputenc}
484 %\usepackage[T1]{fontenc}
486 %\addtolength{\oddsidemargin}{-1cm}
487 %\addtolength{\topmargin}{-1cm}
488 \setlength{\textwidth}{$TW}
489 %\geometry{width=$TW, left=$MARG, top=1cm}
490 \geometry{width=$TW, left=$MARG}
497 # Include \def\mudela-definitions
501 LL=`egrep '^\\\\def.'$L'{' $OF`
504 LLL=`echo $LL | sed -e 's/}.*$//' -e 's/.*{//'`
507 echo "\\"$L'{'$LLL'}%' >> $LatF
521 \vfill\hfill{\LilyIdString}
527 latex $LatF || exit 5
534 [ -n "$OUTPUTDIR" ] && RESULT="$OUTPUTDIR/$RESULT"
542 $IDENTIFICATION: dvi file name is $RESULT
547 # ugh. GF is side-effect.
549 # should check for LILYINCLUDE
550 for lypath in "." `echo $LILYINCLUDE| sed 's/:/ /g'`
552 if [ -f "$lypath/$1" ]
558 if [ -f "$lypath/$1.ly" ]
564 $debug_echo $IDENTIFICATION": Input file "$GF" not found"
565 echo $NAME": Input file "$GF" not found" 1>&2
569 # Loop through all files
579 # Check whether the file is input to or output from lilypond
582 OP=`echo $L1 | grep "^% Creator: GNU LilyPond"`
586 # OK - it's the output from lilypond.
588 # Get lilypond source file name
591 IFL=`grep mudelafilename $OF`
594 IF=`echo $IFL | sed -e 's/.*{//' -e 's/}*.$//'`
596 # Check if source file exists
600 $debug_echo $IDENTIFICATION": Mudela file not found."
604 $debug_echo $IDENTIFICATION": Mudela file name not found."
609 # I have to assume this is the lilypond input file
610 # Find output file name, if defined
615 # Grab output file names
617 $debug_echo "lilypond "$IF
619 lilypond $IF 2>&1 | tee $LOGFILE
620 OF=`egrep '^TeX output to ' $LOGFILE | \\
621 sed -e 's/TeX output to//' -e 's/\.\.\.//'`
622 $debug_echo "==> "$OF
623 STATUS=`egrep -i "error|segmentation|abort" $LOGFILE`
625 if [ ! -z "$STATUS" ]
631 # "Spin through" all the files
635 $debug_echo "--- "$File
637 # Check if output file is generated
641 $debug_echo $IDENTIFICATION": hmm, I could not find the output file "$File
645 # Is this the first file?
660 LILY_OUTPUT_FILES="$LILY_OUTPUT_FILES $OF"