root/trunk/lisp/calendar/cal-tex.el

Revision 4220, 67.8 kB (checked in by miyoshi, 9 months ago)

Sync up with Emacs22.2.

  • Property svn:eol-style set to LF
Line 
1 ;;; cal-tex.el --- calendar functions for printing calendars with LaTeX
2
3 ;; Copyright (C) 1995, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 ;;   Free Software Foundation, Inc.
5
6 ;; Author: Steve Fisk <fisk@bowdoin.edu>
7 ;;      Edward M. Reingold <reingold@cs.uiuc.edu>
8 ;; Maintainer: Glenn Morris <rgm@gnu.org>
9 ;; Keywords: calendar
10 ;; Human-Keywords: Calendar, LaTeX
11
12 ;; This file is part of GNU Emacs.
13
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 3, or (at your option)
17 ;; any later version.
18
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 ;; GNU General Public License for more details.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
26 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27 ;; Boston, MA 02110-1301, USA.
28
29 ;;; Commentary:
30
31 ;; This collection of functions implements the creation of LaTeX calendars
32 ;; based on the user's holiday choices and diary file.
33
34 ;; TO DO
35 ;;
36 ;;     (*)  Add holidays and diary entries to daily calendar.
37 ;;
38 ;;     (*)  Add diary entries to weekly calendar functions.
39 ;;
40 ;;     (*)  Make calendar styles for A4 paper.
41 ;;
42 ;;     (*)  Make monthly styles Filofax paper.
43
44 ;;; Code:
45
46 (require 'calendar)
47
48 (autoload 'diary-list-entries "diary-lib" nil t)
49 (autoload 'calendar-holiday-list "holidays" nil t)
50 (autoload 'calendar-iso-from-absolute "cal-iso" nil t)
51
52 ;;;
53 ;;; Customizable variables
54 ;;;
55
56 (defcustom cal-tex-which-days '(0 1 2 3 4 5 6)
57   "*The days of the week that are displayed on the portrait monthly calendar.
58 Sunday is 0, Monday is 1, and so on.  The default is to print from Sunday to
59 Saturday.  For example, use
60
61                     (setq cal-tex-which-days '(1 3 5))
62
63 to only print Monday, Wednesday, Friday."
64   :type '(repeat integer)
65   :group 'calendar-tex)
66
67 (defcustom cal-tex-holidays t
68   "Non-nil means holidays are printed in the LaTeX calendars that support it.
69 If finding the holidays is too slow, set this to nil."
70   :type 'boolean
71   :group 'calendar-tex)
72
73 (defcustom cal-tex-diary nil
74   "Non-nil means diary entries are printed in LaTeX calendars that support it.
75 At present, this only affects the monthly, filofax, and iso-week
76 calendars (i.e. not the yearly, plain weekly, or daily calendars)."
77   :type 'boolean
78   :group 'calendar-tex)
79
80 (defcustom cal-tex-rules nil
81   "Non-nil means pages will be ruled in some LaTeX calendar styles.
82 At present, this only affects the daily filofax calendar."
83   :type 'boolean
84   :group 'calendar-tex)
85
86 (defcustom cal-tex-daily-string
87   '(let* ((year (extract-calendar-year date))
88           (day  (calendar-day-number date))
89           (days-remaining (- (calendar-day-number (list 12 31 year)) day)))
90      (format "%d/%d" day  days-remaining))
91   "*An expression in the variable `date' whose value is placed on date.
92 The string resulting from evaluating this expression is placed at the bottom
93 center of `date' on the monthly calendar, next to the date in the weekly
94 calendars, and in the top center of daily calendars.
95
96 Default is ordinal day number of the year and the number of days remaining.
97 As an example of what you do, setting this to
98
99     '(progn
100        (require 'cal-hebrew)
101        (calendar-hebrew-date-string date))
102
103 will put the Hebrew date at the bottom of each day."
104   :type 'sexp
105   :group 'calendar-tex)
106
107 (defcustom cal-tex-buffer "calendar.tex"
108   "The name for the output LaTeX calendar buffer."
109   :type 'string
110   :group 'calendar-tex)
111
112 (defcustom cal-tex-24 nil
113   "Non-nil means use a 24 hour clock in the daily calendar."
114   :type 'boolean
115   :group 'calendar-tex)
116
117 (defcustom cal-tex-daily-start 8
118   "*The first hour of the daily calendar page."
119   :type 'integer
120   :group 'calendar-tex)
121
122 (defcustom cal-tex-daily-end 20
123   "*The last hour of the daily calendar page."
124   :type 'integer
125   :group 'calendar-tex)
126
127 (defcustom cal-tex-preamble-extra nil
128   "A string giving extra LaTeX commands to insert in the calendar preamble.
129 For example, to include extra packages:
130 \"\\\\usepackage{foo}\\n\\\\usepackage{bar}\\n\"."
131   :type 'string
132   :group 'calendar-tex
133   :version "22.1")
134
135 (defcustom cal-tex-hook nil
136   "*List of functions called after any LaTeX calendar buffer is generated.
137 You can use this to do postprocessing on the buffer.  For example, to change
138 characters with diacritical marks to their LaTeX equivalents, use
139      (add-hook 'cal-tex-hook
140                '(lambda () (iso-iso2tex (point-min) (point-max))))"
141   :type 'hook
142   :group 'calendar-tex)
143
144 (defcustom cal-tex-year-hook nil
145   "*List of functions called after a LaTeX year calendar buffer is generated."
146   :type 'hook
147   :group 'calendar-tex)
148
149 (defcustom cal-tex-month-hook nil
150   "*List of functions called after a LaTeX month calendar buffer is generated."
151   :type 'hook
152   :group 'calendar-tex)
153
154 (defcustom cal-tex-week-hook nil
155   "*List of functions called after a LaTeX week calendar buffer is generated."
156   :type 'hook
157   :group 'calendar-tex)
158
159 (defcustom cal-tex-daily-hook nil
160   "*List of functions called after a LaTeX daily calendar buffer is generated."
161   :type 'hook
162   :group 'calendar-tex)
163
164 ;;;
165 ;;; Definitions for LaTeX code
166 ;;;
167
168 (defvar cal-tex-day-prefix "\\caldate{%s}{%s}"
169   "The initial LaTeX code for a day.
170 The holidays, diary entries, bottom string, and the text follow.")
171
172 (defvar cal-tex-day-name-format "\\myday{%s}%%"
173   "The format for LaTeX code for a day name.  The names are taken from
174 `calendar-day-name-array'.")
175
176 (defvar cal-tex-cal-one-month
177 "\\def\\calmonth#1#2%
178 {\\begin{center}%
179 \\Huge\\bf\\uppercase{#1} #2 \\\\[1cm]%
180 \\end{center}}%
181 \\vspace*{-1.5cm}%
182 %
183 "
184   "LaTeX code for the month header")
185
186 (defvar cal-tex-cal-multi-month
187 "\\def\\calmonth#1#2#3#4%
188 {\\begin{center}%
189 \\Huge\\bf #1 #2---#3 #4\\\\[1cm]%
190 \\end{center}}%
191 \\vspace*{-1.5cm}%
192 %
193 "
194   "LaTeX code for the month header")
195
196 (defvar cal-tex-myday
197 "\\renewcommand{\\myday}[1]%
198 {\\makebox[\\cellwidth]{\\hfill\\large\\bf#1\\hfill}}
199 %
200 "
201   "LaTeX code for a day heading")
202
203 (defvar cal-tex-caldate
204 "\\fboxsep=0pt
205 \\long\\def\\caldate#1#2#3#4#5#6{%
206     \\fbox{\\hbox to\\cellwidth{%
207      \\vbox to\\cellheight{%
208        \\hbox to\\cellwidth{%
209           {\\hspace*{1mm}\\Large \\bf \\strut #2}\\hspace{.05\\cellwidth}%
210           \\raisebox{\\holidaymult\\cellheight}%
211                    {\\parbox[t]{.75\\cellwidth}{\\tiny \\raggedright #4}}}
212        \\hbox to\\cellwidth{%
213            \\hspace*{1mm}\\parbox{.95\\cellwidth}{\\tiny \\raggedright #3}}
214        \\hspace*{1mm}%
215        \\hbox to\\cellwidth{#6}%
216        \\vfill%
217        \\hbox to\\cellwidth{\\hfill \\tiny #5 \\hfill}%
218        \\vskip 1.4pt}%
219      \\hskip -0.4pt}}}
220 "
221   "LaTeX code to insert one box with date info in calendar.
222 This definition is the heart of the calendar!")
223
224 (defun cal-tex-list-holidays (d1 d2)
225   "Generate a list of all holidays from absolute date D1 to D2."
226   (let* ((start (calendar-gregorian-from-absolute d1))
227          (displayed-month (extract-calendar-month start))
228          (displayed-year (extract-calendar-year start))
229          (end (calendar-gregorian-from-absolute d2))
230          (end-month (extract-calendar-month end))
231          (end-year (extract-calendar-year end))
232          (number-of-intervals
233           (1+ (/ (calendar-interval displayed-month displayed-year
234                                     end-month end-year)
235                  3)))
236          (holidays nil)
237          (in-range))
238     (increment-calendar-month displayed-month displayed-year 1)
239     (calendar-for-loop i from 1 to number-of-intervals do
240       (setq holidays (append holidays (calendar-holiday-list)))
241       (increment-calendar-month displayed-month displayed-year 3))
242     (while holidays
243       (and (car (car holidays))
244            (let ((a (calendar-absolute-from-gregorian (car (car holidays)))))
245              (and (<= d1 a) (<= a d2)))
246            (setq in-range (append (list (car holidays)) in-range)))
247       (setq holidays (cdr holidays)))
248     in-range))
249
250 (defun cal-tex-list-diary-entries (d1 d2)
251   "Generate a list of all diary-entries from absolute date D1 to D2."
252   (let ((diary-list-include-blanks nil)
253         (diary-display-hook 'ignore))
254     (diary-list-entries
255      (calendar-gregorian-from-absolute d1)
256      (1+ (- d2 d1)))))
257
258 (defun cal-tex-preamble (&optional args)
259   "Insert the LaTeX preamble.
260 Preamble Includes initial definitions for various LaTeX commands.
261 Optional ARGS are included."
262   (set-buffer (get-buffer-create cal-tex-buffer))
263   (erase-buffer)
264   (insert "\\documentclass")
265   (if args
266       (insert "[" args "]"))
267   (insert "{article}\n")
268   (if (stringp cal-tex-preamble-extra)
269       (insert cal-tex-preamble-extra "\n"))
270   (insert "\\hbadness 20000
271 \\hfuzz=1000pt
272 \\vbadness 20000
273 \\lineskip 0pt
274 \\marginparwidth 0pt
275 \\oddsidemargin  -2cm
276 \\evensidemargin -2cm
277 \\marginparsep   0pt
278 \\topmargin      0pt
279 \\textwidth      7.5in
280 \\textheight     9.5in
281 \\newlength{\\cellwidth}
282 \\newlength{\\cellheight}
283 \\newlength{\\boxwidth}
284 \\newlength{\\boxheight}
285 \\newlength{\\cellsize}
286 \\newcommand{\\myday}[1]{}
287 \\newcommand{\\caldate}[6]{}
288 \\newcommand{\\nocaldate}[6]{}
289 \\newcommand{\\calsmall}[6]{}
290 %
291 "))
292
293 ;;;
294 ;;;  Yearly calendars
295 ;;;
296
297 (defun cal-tex-cursor-year (&optional arg)
298   "Make a buffer with LaTeX commands for the year cursor is on.
299 Optional prefix argument specifies number of years."
300   (interactive "p")
301   (cal-tex-year (extract-calendar-year (calendar-cursor-to-date t))
302                 (if arg arg 1)))
303
304 (defun cal-tex-cursor-year-landscape (&optional arg)
305   "Make a buffer with LaTeX commands for the year cursor is on.
306 Optional prefix argument specifies number of years."
307   (interactive "p")
308   (cal-tex-year (extract-calendar-year (calendar-cursor-to-date t))
309                 (if arg arg 1)
310                 t))
311
312 (defun cal-tex-year (year n &optional landscape)
313   "Make a one page yearly calendar of YEAR; do this for N years.
314 There are four rows of three months each, unless optional LANDSCAPE is t,
315 in which case the calendar isprinted in landscape mode with three rows of
316 four months each."
317   (cal-tex-insert-preamble 1 landscape "12pt")
318   (if landscape
319       (cal-tex-vspace "-.6cm")
320     (cal-tex-vspace "-3.1cm"))
321   (calendar-for-loop j from 1 to n do
322     (insert "\\vfill%\n")
323     (cal-tex-b-center)
324     (cal-tex-Huge (number-to-string year))
325     (cal-tex-e-center)
326     (cal-tex-vspace "1cm")
327     (cal-tex-b-center)
328     (cal-tex-b-parbox "l" (if landscape "5.9in" "4.3in"))
329     (insert "\n")
330     (cal-tex-noindent)
331     (cal-tex-nl)
332     (calendar-for-loop i from 1 to 12 do
333         (insert (cal-tex-mini-calendar i year "month" "1.1in" "1in"))
334         (insert "\\month")
335         (cal-tex-hspace "0.5in")
336         (if (zerop (mod i (if landscape 4 3)))
337             (cal-tex-nl "0.5in")))
338     (cal-tex-e-parbox)
339     (cal-tex-e-center)
340     (insert "\\vfill%\n")
341     (setq year (1+ year))
342     (if (/= j n)
343         (cal-tex-newpage)
344       (cal-tex-end-document))
345     (run-hooks 'cal-tex-year-hook))
346   (run-hooks 'cal-tex-hook))
347
348 (defun cal-tex-cursor-filofax-year (&optional arg)
349   "Make a Filofax one page yearly calendar of year indicated by cursor.
350 Optional parameter specifies number of years."
351   (interactive "p")
352   (let* ((n (if arg arg 1))
353          (year (extract-calendar-year (calendar-cursor-to-date t))))
354     (cal-tex-preamble "twoside")
355     (cal-tex-cmd "\\textwidth 3.25in")
356     (cal-tex-cmd "\\textheight 6.5in")
357     (cal-tex-cmd "\\oddsidemargin 1.675in")
358     (cal-tex-cmd "\\evensidemargin 1.675in")
359     (cal-tex-cmd "\\topmargin 0pt")
360     (cal-tex-cmd "\\headheight -0.875in")
361     (cal-tex-cmd "\\fboxsep 0.5mm")
362     (cal-tex-cmd "\\pagestyle{empty}")
363     (cal-tex-b-document)
364     (cal-tex-cmd "\\vspace*{0.25in}")
365     (calendar-for-loop j from 1 to n do
366       (insert (format "\\hfil {\\Large \\bf %s} \\hfil\\\\\n" year))
367       (cal-tex-b-center)
368       (cal-tex-b-parbox "l" "\\textwidth")
369       (insert "\n")
370       (cal-tex-noindent)
371       (cal-tex-nl)
372       (let ((month-names; don't use default in case user changed it
373              ;; These are only used to define the command names, not
374              ;; the names of the months they insert.
375              ["January" "February" "March" "April" "May" "June"
376               "July" "August" "September" "October" "November" "December"]))
377         (calendar-for-loop i from 1 to 12 do
378           (insert (cal-tex-mini-calendar i year
379                                          (aref month-names (1- i))
380                                          "1in" ".9in" "tiny" "0.6mm"))))
381       (insert
382        "\\noindent\\fbox{\\January}\\fbox{\\February}\\fbox{\\March}\\\\
383 \\noindent\\fbox{\\April}\\fbox{\\May}\\fbox{\\June}\\\\
384 \\noindent\\fbox{\\July}\\fbox{\\August}\\fbox{\\September}\\\\
385 \\noindent\\fbox{\\October}\\fbox{\\November}\\fbox{\\December}
386 ")
387       (cal-tex-e-parbox)
388       (cal-tex-e-center)
389       (setq year (1+ year))
390       (if (= j n)
391           (cal-tex-end-document)
392         (cal-tex-newpage)
393         (cal-tex-cmd "\\vspace*{0.25in}"))
394       (run-hooks 'cal-tex-year-hook))
395     (run-hooks 'cal-tex-hook)))
396
397 ;;;
398 ;;;  Monthly calendars
399 ;;;
400
401 (defun cal-tex-cursor-month-landscape (&optional arg)
402   "Make a LaTeX calendar buffer for the month the cursor is on.
403 Optional prefix argument specifies number of months to be
404 produced (default 1).  The output is in landscape format, one
405 month to a page.  It shows holiday and diary entries if
406 `cal-tex-holidays' and `cal-tex-diary', respectively, are
407 non-nil."
408   (interactive "p")
409   (let* ((n (if arg arg 1))
410          (date (calendar-cursor-to-date t))
411          (month (extract-calendar-month date))
412          (year (extract-calendar-year date))
413          (end-month month)
414          (end-year year)
415          (cal-tex-which-days '(0 1 2 3 4 5 6)))
416     (increment-calendar-month end-month end-year (1- n))
417     (let ((diary-list (if cal-tex-diary
418                           (cal-tex-list-diary-entries
419                            (calendar-absolute-from-gregorian
420                             (list month 1 year))
421                            (calendar-absolute-from-gregorian
422                             (list end-month
423                                   (calendar-last-day-of-month
424                                    end-month end-year)
425                                   end-year)))))
426           (holidays (if cal-tex-holidays
427                         (cal-tex-list-holidays
428                          (calendar-absolute-from-gregorian
429                           (list month 1 year))
430                          (calendar-absolute-from-gregorian
431                           (list end-month
432                                 (calendar-last-day-of-month end-month end-year)
433                                 end-year)))))
434           (other-month)
435           (other-year)
436           (small-months-at-start))
437       (cal-tex-insert-preamble (cal-tex-number-weeks month year 1) t "12pt")
438       (cal-tex-cmd cal-tex-cal-one-month)
439       (calendar-for-loop i from 1 to n do
440         (setq other-month month)
441         (setq other-year year)
442         (increment-calendar-month other-month other-year -1)
443         (insert (cal-tex-mini-calendar other-month other-year "lastmonth"
444                                        "\\cellwidth" "\\cellheight"))
445         (increment-calendar-month other-month other-year 2)
446         (insert (cal-tex-mini-calendar other-month other-year "nextmonth"
447                                        "\\cellwidth" "\\cellheight"))
448         (cal-tex-insert-month-header 1 month year month year)
449         (cal-tex-insert-day-names)
450         (cal-tex-nl ".2cm")
451         (setq small-months-at-start
452                   (< 1 (mod (- (calendar-day-of-week (list month 1 year))
453                                calendar-week-start-day)
454                             7)))
455         (if small-months-at-start
456             (insert "\\lastmonth\\nextmonth\\hspace*{-2\\cellwidth}"))
457         (cal-tex-insert-blank-days month year cal-tex-day-prefix)
458         (cal-tex-insert-days month year diary-list holidays
459                              cal-tex-day-prefix)
460         (cal-tex-insert-blank-days-at-end month year cal-tex-day-prefix)
461         (if (and (not small-months-at-start)
462                  (< 1 (mod (- (1- calendar-week-start-day)
463                               (calendar-day-of-week
464                                (list month
465                                      (calendar-last-day-of-month month year)
466                                      year)))
467                            7)))
468             (insert "\\vspace*{-\\cellwidth}\\hspace*{-2\\cellwidth}"
469                     "\\lastmonth\\nextmonth%
470 "))
471         (if (/= i n)
472             (progn
473               (run-hooks 'cal-tex-month-hook)
474               (cal-tex-newpage)
475               (increment-calendar-month month year 1)
476               (cal-tex-vspace "-2cm")
477               (cal-tex-insert-preamble
478                (cal-tex-number-weeks month year 1) t "12pt" t))))
479       (cal-tex-end-document)
480       (run-hooks 'cal-tex-hook))))
481
482 (defun cal-tex-cursor-month (arg)
483   "Make a LaTeX calendar buffer for the month the cursor is on.
484 Optional prefix argument specifies number of months to be
485 produced (default 1).  The calendar is condensed onto one page.
486 It shows holiday and diary entries if `cal-tex-holidays' and
487 `cal-tex-diary', respectively, are non-nil."
488   (interactive "p")
489   (let* ((date (calendar-cursor-to-date t))
490          (month (extract-calendar-month date))
491          (year (extract-calendar-year date))
492          (end-month month)
493          (end-year year)
494          (n (if arg arg 1)))
495     (increment-calendar-month end-month end-year (1- n))
496     (let ((diary-list (if cal-tex-diary
497                           (cal-tex-list-diary-entries
498                            (calendar-absolute-from-gregorian
499                             (list month 1 year))
500                            (calendar-absolute-from-gregorian
501                             (list end-month
502                                   (calendar-last-day-of-month
503                                    end-month end-year)
504                                   end-year)))))
505           (holidays (if cal-tex-holidays
506                         (cal-tex-list-holidays
507                          (calendar-absolute-from-gregorian
508                           (list month 1 year))
509                          (calendar-absolute-from-gregorian
510                           (list end-month
511                                 (calendar-last-day-of-month end-month end-year)
512                                 end-year)))))
513           (other-month)
514           (other-year))
515       (cal-tex-insert-preamble (cal-tex-number-weeks month year n) nil"12pt")
516       (if (> n 1)
517           (cal-tex-cmd cal-tex-cal-multi-month)
518         (cal-tex-cmd cal-tex-cal-one-month))
519       (cal-tex-insert-month-header n month year end-month end-year)
520       (cal-tex-insert-day-names)
521       (cal-tex-nl ".2cm")
522       (cal-tex-insert-blank-days month year cal-tex-day-prefix)
523       (calendar-for-loop i from 1 to n do
524         (setq other-month month)
525         (setq other-year year)
526         (cal-tex-insert-days month year diary-list holidays
527                              cal-tex-day-prefix)
528         (if (= (mod (calendar-absolute-from-gregorian
529                        (list month
530                              (calendar-last-day-of-month month year)
531                              year))
532                       7)
533                  6); last day of month was Saturday
534             (progn
535               (cal-tex-hfill)
536               (cal-tex-nl)))
537         (increment-calendar-month month year 1))
538       (cal-tex-insert-blank-days-at-end end-month end-year cal-tex-day-prefix)
539       (cal-tex-end-document)))
540   (run-hooks 'cal-tex-hook))
541
542 (defun cal-tex-insert-days (month year diary-list holidays day-format)
543   "Insert LaTeX commands for a range of days in monthly calendars.
544 LaTeX commands are inserted for the days of the MONTH in YEAR.
545 Diary entries on DIARY-LIST are included.  Holidays on HOLIDAYS
546 are included.  Each day is formatted using format DAY-FORMAT."
547   (let* ((blank-days;; at start of month
548           (mod
549            (- (calendar-day-of-week (list month 1 year))
550               calendar-week-start-day)
551            7))
552          (date)
553          (last (calendar-last-day-of-month month year)))
554     (calendar-for-loop i from 1 to last do
555        (setq date (list month i year))
556        (if (memq (calendar-day-of-week date) cal-tex-which-days)
557            (progn
558              (insert (format day-format (cal-tex-month-name month) i))
559              (cal-tex-arg (cal-tex-latexify-list diary-list date))
560              (cal-tex-arg (cal-tex-latexify-list holidays date))
561              (cal-tex-arg (eval cal-tex-daily-string))
562              (cal-tex-arg)
563              (cal-tex-comment)))
564        (if (and (zerop (mod (+ i blank-days) 7))
565                 (/= i last))
566            (progn
567              (cal-tex-hfill)
568              (cal-tex-nl))))))
569
570 (defun cal-tex-insert-day-names ()
571   "Insert the names of the days at top of a monthly calendar."
572   (calendar-for-loop i from 0 to 6 do
573     (if (memq i cal-tex-which-days)
574         (insert (format cal-tex-day-name-format
575                         (cal-tex-LaTeXify-string
576                          (aref calendar-day-name-array
577                                (mod (+ calendar-week-start-day i) 7))))))
578     (cal-tex-comment)))
579
580 (defun cal-tex-insert-month-header (n month year end-month end-year)
581   "Create a title for a calendar.
582 A title is inserted for a calendar with N months starting with
583 MONTH YEAR and ending with END-MONTH END-YEAR."
584   (let ((month-name (cal-tex-month-name  month))
585          (end-month-name (cal-tex-month-name  end-month)))
586     (if (= 1 n)
587         (insert (format "\\calmonth{%s}{%s}\n\\vspace*{-0.5cm}"
588                 month-name year) )
589         (insert (format "\\calmonth{%s}{%s}{%s}{%s}\n\\vspace*{-0.5cm}"
590                 month-name year end-month-name end-year))))
591   (cal-tex-comment))
592
593 (defun cal-tex-insert-blank-days (month year day-format)
594   "Insert code for initial days not in calendar.
595 Insert LaTeX code for the blank days at the beginning of the MONTH in
596 YEAR.  The entry is formatted using DAY-FORMAT.  If the entire week is
597 blank, no days are inserted."
598   (if (cal-tex-first-blank-p month year)
599       (let* ((blank-days;; at start of month
600               (mod
601                (- (calendar-day-of-week (list month 1 year))
602                   calendar-week-start-day)
603                7)))
604         (calendar-for-loop i from 0 to (1- blank-days) do
605           (if (memq i cal-tex-which-days)
606               (insert (format day-format " " " ") "{}{}{}{}%\n"))))))
607
608 (defun cal-tex-insert-blank-days-at-end (month year day-format)
609   "Insert code for final days not in calendar.
610 Insert LaTeX code for the blank days at the end of the MONTH in YEAR.
611 The entry is formatted using DAY-FORMAT."
612   (if (cal-tex-last-blank-p month year)
613       (let* ((last-day (calendar-last-day-of-month month year))
614              (blank-days;; at end of month
615               (mod
616                (- (calendar-day-of-week (list month last-day year))
617                   calendar-week-start-day)
618                7)))
619         (calendar-for-loop i from (1+ blank-days) to 6 do
620            (if (memq i cal-tex-which-days)
621                (insert (format day-format "" "") "{}{}{}{}%\n"))))))
622
623 (defun cal-tex-first-blank-p (month year)
624   "Determine if any days of the first week will be printed.
625 Return t if there will there be any days of the first week printed
626 in the calendar starting in MONTH YEAR."
627   (let ((any-days nil)
628         (the-saturday))                 ;the day of week of 1st Saturday
629     (calendar-for-loop i from 1 to 7 do
630        (if (= 6 (calendar-day-of-week (list month i year)))
631            (setq the-saturday i)))
632     (calendar-for-loop i from 1 to the-saturday do
633        (if (memq (calendar-day-of-week (list month i year))
634                  cal-tex-which-days)
635            (setq any-days t)))
636     any-days))
637
638 (defun cal-tex-last-blank-p (month year)
639   "Determine if any days of the last week will be printed.
640 Return t if there will there be any days of the last week printed
641 in the calendar starting in MONTH YEAR."
642   (let ((any-days nil)
643         (last-day (calendar-last-day-of-month month year))
644         (the-sunday))                   ;the day of week of last Sunday
645     (calendar-for-loop i from (- last-day 6) to last-day do
646        (if (= 0 (calendar-day-of-week (list month i year)))
647            (setq the-sunday i)))
648     (calendar-for-loop i from the-sunday to last-day do
649        (if (memq (calendar-day-of-week (list month i year))
650                  cal-tex-which-days)
651            (setq any-days t)))
652     any-days))
653
654 (defun cal-tex-number-weeks (month year n)
655   "Determine the number of weeks in a range of dates.
656 Compute the number of  weeks in the calendar starting with MONTH and YEAR,
657 and lasting N months, including only the days in WHICH-DAYS. As it stands,
658 this is only an upper bound."
659   (let ((d (list month 1 year)))
660     (increment-calendar-month month year (1- n))
661     (/ (- (calendar-dayname-on-or-before
662            calendar-week-start-day
663            (+ 7 (calendar-absolute-from-gregorian
664                    (list month (calendar-last-day-of-month month year) year))))
665           (calendar-dayname-on-or-before
666            calendar-week-start-day
667            (calendar-absolute-from-gregorian d)))
668        7)))
669
670 ;;;
671 ;;; Weekly calendars
672 ;;;
673
674 (defvar cal-tex-LaTeX-hourbox
675   "\\newcommand{\\hourbox}[2]%
676 {\\makebox[2em]{\\rule{0cm}{#2ex}#1}\\rule{3in}{.15mm}}\n"
677   "One hour and a line on the right.")
678
679 ;; TODO cal-tex-diary-support.
680 (defun cal-tex-cursor-week (&optional arg)
681   "Make a LaTeX calendar buffer for a two-page one-week calendar.
682 It applies to the week that point is in.  The optional prefix
683 argument specifies the number of weeks (default 1).  The calendar
684 shows holidays if `cal-tex-holidays' is t (note that diary
685 entries are not shown)."
686   (interactive "p")
687   (let* ((n (if arg arg 1))
688          (date (calendar-gregorian-from-absolute
689                 (calendar-dayname-on-or-before
690                  calendar-week-start-day
691                  (calendar-absolute-from-gregorian
692                   (calendar-cursor-to-date t)))))
693          (month (extract-calendar-month date))
694          (year (extract-calendar-year date))
695          (holidays (if cal-tex-holidays
696                        (cal-tex-list-holidays
697                         (calendar-absolute-from-gregorian date)
698                         (+ (* 7 n)
699                            (calendar-absolute-from-gregorian date))))))
700     (cal-tex-preamble "11pt")
701     (cal-tex-cmd "\\textwidth   6.5in")
702     (cal-tex-cmd "\\textheight 10.5in")
703     (cal-tex-cmd "\\oddsidemargin 0in")
704     (cal-tex-cmd "\\evensidemargin 0in")
705     (insert cal-tex-LaTeX-hourbox)
706     (cal-tex-b-document)
707     (cal-tex-cmd "\\pagestyle{empty}")
708     (calendar-for-loop i from 1 to n do
709       (cal-tex-vspace "-1.5in")
710       (cal-tex-b-center)
711       (cal-tex-Huge-bf (format "\\uppercase{%s}"
712                                (cal-tex-month-name month)))
713       (cal-tex-hspace "2em")
714       (cal-tex-Huge-bf (number-to-string year))
715       (cal-tex-nl ".5cm")
716       (cal-tex-e-center)
717       (cal-tex-hspace "-.2in")
718       (cal-tex-b-parbox "l" "7in")
719       (calendar-for-loop j from 1 to 7 do
720         (cal-tex-week-hours date holidays "3.1")
721         (setq date (cal-tex-incr-date date)))
722       (cal-tex-e-parbox)
723       (setq month (extract-calendar-month date))
724       (setq year (extract-calendar-year date))
725       (if (/= i n)
726           (progn
727             (run-hooks 'cal-tex-week-hook)
728             (cal-tex-newpage))))
729     (cal-tex-end-document)
730     (run-hooks 'cal-tex-hook)))
731
732 ;; TODO cal-tex-diary support.
733 (defun cal-tex-cursor-week2 (&optional arg)
734   "Make a LaTeX calendar buffer for a two-page one-week calendar.
735 It applies to the week that point is in.  Optional prefix
736 argument specifies number of weeks (default 1).  The calendar
737 shows holidays if `cal-tex-holidays' is non-nil (note that diary
738 entries are not shown)."
739   (interactive "p")
740   (let* ((n (if arg arg 1))
741          (date (calendar-gregorian-from-absolute
742                 (calendar-dayname-on-or-before
743                  calendar-week-start-day
744                  (calendar-absolute-from-gregorian
745                   (calendar-cursor-to-date t)))))
746          (month (extract-calendar-month date))
747          (year (extract-calendar-year date))
748          (d date)
749          (holidays (if cal-tex-holidays
750                        (cal-tex-list-holidays
751                         (calendar-absolute-from-gregorian date)
752                         (+ (* 7 n)
753                            (calendar-absolute-from-gregorian date))))))
754     (cal-tex-preamble "12pt")
755     (cal-tex-cmd "\\textwidth   6.5in")
756     (cal-tex-cmd "\\textheight 10.5in")
757     (cal-tex-cmd "\\oddsidemargin 0in")
758     (cal-tex-cmd "\\evensidemargin 0in")
759     (insert cal-tex-LaTeX-hourbox)
760     (cal-tex-b-document)
761     (cal-tex-cmd "\\pagestyle{empty}")
762     (calendar-for-loop i from 1 to n do
763       (cal-tex-vspace "-1.5in")
764       (cal-tex-b-center)
765       (cal-tex-Huge-bf (format "\\uppercase{%s}"
766                                (cal-tex-month-name month)))
767       (cal-tex-hspace "2em")
768       (cal-tex-Huge-bf (number-to-string year))
769       (cal-tex-nl ".5cm")
770       (cal-tex-e-center)
771       (cal-tex-hspace "-.2in")
772       (cal-tex-b-parbox "l" "\\textwidth")
773       (calendar-for-loop j from 1 to 3 do
774         (cal-tex-week-hours date holidays "5")
775         (setq date (cal-tex-incr-date date)))
776       (cal-tex-e-parbox)
777       (cal-tex-nl)
778       (insert (cal-tex-mini-calendar
779                (extract-calendar-month (cal-tex-previous-month date))
780                (extract-calendar-year (cal-tex-previous-month date))
781                "lastmonth" "1.1in" "1in"))
782       (insert (cal-tex-mini-calendar
783                (extract-calendar-month date)
784                (extract-calendar-year date)
785                "thismonth" "1.1in" "1in"))
786       (insert (cal-tex-mini-calendar
787                (extract-calendar-month (cal-tex-next-month date))
788                (extract-calendar-year (cal-tex-next-month date))
789                "nextmonth" "1.1in" "1in"))
790       (insert "\\hbox to \\textwidth{")
791       (cal-tex-hfill)
792       (insert "\\lastmonth")
793       (cal-tex-hfill)
794       (insert "\\thismonth")
795       (cal-tex-hfill)
796       (insert "\\nextmonth")
797       (cal-tex-hfill)
798       (insert "}")
799       (cal-tex-nl)
800       (cal-tex-b-parbox "l" "\\textwidth")
801       (calendar-for-loop j from 4 to 7 do
802         (cal-tex-week-hours date holidays "5")
803         (setq date (cal-tex-incr-date date)))
804       (cal-tex-e-parbox)
805       (setq month (extract-calendar-month date))
806       (setq year (extract-calendar-year date))
807       (if (/= i n)
808           (progn
809             (run-hooks 'cal-tex-week-hook)
810             (cal-tex-newpage))))
811     (cal-tex-end-document)
812     (run-hooks 'cal-tex-hook)))
813
814 (defun cal-tex-cursor-week-iso (&optional arg)
815   "Make a LaTeX calendar buffer for a one page ISO-style weekly calendar.
816 Optional prefix argument specifies number of weeks (default 1).
817 The calendar shows holiday and diary entries if
818 `cal-tex-holidays' and `cal-tex-diary', respectively, are non-nil."
819   (interactive "p")
820   (let* ((n (if arg arg 1))
821          (date (calendar-gregorian-from-absolute
822                 (calendar-dayname-on-or-before
823                  1
824                  (calendar-absolute-from-gregorian
825                   (calendar-cursor-to-date t)))))
826          (month (extract-calendar-month date))
827          (year (extract-calendar-year date))
828          (day (extract-calendar-day date))
829          (holidays (if cal-tex-holidays
830                        (cal-tex-list-holidays
831                         (calendar-absolute-from-gregorian date)
832                         (+ (* 7 n)
833                            (calendar-absolute-from-gregorian date)))))
834          (diary-list (if cal-tex-diary
835                          (cal-tex-list-diary-entries
836                           (calendar-absolute-from-gregorian
837                            (list month 1 year))
838                         (+ (* 7 n)
839                            (calendar-absolute-from-gregorian date))))))
840     (cal-tex-preamble "11pt")
841     (cal-tex-cmd "\\textwidth 6.5in")
842     (cal-tex-cmd "\\textheight 10.5in")