1;;; cal-tex.el --- calendar functions for printing calendars with LaTeX
2
3;; Copyright (C) 1995, 2001, 2002, 2003, 2004, 2005, 2006, 2007
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 2, 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.
58Sunday is 0, Monday is 1, and so on.  The default is to print from Sunday to
59Saturday.  For example, use
60
61                    (setq cal-tex-which-days '(1 3 5))
62
63to only print Monday, Wednesday, Friday."
64  :type '(repeat integer)
65  :group 'calendar-tex)
66
67(defcustom cal-tex-holidays t
68  "*If t (default), then the holidays are also printed.
69If 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  "*If t, the diary entries are printed in the calendar."
75  :type 'boolean
76  :group 'calendar-tex)
77
78(defcustom cal-tex-rules nil
79  "*If t, pages will be ruled in some styles."
80  :type 'boolean
81  :group 'calendar-tex)
82
83(defcustom cal-tex-daily-string
84  '(let* ((year (extract-calendar-year date))
85          (day  (calendar-day-number date))
86          (days-remaining (- (calendar-day-number (list 12 31 year)) day)))
87     (format "%d/%d" day  days-remaining))
88  "*An expression in the variable `date' whose value is placed on date.
89The string resulting from evaluating this expression is placed at the bottom
90center of `date' on the monthly calendar, next to the date in the weekly
91calendars, and in the top center of daily calendars.
92
93Default is ordinal day number of the year and the number of days remaining.
94As an example of what you do, setting this to
95
96    '(progn
97       (require 'cal-hebrew)
98       (calendar-hebrew-date-string date))
99
100will put the Hebrew date at the bottom of each day."
101  :type 'sexp
102  :group 'calendar-tex)
103
104(defcustom cal-tex-buffer "calendar.tex"
105  "*The name for the tex-ed calendar."
106  :type 'string
107  :group 'calendar-tex)
108
109(defcustom cal-tex-24 nil
110  "*If t, use a 24 hour clock in the daily calendar."
111  :type 'boolean
112  :group 'calendar-tex)
113
114(defcustom cal-tex-daily-start 8
115  "*The first hour of the daily calendar page."
116  :type 'integer
117  :group 'calendar-tex)
118
119(defcustom cal-tex-daily-end 20
120  "*The last hour of the daily calendar page."
121  :type 'integer
122  :group 'calendar-tex)
123
124(defcustom cal-tex-preamble-extra nil
125  "A string giving extra LaTeX commands to insert in the calendar preamble.
126For example, to include extra packages:
127\"\\\\usepackage{foo}\\n\\\\usepackage{bar}\\n\"."
128  :type 'string
129  :group 'calendar-tex
130  :version "22.1")
131
132(defcustom cal-tex-hook nil
133  "*List of functions called after any LaTeX calendar buffer is generated.
134You can use this to do postprocessing on the buffer.  For example, to change
135characters with diacritical marks to their LaTeX equivalents, use
136     (add-hook 'cal-tex-hook
137               '(lambda () (iso-iso2tex (point-min) (point-max))))"
138  :type 'hook
139  :group 'calendar-tex)
140
141(defcustom cal-tex-year-hook nil
142  "*List of functions called after a LaTeX year calendar buffer is generated."
143  :type 'hook
144  :group 'calendar-tex)
145
146(defcustom cal-tex-month-hook nil
147  "*List of functions called after a LaTeX month calendar buffer is generated."
148  :type 'hook
149  :group 'calendar-tex)
150
151(defcustom cal-tex-week-hook nil
152  "*List of functions called after a LaTeX week calendar buffer is generated."
153  :type 'hook
154  :group 'calendar-tex)
155
156(defcustom cal-tex-daily-hook nil
157  "*List of functions called after a LaTeX daily calendar buffer is generated."
158  :type 'hook
159  :group 'calendar-tex)
160
161;;;
162;;; Definitions for LaTeX code
163;;;
164
165(defvar  cal-tex-day-prefix "\\caldate{%s}{%s}"
166  "The initial LaTeX code for a day.
167The holidays, diary entries, bottom string, and the text follow.")
168
169(defvar cal-tex-day-name-format "\\myday{%s}%%"
170  "The format for LaTeX code for a day name.  The names are taken from
171`calendar-day-name-array'.")
172
173(defvar cal-tex-cal-one-month
174"\\def\\calmonth#1#2%
175{\\begin{center}%
176\\Huge\\bf\\uppercase{#1} #2 \\\\[1cm]%
177\\end{center}}%
178\\vspace*{-1.5cm}%
179%
180"
181  "LaTeX code for the month header")
182
183(defvar cal-tex-cal-multi-month
184"\\def\\calmonth#1#2#3#4%
185{\\begin{center}%
186\\Huge\\bf #1 #2---#3 #4\\\\[1cm]%
187\\end{center}}%
188\\vspace*{-1.5cm}%
189%
190"
191  "LaTeX code for the month header")
192
193(defvar cal-tex-myday
194"\\renewcommand{\\myday}[1]%
195{\\makebox[\\cellwidth]{\\hfill\\large\\bf#1\\hfill}}
196%
197"
198  "LaTeX code for a day heading")
199
200(defvar cal-tex-caldate
201"\\fboxsep=0pt
202\\long\\def\\caldate#1#2#3#4#5#6{%
203    \\fbox{\\hbox to\\cellwidth{%
204     \\vbox to\\cellheight{%
205       \\hbox to\\cellwidth{%
206          {\\hspace*{1mm}\\Large \\bf \\strut #2}\\hspace{.05\\cellwidth}%
207          \\raisebox{\\holidaymult\\cellheight}%
208                   {\\parbox[t]{.75\\cellwidth}{\\tiny \\raggedright #4}}}
209       \\hbox to\\cellwidth{%
210           \\hspace*{1mm}\\parbox{.95\\cellwidth}{\\tiny \\raggedright #3}}
211       \\hspace*{1mm}%
212       \\hbox to\\cellwidth{#6}%
213       \\vfill%
214       \\hbox to\\cellwidth{\\hfill \\tiny #5 \\hfill}%
215       \\vskip 1.4pt}%
216     \\hskip -0.4pt}}}
217"
218  "LaTeX code to insert one box with date info in calendar.
219This definition is the heart of the calendar!")
220
221(defun cal-tex-list-holidays (d1 d2)
222  "Generate a list of all holidays from absolute date D1 to D2."
223  (let* ((start (calendar-gregorian-from-absolute d1))
224         (displayed-month (extract-calendar-month start))
225         (displayed-year (extract-calendar-year start))
226         (end (calendar-gregorian-from-absolute d2))
227         (end-month (extract-calendar-month end))
228         (end-year (extract-calendar-year end))
229         (number-of-intervals
230          (1+ (/ (calendar-interval displayed-month displayed-year
231                                    end-month end-year)
232                 3)))
233         (holidays nil)
234         (in-range))
235    (increment-calendar-month displayed-month displayed-year 1)
236    (calendar-for-loop i from 1 to number-of-intervals do
237      (setq holidays (append holidays (calendar-holiday-list)))
238      (increment-calendar-month displayed-month displayed-year 3))
239    (while holidays
240      (and (car (car holidays))
241           (let ((a (calendar-absolute-from-gregorian (car (car holidays)))))
242             (and (<= d1 a) (<= a d2)))
243           (setq in-range (append (list (car holidays)) in-range)))
244      (setq holidays (cdr holidays)))
245    in-range))
246
247(defun cal-tex-list-diary-entries (d1 d2)
248  "Generate a list of all diary-entries from absolute date D1 to D2."
249  (let ((diary-list-include-blanks nil)
250        (diary-display-hook 'ignore))
251    (diary-list-entries
252     (calendar-gregorian-from-absolute d1)
253     (1+ (- d2 d1)))))
254
255(defun cal-tex-preamble (&optional args)
256  "Insert the LaTeX preamble.
257Preamble Includes initial definitions for various LaTeX commands.
258Optional ARGS are included."
259  (set-buffer (get-buffer-create cal-tex-buffer))
260  (erase-buffer)
261  (insert "\\documentclass")
262  (if args
263      (insert "[" args "]"))
264  (insert "{article}\n")
265  (if (stringp cal-tex-preamble-extra)
266      (insert cal-tex-preamble-extra "\n"))
267  (insert "\\hbadness 20000
268\\hfuzz=1000pt
269\\vbadness 20000
270\\lineskip 0pt
271\\marginparwidth 0pt
272\\oddsidemargin  -2cm
273\\evensidemargin -2cm
274\\marginparsep   0pt
275\\topmargin      0pt
276\\textwidth      7.5in
277\\textheight     9.5in
278\\newlength{\\cellwidth}
279\\newlength{\\cellheight}
280\\newlength{\\boxwidth}
281\\newlength{\\boxheight}
282\\newlength{\\cellsize}
283\\newcommand{\\myday}[1]{}
284\\newcommand{\\caldate}[6]{}
285\\newcommand{\\nocaldate}[6]{}
286\\newcommand{\\calsmall}[6]{}
287%
288"))
289
290;;;
291;;;  Yearly calendars
292;;;
293
294(defun cal-tex-cursor-year (&optional arg)
295  "Make a buffer with LaTeX commands for the year cursor is on.
296Optional prefix argument specifies number of years."
297  (interactive "p")
298  (cal-tex-year (extract-calendar-year (calendar-cursor-to-date t))
299                (if arg arg 1)))
300
301(defun cal-tex-cursor-year-landscape (&optional arg)
302  "Make a buffer with LaTeX commands for the year cursor is on.
303Optional prefix argument specifies number of years."
304  (interactive "p")
305  (cal-tex-year (extract-calendar-year (calendar-cursor-to-date t))
306                (if arg arg 1)
307                t))
308
309(defun cal-tex-year (year n &optional landscape)
310  "Make a one page yearly calendar of YEAR; do this for N years.
311There are four rows of three months each, unless optional LANDSCAPE is t,
312in which case the calendar isprinted in landscape mode with three rows of
313four months each."
314  (cal-tex-insert-preamble 1 landscape "12pt")
315  (if landscape
316      (cal-tex-vspace "-.6cm")
317    (cal-tex-vspace "-3.1cm"))
318  (calendar-for-loop j from 1 to n do
319     (insert "\\vfill%\n")
320     (cal-tex-b-center)
321     (cal-tex-Huge (number-to-string year))
322     (cal-tex-e-center)
323     (cal-tex-vspace "1cm")
324     (cal-tex-b-center)
325     (cal-tex-b-parbox "l" (if landscape "5.9in" "4.3in"))
326     (insert "\n")
327     (cal-tex-noindent)
328     (cal-tex-nl)
329     (calendar-for-loop i from 1 to 12 do
330        (insert (cal-tex-mini-calendar i year "month" "1.1in" "1in"))
331        (insert "\\month")
332        (cal-tex-hspace "0.5in")
333        (if (zerop (mod i (if landscape 4 3)))
334            (cal-tex-nl "0.5in")))
335     (cal-tex-e-parbox)
336     (cal-tex-e-center)
337     (insert "\\vfill%\n")
338     (setq year (1+ year))
339     (if (/= j n)
340         (cal-tex-newpage)
341       (cal-tex-end-document))
342     (run-hooks 'cal-tex-year-hook))
343  (run-hooks 'cal-tex-hook))
344
345(defun cal-tex-cursor-filofax-year (&optional arg)
346  "Make a Filofax one page yearly calendar of year indicated by cursor.
347Optional parameter specifies number of years."
348  (interactive "p")
349  (let* ((n (if arg arg 1))
350         (year (extract-calendar-year (calendar-cursor-to-date t))))
351    (cal-tex-preamble "twoside")
352    (cal-tex-cmd "\\textwidth 3.25in")
353    (cal-tex-cmd "\\textheight 6.5in")
354    (cal-tex-cmd "\\oddsidemargin 1.675in")
355    (cal-tex-cmd "\\evensidemargin 1.675in")
356    (cal-tex-cmd "\\topmargin 0pt")
357    (cal-tex-cmd "\\headheight -0.875in")
358    (cal-tex-cmd "\\fboxsep 0.5mm")
359    (cal-tex-cmd "\\pagestyle{empty}")
360    (cal-tex-b-document)
361    (cal-tex-cmd "\\vspace*{0.25in}")
362    (calendar-for-loop j from 1 to n do
363       (insert (format "\\hfil {\\Large \\bf %s} \\hfil\\\\\n" year))
364       (cal-tex-b-center)
365       (cal-tex-b-parbox "l" "\\textwidth")
366       (insert "\n")
367       (cal-tex-noindent)
368       (cal-tex-nl)
369       (let ((month-names; don't use default in case user changed it
370              ;; These are only used to define the command names, not
371              ;; the names of the months they insert.
372              ["January" "February" "March" "April" "May" "June"
373               "July" "August" "September" "October" "November" "December"]))
374         (calendar-for-loop i from 1 to 12 do
375            (insert (cal-tex-mini-calendar i year
376                                           (aref month-names (1- i))
377                                           "1in" ".9in" "tiny" "0.6mm"))))
378       (insert
379"\\noindent\\fbox{\\January}\\fbox{\\February}\\fbox{\\March}\\\\
380\\noindent\\fbox{\\April}\\fbox{\\May}\\fbox{\\June}\\\\
381\\noindent\\fbox{\\July}\\fbox{\\August}\\fbox{\\September}\\\\
382\\noindent\\fbox{\\October}\\fbox{\\November}\\fbox{\\December}
383")
384       (cal-tex-e-parbox)
385       (cal-tex-e-center)
386       (setq year (1+ year))
387       (if (= j n)
388           (cal-tex-end-document)
389         (cal-tex-newpage)
390         (cal-tex-cmd "\\vspace*{0.25in}"))
391       (run-hooks 'cal-tex-year-hook))
392    (run-hooks 'cal-tex-hook)))
393
394;;;
395;;;  Monthly calendars
396;;;
397
398(defun cal-tex-cursor-month-landscape (&optional arg)
399  "Make a buffer with LaTeX commands for the month cursor is on.
400Optional prefix argument specifies number of months to be produced.
401The output is in landscape format, one month to a page."
402  (interactive "p")
403  (let* ((n (if arg arg 1))
404         (date (calendar-cursor-to-date t))
405         (month (extract-calendar-month date))
406         (year (extract-calendar-year date))
407         (end-month month)
408         (end-year year)
409         (cal-tex-which-days '(0 1 2 3 4 5 6)))
410    (increment-calendar-month end-month end-year (1- n))
411    (let ((diary-list (if cal-tex-diary
412                          (cal-tex-list-diary-entries
413                           (calendar-absolute-from-gregorian
414                            (list month 1 year))
415                           (calendar-absolute-from-gregorian
416                            (list end-month
417                                  (calendar-last-day-of-month
418                                   end-month end-year)
419                                  end-year)))))
420          (holidays (if cal-tex-holidays
421                        (cal-tex-list-holidays
422                         (calendar-absolute-from-gregorian
423                          (list month 1 year))
424                         (calendar-absolute-from-gregorian
425                          (list end-month
426                                (calendar-last-day-of-month end-month end-year)
427                                end-year)))))
428          (other-month)
429          (other-year)
430          (small-months-at-start))
431      (cal-tex-insert-preamble (cal-tex-number-weeks month year 1) t "12pt")
432      (cal-tex-cmd cal-tex-cal-one-month)
433      (calendar-for-loop i from 1 to n do
434         (setq other-month month)
435         (setq other-year year)
436         (increment-calendar-month other-month other-year -1)
437         (insert (cal-tex-mini-calendar other-month other-year "lastmonth"
438                                        "\\cellwidth" "\\cellheight"))
439         (increment-calendar-month other-month other-year 2)
440         (insert (cal-tex-mini-calendar other-month other-year "nextmonth"
441                                        "\\cellwidth" "\\cellheight"))
442         (cal-tex-insert-month-header 1 month year month year)
443         (cal-tex-insert-day-names)
444         (cal-tex-nl ".2cm")
445         (setq small-months-at-start
446               (< 1 (mod (- (calendar-day-of-week (list month 1 year))
447                            calendar-week-start-day)
448                         7)))
449         (if small-months-at-start
450             (insert "\\lastmonth\\nextmonth\\hspace*{-2\\cellwidth}"))
451         (cal-tex-insert-blank-days month year cal-tex-day-prefix)
452         (cal-tex-insert-days month year diary-list holidays
453                              cal-tex-day-prefix)
454         (cal-tex-insert-blank-days-at-end month year cal-tex-day-prefix)
455         (if (and (not small-months-at-start)
456                  (< 1 (mod (- (1- calendar-week-start-day)
457                               (calendar-day-of-week
458                                (list month
459                                      (calendar-last-day-of-month month year)
460                                      year)))
461                                 7)))
462             (insert "\\vspace*{-\\cellwidth}\\hspace*{-2\\cellwidth}"
463                     "\\lastmonth\\nextmonth%
464"))
465         (if (/= i n)
466             (progn
467               (run-hooks 'cal-tex-month-hook)
468               (cal-tex-newpage)
469               (increment-calendar-month month year 1)
470               (cal-tex-vspace "-2cm")
471               (cal-tex-insert-preamble
472                (cal-tex-number-weeks month year 1) t "12pt" t))))
473      (cal-tex-end-document)
474      (run-hooks 'cal-tex-hook))))
475
476(defun cal-tex-cursor-month (arg)
477  "Make a buffer with LaTeX commands for the month cursor is on.
478Optional prefix argument specifies number of months to be produced.
479Calendar is condensed onto one page."
480  (interactive "p")
481  (let* ((date (calendar-cursor-to-date t))
482         (month (extract-calendar-month date))
483         (year (extract-calendar-year date))
484         (end-month month)
485         (end-year year)
486         (n (if arg arg 1)))
487    (increment-calendar-month end-month end-year (1- n))
488    (let ((diary-list (if cal-tex-diary
489                          (cal-tex-list-diary-entries
490                           (calendar-absolute-from-gregorian
491                            (list month 1 year))
492                           (calendar-absolute-from-gregorian
493                            (list end-month
494                                  (calendar-last-day-of-month
495                                   end-month end-year)
496                                  end-year)))))
497          (holidays (if cal-tex-holidays
498                        (cal-tex-list-holidays
499                         (calendar-absolute-from-gregorian
500                          (list month 1 year))
501                         (calendar-absolute-from-gregorian
502                          (list end-month
503                                (calendar-last-day-of-month end-month end-year)
504                                end-year)))))
505          (other-month)
506          (other-year))
507      (cal-tex-insert-preamble (cal-tex-number-weeks month year n) nil"12pt")
508      (if (> n 1)
509          (cal-tex-cmd cal-tex-cal-multi-month)
510        (cal-tex-cmd cal-tex-cal-one-month))
511      (cal-tex-insert-month-header n month year end-month end-year)
512      (cal-tex-insert-day-names)
513      (cal-tex-nl ".2cm")
514      (cal-tex-insert-blank-days month year cal-tex-day-prefix)
515      (calendar-for-loop i from 1 to n do
516         (setq other-month month)
517         (setq other-year year)
518         (cal-tex-insert-days month year diary-list holidays
519                              cal-tex-day-prefix)
520         (if (= (mod (calendar-absolute-from-gregorian
521                          (list month
522                                (calendar-last-day-of-month month year)
523                                year))
524                         7)
525                6); last day of month was Saturday
526           (progn
527             (cal-tex-hfill)
528             (cal-tex-nl)))
529         (increment-calendar-month month year 1))
530      (cal-tex-insert-blank-days-at-end end-month end-year cal-tex-day-prefix)
531      (cal-tex-end-document)))
532  (run-hooks 'cal-tex-hook))
533
534(defun cal-tex-insert-days (month year diary-list holidays day-format)
535  "Insert LaTeX commands for a range of days in monthly calendars.
536LaTeX commands are inserted for the days of the MONTH in YEAR.
537Diary entries on DIARY-LIST are included. Holidays on HOLIDAYS are included.
538Each day is formatted using format DAY-FORMAT."
539  (let* ((blank-days;; at start of month
540          (mod
541           (- (calendar-day-of-week (list month 1 year))
542              calendar-week-start-day)
543           7))
544         (date)
545         (last (calendar-last-day-of-month month year)))
546    (calendar-for-loop i from 1 to last do
547       (setq date (list month i year))
548       (if (memq (calendar-day-of-week date) cal-tex-which-days)
549           (progn
550             (insert (format day-format (cal-tex-month-name month) i))
551             (cal-tex-arg (cal-tex-latexify-list diary-list date))
552             (cal-tex-arg (cal-tex-latexify-list holidays date))
553             (cal-tex-arg (eval cal-tex-daily-string))
554             (cal-tex-arg)
555             (cal-tex-comment)))
556       (if (and (zerop (mod (+ i blank-days) 7))
557                (/= i last))
558           (progn
559             (cal-tex-hfill)
560             (cal-tex-nl))))))
561
562(defun cal-tex-insert-day-names ()
563  "Insert the names of the days at top of a monthly calendar."
564  (calendar-for-loop i from 0 to 6 do
565     (if (memq i cal-tex-which-days)
566         (insert (format cal-tex-day-name-format
567                         (cal-tex-LaTeXify-string
568                          (aref calendar-day-name-array
569                                (mod (+ calendar-week-start-day i) 7))))))
570     (cal-tex-comment)))
571
572(defun cal-tex-insert-month-header (n month year end-month end-year)
573  "Create a title for a calendar.
574A title is inserted for a calendar with N months starting with
575MONTH YEAR and ending with END-MONTH END-YEAR."
576  (let ((month-name (cal-tex-month-name  month))
577         (end-month-name (cal-tex-month-name  end-month)))
578    (if (= 1 n)
579        (insert (format "\\calmonth{%s}{%s}\n\\vspace*{-0.5cm}"
580                month-name year) )
581        (insert (format "\\calmonth{%s}{%s}{%s}{%s}\n\\vspace*{-0.5cm}"
582                month-name year end-month-name end-year))))
583  (cal-tex-comment))
584
585(defun cal-tex-insert-blank-days (month year day-format)
586  "Insert code for initial days not in calendar.
587Insert LaTeX code for the blank days at the beginning of the MONTH in
588YEAR.  The entry is formatted using DAY-FORMAT.  If the entire week is
589blank, no days are inserted."
590  (if (cal-tex-first-blank-p month year)
591      (let* ((blank-days;; at start of month
592              (mod
593               (- (calendar-day-of-week (list month 1 year))
594                  calendar-week-start-day)
595               7)))
596        (calendar-for-loop i from 0 to (1- blank-days) do
597            (if (memq i cal-tex-which-days)
598                (insert (format day-format " " " ") "{}{}{}{}%\n"))))))
599
600(defun cal-tex-insert-blank-days-at-end (month year day-format)
601  "Insert code for final days not in calendar.
602Insert LaTeX code for the blank days at the end of the MONTH in YEAR.
603The entry is formatted using DAY-FORMAT."
604  (if (cal-tex-last-blank-p month year)
605      (let* ((last-day (calendar-last-day-of-month month year))
606             (blank-days;; at end of month
607              (mod
608               (- (calendar-day-of-week (list month last-day year))
609                  calendar-week-start-day)
610               7)))
611        (calendar-for-loop i from (1+ blank-days) to 6 do
612           (if (memq i cal-tex-which-days)
613               (insert (format day-format "" "") "{}{}{}{}%\n"))))))
614
615(defun cal-tex-first-blank-p (month year)
616  "Determine if any days of the first week will be printed.
617Return t if there will there be any days of the first week printed
618in the calendar starting in MONTH YEAR."
619  (let ((any-days nil)
620        (the-saturday))                 ;the day of week of 1st Saturday
621    (calendar-for-loop i from 1 to 7 do
622       (if (= 6 (calendar-day-of-week (list month i year)))
623           (setq the-saturday i)))
624    (calendar-for-loop i from 1 to the-saturday do
625       (if (memq (calendar-day-of-week (list month i year))
626                 cal-tex-which-days)
627           (setq any-days t)))
628    any-days))
629
630(defun cal-tex-last-blank-p (month year)
631  "Determine if any days of the last week will be printed.
632Return t if there will there be any days of the last week printed
633in the calendar starting in MONTH YEAR."
634  (let ((any-days nil)
635        (last-day (calendar-last-day-of-month month year))
636        (the-sunday))                   ;the day of week of last Sunday
637    (calendar-for-loop i from (- last-day 6) to last-day do
638       (if (= 0 (calendar-day-of-week (list month i year)))
639           (setq the-sunday i)))
640    (calendar-for-loop i from the-sunday to last-day do
641       (if (memq (calendar-day-of-week (list month i year))
642                 cal-tex-which-days)
643           (setq any-days t)))
644    any-days))
645
646(defun cal-tex-number-weeks (month year n)
647  "Determine the number of weeks in a range of dates.
648Compute the number of  weeks in the calendar starting with MONTH and YEAR,
649and lasting N months, including only the days in WHICH-DAYS. As it stands,
650this is only an upper bound."
651  (let ((d (list month 1 year)))
652    (increment-calendar-month month year (1- n))
653    (/ (- (calendar-dayname-on-or-before
654           calendar-week-start-day
655           (+ 7 (calendar-absolute-from-gregorian
656                   (list month (calendar-last-day-of-month month year) year))))
657          (calendar-dayname-on-or-before
658           calendar-week-start-day
659           (calendar-absolute-from-gregorian d)))
660       7)))
661
662;;;
663;;; Weekly calendars
664;;;
665
666(defvar cal-tex-LaTeX-hourbox
667  "\\newcommand{\\hourbox}[2]%
668{\\makebox[2em]{\\rule{0cm}{#2ex}#1}\\rule{3in}{.15mm}}\n"
669  "One hour and a line on the right.")
670
671(defun cal-tex-cursor-week (&optional arg)
672  "Make a buffer with LaTeX commands for a two-page one-week calendar.
673It applies to the week that point is in.
674Optional prefix argument specifies number of weeks.
675Holidays are included if `cal-tex-holidays' is t."
676  (interactive "p")
677  (let* ((n (if arg arg 1))
678         (date (calendar-gregorian-from-absolute
679                (calendar-dayname-on-or-before
680                 calendar-week-start-day
681                 (calendar-absolute-from-gregorian
682                  (calendar-cursor-to-date t)))))
683         (month (extract-calendar-month date))
684         (year (extract-calendar-year date))
685         (holidays (if cal-tex-holidays
686                       (cal-tex-list-holidays
687                        (calendar-absolute-from-gregorian date)
688                        (+ (* 7 n)
689                           (calendar-absolute-from-gregorian date))))))
690    (cal-tex-preamble "11pt")
691    (cal-tex-cmd "\\textwidth   6.5in")
692    (cal-tex-cmd "\\textheight 10.5in")
693    (cal-tex-cmd "\\oddsidemargin 0in")
694    (cal-tex-cmd "\\evensidemargin 0in")
695    (insert cal-tex-LaTeX-hourbox)
696    (cal-tex-b-document)
697    (cal-tex-cmd "\\pagestyle{empty}")
698    (calendar-for-loop i from 1 to n do
699       (cal-tex-vspace "-1.5in")
700       (cal-tex-b-center)
701       (cal-tex-Huge-bf (format "\\uppercase{%s}"
702                                (cal-tex-month-name month)))
703       (cal-tex-hspace "2em")
704       (cal-tex-Huge-bf (number-to-string year))
705       (cal-tex-nl ".5cm")
706       (cal-tex-e-center)
707       (cal-tex-hspace "-.2in")
708       (cal-tex-b-parbox "l" "7in")
709       (calendar-for-loop j from 1 to 7 do
710          (cal-tex-week-hours date holidays "3.1")
711          (setq date (cal-tex-incr-date date)))
712       (cal-tex-e-parbox)
713       (setq month (extract-calendar-month date))
714       (setq year (extract-calendar-year date))
715       (if (/= i n)
716           (progn
717             (run-hooks 'cal-tex-week-hook)
718             (cal-tex-newpage))))
719    (cal-tex-end-document)
720    (run-hooks 'cal-tex-hook)))
721
722(defun cal-tex-cursor-week2 (&optional arg)
723  "Make a buffer with LaTeX commands for a two-page one-week calendar.
724It applies to the week that point is in.
725Optional prefix argument specifies number of weeks.
726Holidays are included if `cal-tex-holidays' is t."
727  (interactive "p")
728  (let* ((n (if arg arg 1))
729         (date (calendar-gregorian-from-absolute
730                (calendar-dayname-on-or-before
731                 calendar-week-start-day
732                 (calendar-absolute-from-gregorian
733                  (calendar-cursor-to-date t)))))
734         (month (extract-calendar-month date))
735         (year (extract-calendar-year date))
736         (d date)
737         (holidays (if cal-tex-holidays
738                       (cal-tex-list-holidays
739                        (calendar-absolute-from-gregorian date)
740                        (+ (* 7 n)
741                           (calendar-absolute-from-gregorian date))))))
742    (cal-tex-preamble "12pt")
743    (cal-tex-cmd "\\textwidth   6.5in")
744    (cal-tex-cmd "\\textheight 10.5in")
745    (cal-tex-cmd "\\oddsidemargin 0in")
746    (cal-tex-cmd "\\evensidemargin 0in")
747    (insert cal-tex-LaTeX-hourbox)
748    (cal-tex-b-document)
749    (cal-tex-cmd "\\pagestyle{empty}")
750    (calendar-for-loop i from 1 to n do
751       (cal-tex-vspace "-1.5in")
752       (cal-tex-b-center)
753       (cal-tex-Huge-bf (format "\\uppercase{%s}"
754                                (cal-tex-month-name month)))
755       (cal-tex-hspace "2em")
756       (cal-tex-Huge-bf (number-to-string year))
757       (cal-tex-nl ".5cm")
758       (cal-tex-e-center)
759       (cal-tex-hspace "-.2in")
760       (cal-tex-b-parbox "l" "\\textwidth")
761       (calendar-for-loop j from 1 to 3 do
762          (cal-tex-week-hours date holidays "5")
763          (setq date (cal-tex-incr-date date)))
764       (cal-tex-e-parbox)
765       (cal-tex-nl)
766       (insert (cal-tex-mini-calendar
767                (extract-calendar-month (cal-tex-previous-month date))
768                (extract-calendar-year (cal-tex-previous-month date))
769                "lastmonth" "1.1in" "1in"))
770       (insert (cal-tex-mini-calendar
771                (extract-calendar-month date)
772                (extract-calendar-year date)
773                "thismonth" "1.1in" "1in"))
774       (insert (cal-tex-mini-calendar
775                (extract-calendar-month (cal-tex-next-month date))
776                (extract-calendar-year (cal-tex-next-month date))
777                "nextmonth" "1.1in" "1in"))
778       (insert "\\hbox to \\textwidth{")
779       (cal-tex-hfill)
780       (insert "\\lastmonth")
781       (cal-tex-hfill)
782       (insert "\\thismonth")
783       (cal-tex-hfill)
784       (insert "\\nextmonth")
785       (cal-tex-hfill)
786       (insert "}")
787       (cal-tex-nl)
788       (cal-tex-b-parbox "l" "\\textwidth")
789       (calendar-for-loop j from 4 to 7 do
790                          (cal-tex-week-hours date holidays "5")
791                          (setq date (cal-tex-incr-date date)))
792       (cal-tex-e-parbox)
793       (setq month (extract-calendar-month date))
794       (setq year (extract-calendar-year date))
795       (if (/= i n)
796           (progn
797             (run-hooks 'cal-tex-week-hook)
798             (cal-tex-newpage))))
799    (cal-tex-end-document)
800    (run-hooks 'cal-tex-hook)))
801
802(defun cal-tex-cursor-week-iso (&optional arg)
803  "Make a buffer with LaTeX commands for a one page ISO-style weekly calendar.
804Optional prefix argument specifies number of weeks.
805Diary entries are included if `cal-tex-diary' is t.
806Holidays are included if `cal-tex-holidays' is t."
807  (interactive "p")
808  (let* ((n (if arg arg 1))
809         (date (calendar-gregorian-from-absolute
810                (calendar-dayname-on-or-before
811                 1
812                 (calendar-absolute-from-gregorian
813                  (calendar-cursor-to-date t)))))
814         (month (extract-calendar-month date))
815         (year (extract-calendar-year date))
816         (day (extract-calendar-day date))
817         (holidays (if cal-tex-holidays
818                       (cal-tex-list-holidays
819                        (calendar-absolute-from-gregorian date)
820                        (+ (* 7 n)
821                           (calendar-absolute-from-gregorian date)))))
822         (diary-list (if cal-tex-diary
823                         (cal-tex-list-diary-entries
824                          (calendar-absolute-from-gregorian
825                           (list month 1 year))
826                        (+ (* 7 n)
827                           (calendar-absolute-from-gregorian date))))))
828    (cal-tex-preamble "11pt")
829    (cal-tex-cmd "\\textwidth 6.5in")
830    (cal-tex-cmd "\\textheight 10.5in")
831    (cal-tex-cmd "\\oddsidemargin 0in")
832    (cal-tex-cmd "\\evensidemargin 0in")
833    (cal-tex-b-document)
834    (cal-tex-cmd "\\pagestyle{empty}")
835    (calendar-for-loop i from 1 to n do
836       (cal-tex-vspace "-1.5in")
837       (cal-tex-b-center)
838       (cal-tex-Huge-bf
839        (let* ((d (calendar-iso-from-absolute
840                   (calendar-absolute-from-gregorian date))))
841          (format "Week %d of %d"
842                  (extract-calendar-month d)
843                  (extract-calendar-year d))))
844       (cal-tex-nl ".5cm")
845       (cal-tex-e-center)
846       (cal-tex-b-parbox "l" "\\textwidth")
847       (calendar-for-loop j from 1 to 7 do
848          (cal-tex-b-parbox "t" "\\textwidth")
849          (cal-tex-b-parbox "t" "\\textwidth")
850          (cal-tex-rule "0pt" "\\textwidth" ".2mm")
851          (cal-tex-nl)
852          (cal-tex-b-parbox "t" "\\textwidth")
853          (cal-tex-large-bf (cal-tex-LaTeXify-string (calendar-day-name date)))
854          (insert ", ")
855          (cal-tex-large-bf (cal-tex-month-name month))
856          (insert " ")
857          (cal-tex-large-bf (number-to-string day))
858          (if (not (string= "" (cal-tex-latexify-list holidays date)))
859              (progn
860                (insert ": ")
861                (cal-tex-large-bf (cal-tex-latexify-list holidays date "; "))))
862          (cal-tex-hfill)
863          (insert " " (eval cal-tex-daily-string))
864          (cal-tex-e-parbox)
865          (cal-tex-nl)
866          (cal-tex-noindent)
867          (cal-tex-b-parbox "t" "\\textwidth")
868          (if (not (string= "" (cal-tex-latexify-list diary-list date)))
869              (progn
870                (insert "\\vbox to 0pt{")
871                (cal-tex-large-bf
872                 (cal-tex-latexify-list diary-list date))
873                (insert "}")))
874          (cal-tex-e-parbox)
875          (cal-tex-nl)
876          (setq date (cal-tex-incr-date date))
877          (setq month (extract-calendar-month date))
878          (setq day (extract-calendar-day date))
879          (cal-tex-e-parbox)
880          (cal-tex-e-parbox "2cm")
881          (cal-tex-nl)
882          (setq month (extract-calendar-month date))
883          (setq year (extract-calendar-year date)))
884       (cal-tex-e-parbox)
885       (if (/= i n)
886           (progn
887             (run-hooks 'cal-tex-week-hook)
888             (cal-tex-newpage))))
889    (cal-tex-end-document)
890    (run-hooks 'cal-tex-hook)))
891
892(defun cal-tex-week-hours (date holidays height)
893  "Insert hourly entries for DATE with HOLIDAYS, with line height HEIGHT."
894  (let ((month (extract-calendar-month date))
895        (day   (extract-calendar-day date))
896        (year  (extract-calendar-year date))
897        (afternoon))
898  (cal-tex-comment "begin cal-tex-week-hours")
899  (cal-tex-cmd  "\\ \\\\[-.2cm]")
900  (cal-tex-cmd "\\noindent")
901  (cal-tex-b-parbox "l" "6.8in")
902  (cal-tex-large-bf (cal-tex-LaTeXify-string (calendar-day-name date)))
903  (insert ", ")
904  (cal-tex-large-bf (cal-tex-month-name month))
905  (insert " ")
906  (cal-tex-large-bf (number-to-string day))
907  (if (not (string= "" (cal-tex-latexify-list holidays date)))
908      (progn
909        (insert ": ")
910        (cal-tex-large-bf (cal-tex-latexify-list holidays date "; "))))
911  (cal-tex-hfill)
912  (insert " " (eval cal-tex-daily-string))
913  (cal-tex-e-parbox)
914  (cal-tex-nl "-.3cm")
915  (cal-tex-rule "0pt" "6.8in" ".2mm")
916  (cal-tex-nl "-.1cm")
917  (calendar-for-loop i from 8 to 12 do
918     (if cal-tex-24
919         (setq afternoon (+ i 5))
920       (setq afternoon (- i 7)))
921     (cal-tex-cmd "\\hourbox"  (number-to-string i))
922     (cal-tex-arg height)
923     (cal-tex-hspace ".4cm")
924     (cal-tex-cmd "\\hourbox"  (number-to-string afternoon))
925     (cal-tex-arg height)
926     (cal-tex-nl))))
927
928(defun cal-tex-cursor-week-monday (&optional arg)
929  "Make a buffer with LaTeX commands for a two-page one-week calendar.
930It applies to the week that point is in, and starts on Monday.
931Optional prefix argument specifies number of weeks.
932Holidays are included if `cal-tex-holidays' is t."
933  (interactive "p")
934  (let* ((n (if arg arg 1))
935         (date (calendar-gregorian-from-absolute
936                (calendar-dayname-on-or-before
937                 0
938                 (calendar-absolute-from-gregorian
939                  (calendar-cursor-to-date t))))))
940    (cal-tex-preamble "11pt")
941    (cal-tex-cmd "\\textwidth   6.5in")
942    (cal-tex-cmd "\\textheight 10.5in")
943    (cal-tex-cmd "\\oddsidemargin 0in")
944    (cal-tex-cmd "\\evensidemargin 0in")
945    (cal-tex-b-document)
946    (calendar-for-loop i from 1 to n do
947       (cal-tex-vspace "-1cm")
948       (insert "\\noindent ")
949       (cal-tex-weekly4-box (cal-tex-incr-date date) nil)
950       (cal-tex-weekly4-box (cal-tex-incr-date date 4) nil)
951       (cal-tex-nl ".2cm")
952       (cal-tex-weekly4-box (cal-tex-incr-date date 2) nil)
953       (cal-tex-weekly4-box (cal-tex-incr-date date 5) nil)
954       (cal-tex-nl ".2cm")
955       (cal-tex-weekly4-box (cal-tex-incr-date date 3) nil)
956       (cal-tex-weekly4-box (cal-tex-incr-date date 6) t)
957       (if (/= i n)
958           (progn
959             (run-hooks 'cal-tex-week-hook)
960	     (setq date (cal-tex-incr-date date 7))
961             (cal-tex-newpage))))
962    (cal-tex-end-document)
963    (run-hooks 'cal-tex-hook)))
964
965(defun cal-tex-weekly4-box (date weekend)
966  "Make one box for DATE, different if WEEKEND."
967  (let* (
968	(day (extract-calendar-day date))
969	(month (extract-calendar-month date))
970	(year (extract-calendar-year date))
971	(dayname (cal-tex-LaTeXify-string (calendar-day-name date)))
972	(date1 (cal-tex-incr-date date))
973	(day1 (extract-calendar-day date1))
974	(month1 (extract-calendar-month date1))
975	(year1 (extract-calendar-year date1))
976	(dayname1 (cal-tex-LaTeXify-string (calendar-day-name date1)))
977	)
978    (cal-tex-b-framebox "8cm" "l")
979    (cal-tex-b-parbox "b" "7.5cm")
980    (insert (format "{\\Large\\bf %s,} %s/%s/%s\\\\\n" dayname month day year))
981    (cal-tex-rule "0pt" "7.5cm" ".5mm")
982    (cal-tex-nl)
983    (if (not weekend)
984	(progn
985	  (calendar-for-loop i from 8 to 12 do
986	     (insert (format "{\\large\\sf %d}\\\\\n" i)))
987	  (calendar-for-loop i from 1 to 5 do
988	     (insert (format "{\\large\\sf %d}\\\\\n"
989			     (if cal-tex-24 (+ i 12) i))))))
990    (cal-tex-nl ".5cm")
991    (if weekend
992	(progn
993	  (cal-tex-vspace "1cm")
994	  (insert "\\ \\vfill")
995	  (insert (format "{\\Large\\bf %s,} %s/%s/%s\\\\\n"
996			  dayname1 month1 day1 year1))
997	  (cal-tex-rule "0pt" "7.5cm" ".5mm")
998	  (cal-tex-nl "1.5cm")
999	  (cal-tex-vspace "1cm")))
1000     (cal-tex-e-parbox)
1001     (cal-tex-e-framebox)
1002     (cal-tex-hspace "1cm")))
1003
1004(defun cal-tex-cursor-filofax-2week (&optional arg)
1005  "Two-weeks-at-a-glance Filofax style calendar for week indicated by cursor.
1006Optional prefix argument specifies number of weeks.
1007Diary entries are included if `cal-tex-diary' is t.
1008Holidays are included if `cal-tex-holidays' is t."
1009  (interactive "p")
1010  (let* ((n (if arg arg 1))
1011         (date (calendar-gregorian-from-absolute
1012                (calendar-dayname-on-or-before
1013                 calendar-week-start-day
1014                 (calendar-absolute-from-gregorian
1015                  (calendar-cursor-to-date t)))))
1016         (month (extract-calendar-month date))
1017         (year (extract-calendar-year date))
1018         (day (extract-calendar-day date))
1019         (holidays (if cal-tex-holidays
1020                       (cal-tex-list-holidays
1021                        (calendar-absolute-from-gregorian date)
1022                        (+ (* 7 n)
1023                           (calendar-absolute-from-gregorian date)))))
1024         (diary-list (if cal-tex-diary
1025                         (cal-tex-list-diary-entries
1026                          (calendar-absolute-from-gregorian
1027                           (list month 1 year))
1028                        (+ (* 7 n)
1029                           (calendar-absolute-from-gregorian date))))))
1030    (cal-tex-preamble "twoside")
1031    (cal-tex-cmd "\\textwidth 3.25in")
1032    (cal-tex-cmd "\\textheight 6.5in")
1033    (cal-tex-cmd "\\oddsidemargin 1.75in")
1034    (cal-tex-cmd "\\evensidemargin 1.5in")
1035    (cal-tex-cmd "\\topmargin 0pt")
1036    (cal-tex-cmd "\\headheight -0.875in")
1037    (cal-tex-cmd "\\headsep 0.125in")
1038    (cal-tex-cmd "\\footskip .125in")
1039    (insert "\\def\\righthead#1{\\hfill {\\normalsize \\bf #1}\\\\[-6pt]}
1040\\long\\def\\rightday#1#2#3#4#5{%
1041   \\rule{\\textwidth}{0.3pt}\\\\%
1042   \\hbox to \\textwidth{%
1043     \\vbox to 0.7in{%
1044          \\vspace*{2pt}%
1045          \\hbox to \\textwidth{\\small #5 \\hfill #1 {\\normalsize \\bf #2}}%
1046          \\hbox to \\textwidth{\\vbox {\\raggedleft \\footnotesize \\em #4}}%
1047          \\hbox to \\textwidth{\\vbox to 0pt {\\noindent \\footnotesize #3}}}}\\\\}
1048\\def\\lefthead#1{\\noindent {\\normalsize \\bf #1}\\hfill\\\\[-6pt]}
1049\\long\\def\\leftday#1#2#3#4#5{%
1050   \\rule{\\textwidth}{0.3pt}\\\\%
1051   \\hbox to \\textwidth{%
1052     \\vbox to 0.7in{%
1053          \\vspace*{2pt}%
1054          \\hbox to \\textwidth{\\noindent {\\normalsize \\bf #2} \\small #1 \\hfill #5}%
1055          \\hbox to \\textwidth{\\vbox {\\noindent \\footnotesize \\em #4}}%
1056          \\hbox to \\textwidth{\\vbox to 0pt {\\noindent \\footnotesize #3}}}}\\\\}
1057")
1058    (cal-tex-b-document)
1059    (cal-tex-cmd "\\pagestyle{empty}")
1060    (calendar-for-loop i from 1 to n do
1061       (if (= (mod i 2) 1)
1062           (insert "\\righthead")
1063         (insert "\\lefthead"))
1064       (cal-tex-arg
1065        (let ((d (cal-tex-incr-date date 6)))
1066          (if (= (extract-calendar-month date)
1067                 (extract-calendar-month d))
1068              (format "%s %s"
1069                      (cal-tex-month-name
1070                       (extract-calendar-month date))
1071                      (extract-calendar-year date))
1072            (if (=  (extract-calendar-year date)
1073                    (extract-calendar-year d))
1074                (format "%s---%s %s"
1075                        (cal-tex-month-name
1076                         (extract-calendar-month date))
1077                        (cal-tex-month-name
1078                         (extract-calendar-month d))
1079                        (extract-calendar-year date))
1080              (format "%s %s---%s %s"
1081                      (cal-tex-month-name
1082                       (extract-calendar-month date))
1083                      (extract-calendar-year date)
1084                      (cal-tex-month-name (extract-calendar-month d))
1085                      (extract-calendar-year d))))))
1086       (insert "%\n")
1087       (calendar-for-loop j from 1 to 7 do
1088          (if (= (mod i 2) 1)
1089              (insert "\\rightday")
1090            (insert "\\leftday"))
1091          (cal-tex-arg (cal-tex-LaTeXify-string (calendar-day-name date)))
1092          (cal-tex-arg (int-to-string (extract-calendar-day date)))
1093          (cal-tex-arg (cal-tex-latexify-list diary-list date))
1094          (cal-tex-arg (cal-tex-latexify-list holidays date))
1095          (cal-tex-arg (eval cal-tex-daily-string))
1096          (insert "%\n")
1097          (setq date (cal-tex-incr-date date)))
1098       (if (/= i n)
1099           (progn
1100             (run-hooks 'cal-tex-week-hook)
1101             (cal-tex-newpage))))
1102    (cal-tex-end-document)
1103    (run-hooks 'cal-tex-hook)))
1104
1105(defun cal-tex-cursor-filofax-week (&optional arg)
1106  "One-week-at-a-glance Filofax style calendar for week indicated by cursor.
1107Optional prefix argument specifies number of weeks.
1108Weeks start on Monday.
1109Diary entries are included if `cal-tex-diary' is t.
1110Holidays are included if `cal-tex-holidays' is t."
1111  (interactive "p")
1112  (let* ((n (if arg arg 1))
1113         (date (calendar-gregorian-from-absolute
1114                (calendar-dayname-on-or-before
1115                 1
1116                 (calendar-absolute-from-gregorian
1117                  (calendar-cursor-to-date t)))))
1118         (month (extract-calendar-month date))
1119         (year (extract-calendar-year date))
1120         (day (extract-calendar-day date))
1121         (holidays (if cal-tex-holidays
1122                       (cal-tex-list-holidays
1123                        (calendar-absolute-from-gregorian date)
1124                        (+ (* 7 n)
1125                           (calendar-absolute-from-gregorian date)))))
1126         (diary-list (if cal-tex-diary
1127                         (cal-tex-list-diary-entries
1128                          (calendar-absolute-from-gregorian
1129                           (list month 1 year))
1130                        (+ (* 7 n)
1131                           (calendar-absolute-from-gregorian date))))))
1132    (cal-tex-preamble "twoside")
1133    (cal-tex-cmd "\\textwidth 3.25in")
1134    (cal-tex-cmd "\\textheight 6.5in")
1135    (cal-tex-cmd "\\oddsidemargin 1.75in")
1136    (cal-tex-cmd "\\evensidemargin 1.5in")
1137    (cal-tex-cmd "\\topmargin 0pt")
1138    (cal-tex-cmd "\\headheight -0.875in")
1139    (cal-tex-cmd "\\headsep 0.125in")
1140    (cal-tex-cmd "\\footskip .125in")
1141    (insert "\\def\\righthead#1{\\hfill {\\normalsize \\bf #1}\\\\[-6pt]}
1142\\long\\def\\rightday#1#2#3#4#5{%
1143   \\rule{\\textwidth}{0.3pt}\\\\%
1144   \\hbox to \\textwidth{%
1145     \\vbox to 1.85in{%
1146          \\vspace*{2pt}%
1147          \\hbox to \\textwidth{\\small #5 \\hfill #1 {\\normalsize \\bf #2}}%
1148          \\hbox to \\textwidth{\\vbox {\\raggedleft \\footnotesize \\em #4}}%
1149          \\hbox to \\textwidth{\\vbox to 0pt {\\noindent \\footnotesize #3}}}}\\\\}
1150\\long\\def\\weekend#1#2#3#4#5{%
1151   \\rule{\\textwidth}{0.3pt}\\\\%
1152   \\hbox to \\textwidth{%
1153     \\vbox to .8in{%
1154          \\vspace*{2pt}%
1155          \\hbox to \\textwidth{\\small #5 \\hfill #1 {\\normalsize \\bf #2}}%
1156          \\hbox to \\textwidth{\\vbox {\\raggedleft \\footnotesize \\em #4}}%
1157          \\hbox to \\textwidth{\\vbox to 0pt {\\noindent \\footnotesize #3}}}}\\\\}
1158\\def\\lefthead#1{\\noindent {\\normalsize \\bf #1}\\hfill\\\\[-6pt]}
1159\\long\\def\\leftday#1#2#3#4#5{%
1160   \\rule{\\textwidth}{0.3pt}\\\\%
1161   \\hbox to \\textwidth{%
1162     \\vbox to 1.85in{%
1163          \\vspace*{2pt}%
1164          \\hbox to \\textwidth{\\noindent {\\normalsize \\bf #2} \\small #1 \\hfill #5}%
1165          \\hbox to \\textwidth{\\vbox {\\noindent \\footnotesize \\em #4}}%
1166          \\hbox to \\textwidth{\\vbox to 0pt {\\noindent \\footnotesize #3}}}}\\\\}
1167")
1168    (cal-tex-b-document)
1169    (cal-tex-cmd "\\pagestyle{empty}\\ ")
1170    (cal-tex-newpage)
1171    (calendar-for-loop i from 1 to n do
1172       (insert "\\lefthead")
1173       (cal-tex-arg
1174        (let ((d (cal-tex-incr-date date 2)))
1175          (if (= (extract-calendar-month date)
1176                 (extract-calendar-month d))
1177              (format "%s %s"
1178                      (cal-tex-month-name
1179                       (extract-calendar-month date))
1180                      (extract-calendar-year date))
1181            (if (=  (extract-calendar-year date)
1182                    (extract-calendar-year d))
1183                (format "%s---%s %s"
1184                        (cal-tex-month-name
1185                         (extract-calendar-month date))
1186                        (cal-tex-month-name
1187                         (extract-calendar-month d))
1188                        (extract-calendar-year date))
1189              (format "%s %s---%s %s"
1190                      (cal-tex-month-name
1191                       (extract-calendar-month date))
1192                      (extract-calendar-year date)
1193                      (cal-tex-month-name (extract-calendar-month d))
1194                      (extract-calendar-year d))))))
1195       (insert "%\n")
1196       (calendar-for-loop j from 1 to 3 do
1197          (insert "\\leftday")
1198          (cal-tex-arg (cal-tex-LaTeXify-string (calendar-day-name date)))
1199          (cal-tex-arg (int-to-string (extract-calendar-day date)))
1200          (cal-tex-arg (cal-tex-latexify-list diary-list date))
1201          (cal-tex-arg (cal-tex-latexify-list holidays date))
1202          (cal-tex-arg (eval cal-tex-daily-string))
1203          (insert "%\n")
1204          (setq date (cal-tex-incr-date date)))
1205       (insert "\\noindent\\rule{\\textwidth}{0.3pt}\\\\%\n")
1206       (cal-tex-newpage)
1207       (insert "\\righthead")
1208       (cal-tex-arg
1209        (let ((d (cal-tex-incr-date date 3)))
1210          (if (= (extract-calendar-month date)
1211                 (extract-calendar-month d))
1212              (format "%s %s"
1213                      (cal-tex-month-name
1214                       (extract-calendar-month date))
1215                      (extract-calendar-year date))
1216            (if (=  (extract-calendar-year date)
1217                    (extract-calendar-year d))
1218                (format "%s---%s %s"
1219                        (cal-tex-month-name
1220                         (extract-calendar-month date))
1221                        (cal-tex-month-name
1222                         (extract-calendar-month d))
1223                        (extract-calendar-year date))
1224              (format "%s %s---%s %s"
1225                      (cal-tex-month-name
1226                       (extract-calendar-month date))
1227                      (extract-calendar-year date)
1228                      (cal-tex-month-name (extract-calendar-month d))
1229                      (extract-calendar-year d))))))
1230       (insert "%\n")
1231       (calendar-for-loop j from 1 to 2 do
1232          (insert "\\rightday")
1233          (cal-tex-arg (cal-tex-LaTeXify-string (calendar-day-name date)))
1234          (cal-tex-arg (int-to-string (extract-calendar-day date)))
1235          (cal-tex-arg (cal-tex-latexify-list diary-list date))
1236          (cal-tex-arg (cal-tex-latexify-list holidays date))
1237          (cal-tex-arg (eval cal-tex-daily-string))
1238          (insert "%\n")
1239          (setq date (cal-tex-incr-date date)))
1240       (calendar-for-loop j from 1 to 2 do
1241          (insert "\\weekend")
1242          (cal-tex-arg (cal-tex-LaTeXify-string (calendar-day-name date)))
1243          (cal-tex-arg (int-to-string (extract-calendar-day date)))
1244          (cal-tex-arg (cal-tex-latexify-list diary-list date))
1245          (cal-tex-arg (cal-tex-latexify-list holidays date))
1246          (cal-tex-arg (eval cal-tex-daily-string))
1247          (insert "%\n")
1248          (setq date (cal-tex-incr-date date)))
1249       (if (/= i n)
1250           (progn
1251             (run-hooks 'cal-tex-week-hook)
1252             (cal-tex-newpage))))
1253    (cal-tex-end-document)
1254    (run-hooks 'cal-tex-hook)))
1255
1256(defun cal-tex-cursor-filofax-daily (&optional arg)
1257  "Day-per-page Filofax style calendar for week indicated by cursor.
1258Optional prefix argument specifies number of weeks.  Weeks start on Monday.
1259Diary entries are included if `cal-tex-diary' is t.
1260Holidays are included if `cal-tex-holidays' is t.
1261Pages are ruled if `cal-tex-rules' is t."
1262  (interactive "p")
1263  (let* ((n (if arg arg 1))
1264         (date (calendar-gregorian-from-absolute
1265                (calendar-dayname-on-or-before
1266                 1
1267                 (calendar-absolute-from-gregorian
1268                  (calendar-cursor-to-date t)))))
1269         (month (extract-calendar-month date))
1270         (year (extract-calendar-year date))
1271         (day (extract-calendar-day date))
1272         (holidays (if cal-tex-holidays
1273                       (cal-tex-list-holidays
1274                        (calendar-absolute-from-gregorian date)
1275                        (+ (* 7 n)
1276                           (calendar-absolute-from-gregorian date)))))
1277         (diary-list (if cal-tex-diary
1278                         (cal-tex-list-diary-entries
1279                          (calendar-absolute-from-gregorian
1280                           (list month 1 year))
1281			  (+ (* 7 n)
1282			     (calendar-absolute-from-gregorian date))))))
1283    (cal-tex-preamble "twoside")
1284    (cal-tex-cmd "\\textwidth 3.25in")
1285    (cal-tex-cmd "\\textheight 6.5in")
1286    (cal-tex-cmd "\\oddsidemargin 1.75in")
1287    (cal-tex-cmd "\\evensidemargin 1.5in")
1288    (cal-tex-cmd "\\topmargin 0pt")
1289    (cal-tex-cmd "\\headheight -0.875in")
1290    (cal-tex-cmd "\\headsep 0.125in")
1291    (cal-tex-cmd "\\footskip .125in")
1292    (insert "\\def\\righthead#1{\\hfill {\\normalsize \\bf #1}\\\\[-6pt]}
1293\\long\\def\\rightday#1#2#3{%
1294   \\rule{\\textwidth}{0.3pt}\\\\%
1295   \\hbox to \\textwidth{%
1296     \\vbox {%
1297          \\vspace*{2pt}%
1298          \\hbox to \\textwidth{\\hfill \\small #3 \\hfill}%
1299          \\hbox to \\textwidth{\\vbox {\\raggedleft \\em #2}}%
1300          \\hbox to \\textwidth{\\vbox {\\noindent \\footnotesize #1}}}}}
1301\\long\\def\\weekend#1#2#3{%
1302   \\rule{\\textwidth}{0.3pt}\\\\%
1303   \\hbox to \\textwidth{%
1304     \\vbox {%
1305          \\vspace*{2pt}%
1306          \\hbox to \\textwidth{\\hfill \\small #3 \\hfill}%
1307          \\hbox to \\textwidth{\\vbox {\\noindent \\em #2}}%
1308          \\hbox to \\textwidth{\\vbox {\\noindent \\footnotesize #1}}}}}
1309\\def\\lefthead#1{\\noindent {\\normalsize \\bf #1}\\hfill\\\\[-6pt]}
1310\\long\\def\\leftday#1#2#3{%
1311   \\rule{\\textwidth}{0.3pt}\\\\%
1312   \\hbox to \\textwidth{%
1313     \\vbox {%
1314          \\vspace*{2pt}%
1315          \\hbox to \\textwidth{\\hfill \\small #3 \\hfill}%
1316          \\hbox to \\textwidth{\\vbox {\\noindent \\em #2}}%
1317          \\hbox to \\textwidth{\\vbox {\\noindent \\footnotesize #1}}}}}
1318\\newbox\\LineBox
1319\\setbox\\LineBox=\\hbox to\\textwidth{%
1320\\vrule height.2in width0pt\\leaders\\hrule\\hfill}
1321\\def\\linesfill{\\par\\leaders\\copy\\LineBox\\vfill}
1322")
1323    (cal-tex-b-document)
1324    (cal-tex-cmd "\\pagestyle{empty}")
1325    (calendar-for-loop i from 1 to n do
1326       (calendar-for-loop j from 1 to 5 do
1327	  (let ((odd (/= 0 (% j 2))))
1328	    (insert (if odd "\\righthead" "\\lefthead"))
1329	    (cal-tex-arg (calendar-date-string date))
1330	    (insert "%\n")
1331	    (insert (if odd "\\rightday"  "\\leftday")))
1332	  (cal-tex-arg (cal-tex-latexify-list diary-list date))
1333	  (cal-tex-arg (cal-tex-latexify-list holidays date "\\\\" t))
1334	  (cal-tex-arg (eval cal-tex-daily-string))
1335	  (insert "%\n")
1336	  (if cal-tex-rules
1337	      (insert "\\linesfill\n")
1338	    (insert "\\vfill\\noindent\\rule{\\textwidth}{0.3pt}\\\\%\n"))
1339	  (cal-tex-newpage)
1340	  (setq date (cal-tex-incr-date date)))
1341       (insert "%\n")
1342       (calendar-for-loop j from 1 to 2 do
1343	  (insert "\\lefthead")
1344          (cal-tex-arg (calendar-date-string date))
1345          (insert "\\weekend")
1346          (cal-tex-arg (cal-tex-latexify-list diary-list date))
1347          (cal-tex-arg (cal-tex-latexify-list holidays date "\\\\" t))
1348          (cal-tex-arg (eval cal-tex-daily-string))
1349          (insert "%\n")
1350          (if cal-tex-rules
1351              (insert "\\linesfill\n")
1352            (insert "\\vfill"))
1353          (setq date (cal-tex-incr-date date)))
1354       (if (not cal-tex-rules)
1355	   (insert "\\noindent\\rule{\\textwidth}{0.3pt}\\\\%\n"))
1356       (if (/= i n)
1357           (progn
1358             (run-hooks 'cal-tex-week-hook)
1359             (cal-tex-newpage))))
1360    (cal-tex-end-document)
1361    (run-hooks 'cal-tex-hook)))
1362
1363
1364;;;
1365;;;  Daily calendars
1366;;;
1367
1368(defun cal-tex-cursor-day (&optional arg)
1369  "Make a buffer with LaTeX commands for the day cursor is on.
1370Optional prefix argument specifies number of days."
1371  (interactive "p")
1372  (let ((n (if arg arg 1))
1373        (date (calendar-absolute-from-gregorian (calendar-cursor-to-date t))))
1374    (cal-tex-preamble "12pt")
1375    (cal-tex-cmd "\\textwidth       6.5in")
1376    (cal-tex-cmd "\\textheight 10.5in")
1377    (cal-tex-b-document)
1378    (cal-tex-cmd "\\pagestyle{empty}")
1379    (calendar-for-loop i from 1 to n do
1380       (cal-tex-vspace "-1.7in")
1381       (cal-tex-daily-page (calendar-gregorian-from-absolute date))
1382       (setq date (1+ date))
1383       (if (/= i n)
1384          (progn
1385            (cal-tex-newpage)
1386            (run-hooks 'cal-tex-daily-hook))))
1387    (cal-tex-end-document)
1388    (run-hooks 'cal-tex-hook)))
1389
1390(defun cal-tex-daily-page (date)
1391  "Make a calendar page for Gregorian DATE on 8.5 by 11 paper."
1392  (let* ((hour)
1393         (month-name (cal-tex-month-name (extract-calendar-month date))))
1394    (cal-tex-banner "cal-tex-daily-page")
1395    (cal-tex-b-makebox "4cm" "l")
1396    (cal-tex-b-parbox "b" "3.8cm")
1397    (cal-tex-rule "0mm" "0mm" "2cm")
1398    (cal-tex-Huge (number-to-string (extract-calendar-day date)))
1399    (cal-tex-nl ".5cm")
1400    (cal-tex-bf month-name )
1401    (cal-tex-e-parbox)
1402    (cal-tex-hspace "1cm")
1403    (cal-tex-scriptsize (eval cal-tex-daily-string))
1404    (cal-tex-hspace "3.5cm")
1405    (cal-tex-e-makebox)
1406    (cal-tex-hfill)
1407    (cal-tex-b-makebox "4cm" "r")
1408    (cal-tex-bf (cal-tex-LaTeXify-string (calendar-day-name date)))
1409    (cal-tex-e-makebox)
1410    (cal-tex-nl)
1411    (cal-tex-hspace ".4cm")
1412    (cal-tex-rule "0mm" "16.1cm" "1mm")
1413    (cal-tex-nl ".1cm")
1414    (calendar-for-loop i from cal-tex-daily-start to cal-tex-daily-end do
1415       (cal-tex-cmd "\\noindent")
1416       (setq hour (if cal-tex-24
1417                      i
1418                    (mod i 12)))
1419       (if (= 0 hour) (setq hour 12))
1420       (cal-tex-b-makebox "1cm" "c")
1421       (cal-tex-arg (number-to-string hour))
1422       (cal-tex-e-makebox)
1423       (cal-tex-rule "0mm" "15.5cm" ".2mm")
1424       (cal-tex-nl ".2cm")
1425       (cal-tex-b-makebox "1cm" "c")
1426       (cal-tex-arg "$\\diamond$" )
1427       (cal-tex-e-makebox)
1428       (cal-tex-rule "0mm" "15.5cm" ".2mm")
1429       (cal-tex-nl ".2cm"))
1430    (cal-tex-hfill)
1431    (insert (cal-tex-mini-calendar
1432             (extract-calendar-month (cal-tex-previous-month date))
1433             (extract-calendar-year (cal-tex-previous-month date))
1434             "lastmonth" "1.1in" "1in"))
1435    (insert (cal-tex-mini-calendar
1436             (extract-calendar-month date)
1437             (extract-calendar-year date)
1438             "thismonth" "1.1in" "1in"))
1439    (insert (cal-tex-mini-calendar
1440             (extract-calendar-month (cal-tex-next-month date))
1441             (extract-calendar-year (cal-tex-next-month date))
1442             "nextmonth" "1.1in" "1in"))
1443    (insert "\\hbox to \\textwidth{")
1444    (cal-tex-hfill)
1445    (insert "\\lastmonth")
1446    (cal-tex-hfill)
1447    (insert "\\thismonth")
1448    (cal-tex-hfill)
1449    (insert "\\nextmonth")
1450    (cal-tex-hfill)
1451    (insert "}")
1452    (cal-tex-banner "end of cal-tex-daily-page")))
1453
1454;;;
1455;;;  Mini calendars
1456;;;
1457
1458(defun cal-tex-mini-calendar (month year name width height &optional ptsize colsep)
1459  "Produce mini-calendar for MONTH, YEAR in macro NAME with WIDTH and HEIGHT.
1460Optional PTSIZE gives the point ptsize; scriptsize is the default.  Optional
1461COLSEP gives the column separation; 1mm is the default."
1462  (let* ((blank-days;; at start of month
1463          (mod
1464           (- (calendar-day-of-week (list month 1 year))
1465              calendar-week-start-day)
1466           7))
1467          (last (calendar-last-day-of-month month year))
1468         (colsep (if colsep colsep "1mm"))
1469         (str (concat "\\def\\" name "{\\hbox to" width "{%\n"
1470                      "\\vbox to" height "{%\n"
1471                      "\\vfil  \\hbox to" width "{%\n"
1472                      "\\hfil\\"
1473                      (if ptsize ptsize "scriptsize")
1474                      "\\begin{tabular}"
1475                      "{@{\\hspace{0mm}}r@{\\hspace{" colsep
1476                      "}}r@{\\hspace{" colsep "}}r@{\\hspace{" colsep
1477                      "}}r@{\\hspace{" colsep "}}r@{\\hspace{" colsep
1478                      "}}r@{\\hspace{" colsep "}}r@{\\hspace{0mm}}}%\n"
1479                      "\\multicolumn{7}{c}{"
1480                      (cal-tex-month-name month)
1481                      " "
1482                      (int-to-string year)
1483                      "}\\\\[1mm]\n")))
1484    (calendar-for-loop i from 0 to 6 do
1485      (setq str
1486            (concat str
1487                    (cal-tex-LaTeXify-string
1488                     (substring (aref calendar-day-name-array
1489                                      (mod (+ calendar-week-start-day i) 7))
1490                                0 2))
1491                    (if (/= i 6)
1492                        " & "
1493                      "\\\\[0.7mm]\n"))))
1494    (calendar-for-loop i from 1 to blank-days do
1495      (setq str (concat str " & ")))
1496    (calendar-for-loop i from 1 to last do
1497      (setq str (concat str (int-to-string i)))
1498      (setq str (concat str (if (zerop (mod (+ i blank-days) 7))
1499                                (if (/= i last) "\\\\[0.5mm]\n" "")
1500                              " & "))))
1501    (setq str (concat str "\n\\end{tabular}\\hfil}\\vfil}}}%\n"))
1502   str))
1503
1504;;;
1505;;;  Various calendar functions
1506;;;
1507
1508(defun cal-tex-incr-date (date &optional n)
1509  "The date of the day following DATE.
1510If optional N is given, the date of N days after DATE."
1511  (calendar-gregorian-from-absolute
1512   (+ (if n n 1) (calendar-absolute-from-gregorian date))))
1513
1514(defun cal-tex-latexify-list (date-list date &optional separator final-separator)
1515  "Return string with concatenated, LaTeXified entries in DATE-LIST for DATE.
1516Use double backslash as a separator unless optional SEPARATOR is given.
1517If resulting string is not empty, put separator at end if optional
1518FINAL-SEPARATOR is t."
1519  (let* ((sep (if separator separator "\\\\"))
1520         (result
1521          (mapconcat '(lambda (x) (cal-tex-LaTeXify-string  x))
1522                     (let ((result)
1523                           (p date-list))
1524                       (while p
1525                         (and (car (car p))
1526                              (calendar-date-equal date (car (car p)))
1527                              (setq result (cons (car (cdr (car p))) result)))
1528                         (setq p (cdr p)))
1529                       (reverse result))
1530                     sep)))
1531    (if (and final-separator (not (string-equal result "")))
1532          (concat result sep)
1533        result)))
1534
1535(defun cal-tex-previous-month (date)
1536  "Return the date of the first day in the month previous to DATE."
1537  (let* ((month (extract-calendar-month date))
1538         (year (extract-calendar-year date)))
1539    (increment-calendar-month month year -1)
1540    (list month 1 year)))
1541
1542(defun cal-tex-next-month (date)
1543  "Return the date of the first day in the  month following DATE."
1544  (let* ((month (extract-calendar-month date))
1545         (year (extract-calendar-year date)))
1546    (increment-calendar-month month year 1)
1547    (list month 1 year)))
1548
1549;;;
1550;;;  LaTeX Code
1551;;;
1552
1553(defun cal-tex-end-document ()
1554  "Finish the LaTeX document.
1555Insert the trailer to LaTeX document, pop to LaTeX buffer, add
1556informative header, and run HOOK."
1557  (cal-tex-e-document)
1558  (latex-mode)
1559  (pop-to-buffer cal-tex-buffer)
1560  (goto-char (point-min))
1561  (cal-tex-comment "       This buffer was produced by cal-tex.el.")
1562  (cal-tex-comment "       To print a calendar, type")
1563  (cal-tex-comment "          M-x tex-buffer RET")
1564  (cal-tex-comment "          M-x tex-print  RET")
1565  (goto-char (point-min)))
1566
1567(defun cal-tex-insert-preamble (weeks landscape size &optional append)
1568  "Initialize the output buffer.
1569Select the output buffer, and insert the preamble for a calendar of
1570WEEKS weeks.  Insert code for landscape mode if LANDSCAPE is true.
1571Use pointsize SIZE.  Optional argument APPEND, if t, means add to end of
1572without erasing current contents."
1573  (let ((width "18cm")
1574        (height "24cm"))
1575    (if landscape
1576        (progn
1577          (setq width "24cm")
1578          (setq height "18cm")))
1579    (if (not append)
1580        (progn
1581          (cal-tex-preamble size)
1582          (if (not landscape)
1583            (progn
1584              (cal-tex-cmd "\\oddsidemargin -1.75cm")
1585              (cal-tex-cmd "\\def\\holidaymult{.06}"))
1586            (cal-tex-cmd "\\special{landscape}")
1587            (cal-tex-cmd "\\textwidth  9.5in")
1588            (cal-tex-cmd "\\textheight 7in")
1589            (cal-tex-comment)
1590            (cal-tex-cmd "\\def\\holidaymult{.08}"))
1591          (cal-tex-cmd  cal-tex-caldate)
1592          (cal-tex-cmd  cal-tex-myday)
1593          (cal-tex-b-document)
1594          (cal-tex-cmd "\\pagestyle{empty}")))
1595    (cal-tex-cmd "\\setlength{\\cellwidth}" width)
1596    (insert (format "\\setlength{\\cellwidth}{%f\\cellwidth}\n"
1597                    (/ 1.1 (length cal-tex-which-days))))
1598    (cal-tex-cmd "\\setlength{\\cellheight}" height)
1599    (insert (format "\\setlength{\\cellheight}{%f\\cellheight}\n"
1600                    (/ 1.0 weeks)))
1601    (cal-tex-cmd "\\ \\par")
1602    (cal-tex-vspace "-3cm")))
1603
1604(defvar cal-tex-LaTeX-subst-list
1605  '(("\"". "``")
1606    ("\"". "''");; Quote changes meaning when list is reversed.
1607    ("@" . "\\verb|@|")
1608    ("&" . "\\&")
1609    ("%" . "\\%")
1610    ("$" . "\\$")
1611    ("#" . "\\#")
1612    ("_" . "\\_")
1613    ("{" . "\\{")
1614    ("}" . "\\}")
1615    ("<" . "$<$")
1616    (">" . "$>$")
1617    ("\n" . "\\ \\\\"))  ;\\ needed for e.g \begin{center}\n AA\end{center}
1618  "List of symbols and their replacements.")
1619
1620(defun cal-tex-LaTeXify-string (string)
1621  "Protect special characters in STRING from LaTeX."
1622  (if (not string)
1623      ""
1624    (let ((head "")
1625          (tail string)
1626          (list cal-tex-LaTeX-subst-list))
1627      (while (not (string-equal tail ""))
1628        (let* ((ch (substring tail 0 1))
1629               (pair (assoc ch list)))
1630          (if (and pair (string-equal ch "\""))
1631              (setq list (reverse list)));; Quote changes meaning each time.
1632          (setq tail (substring tail 1))
1633          (setq head (concat head (if pair (cdr pair) ch)))))
1634      head)))
1635
1636(defun cal-tex-month-name (month)
1637  "The name of MONTH, LaTeXified."
1638  (cal-tex-LaTeXify-string (calendar-month-name month)))
1639
1640(defun cal-tex-hfill () "Insert hfill." (insert "\\hfill"))
1641
1642(defun cal-tex-newpage () "Insert newpage." (insert "\\newpage%\n"))
1643
1644(defun cal-tex-noindent () "Insert noindent." (insert "\\noindent"))
1645
1646(defun cal-tex-vspace (space)
1647  "Insert vspace command to move SPACE vertically."
1648  (insert "\\vspace*{" space "}")
1649  (cal-tex-comment))
1650
1651(defun cal-tex-hspace (space)
1652  "Insert hspace command to move SPACE horizontally."
1653  (insert "\\hspace*{" space "}")
1654  (cal-tex-comment))
1655
1656(defun cal-tex-comment (&optional comment)
1657  "Insert % at end of line, include COMMENT if present, and move
1658   to next line."
1659  (insert "% ")
1660  (if comment
1661      (insert comment))
1662  (insert "\n"))
1663
1664(defun cal-tex-banner (comment)
1665  "Insert the COMMENT separated by blank lines."
1666  (cal-tex-comment)
1667  (cal-tex-comment)
1668  (cal-tex-comment (concat "\t\t\t" comment))
1669  (cal-tex-comment))
1670
1671
1672(defun cal-tex-nl (&optional skip comment)
1673  "End a line with \\.  If SKIP, then add that much spacing.
1674   Add COMMENT if present"
1675  (insert "\\\\")
1676  (if skip
1677      (insert "[" skip "]"))
1678  (cal-tex-comment comment))
1679
1680(defun cal-tex-arg (&optional text)
1681  "Insert optional TEXT surrounded by braces."
1682  (insert "{")
1683  (if text (insert text))
1684  (insert "}"))
1685
1686(defun cal-tex-cmd (cmd &optional arg)
1687  "Insert LaTeX CMD, with optional ARG, and end with %"
1688  (insert cmd)
1689  (cal-tex-arg arg)
1690  (cal-tex-comment))
1691
1692;;;
1693;;;   Environments
1694;;;
1695
1696(defun cal-tex-b-document ()
1697  "Insert beginning of document."
1698  (cal-tex-cmd "\\begin{document}"))
1699
1700(defun cal-tex-e-document ()
1701  "Insert end of document."
1702  (cal-tex-cmd "\\end{document}"))
1703
1704(defun cal-tex-b-center ()
1705  "Insert beginning of centered block."
1706  (cal-tex-cmd "\\begin{center}"))
1707
1708(defun cal-tex-e-center ()
1709  "Insert end of centered block."
1710  (cal-tex-comment)
1711  (cal-tex-cmd "\\end{center}"))
1712
1713
1714;;;
1715;;;  Boxes
1716;;;
1717
1718
1719(defun cal-tex-b-parbox (position width)
1720  "Insert parbox with parameters POSITION and WIDTH."
1721  (insert "\\parbox[" position "]{" width "}{")
1722  (cal-tex-comment))
1723
1724(defun cal-tex-e-parbox (&optional height)
1725  "Insert end of parbox. Force it to be a given HEIGHT."
1726  (cal-tex-comment)
1727  (if height
1728      (cal-tex-rule "0mm" "0mm" height))
1729  (insert "}")
1730  (cal-tex-comment "end parbox"))
1731
1732(defun cal-tex-b-framebox ( width position )
1733  "Insert  framebox with parameters WIDTH and POSITION (clr)."
1734  (insert "\\framebox[" width "][" position "]{" )
1735  (cal-tex-comment))
1736
1737(defun cal-tex-e-framebox ()
1738  "Insert end of framebox."
1739  (cal-tex-comment)
1740  (insert "}")
1741  (cal-tex-comment "end framebox"))
1742
1743
1744(defun cal-tex-b-makebox ( width position )
1745  "Insert  makebox with parameters WIDTH and POSITION (clr)."
1746  (insert "\\makebox[" width "][" position "]{" )
1747  (cal-tex-comment))
1748
1749(defun cal-tex-e-makebox ()
1750  "Insert end of makebox."
1751  (cal-tex-comment)
1752  (insert "}")
1753  (cal-tex-comment "end makebox"))
1754
1755
1756(defun cal-tex-rule (lower width height)
1757  "Insert a rule with parameters LOWER WIDTH HEIGHT."
1758  (insert "\\rule[" lower "]{" width "}{" height "}"))
1759
1760;;;
1761;;;     Fonts
1762;;;
1763
1764(defun cal-tex-em (string)
1765  "Insert STRING in bf font."
1766  (insert "{\\em " string "}"))
1767
1768(defun cal-tex-bf (string)
1769  "Insert STRING in bf font."
1770  (insert "{\\bf " string "}"))
1771
1772(defun cal-tex-scriptsize (string)
1773  "Insert STRING in scriptsize font."
1774  (insert "{\\scriptsize " string "}"))
1775
1776(defun cal-tex-huge (string)
1777  "Insert STRING in huge size."
1778  (insert "{\\huge " string "}"))
1779
1780(defun cal-tex-Huge (string)
1781  "Insert STRING in Huge size."
1782  (insert "{\\Huge " string "}"))
1783
1784(defun cal-tex-Huge-bf (string)
1785  "Insert STRING in Huge bf size."
1786  (insert "{\\Huge\\bf " string "}"))
1787
1788(defun cal-tex-large (string)
1789  "Insert STRING in large size."
1790  (insert "{\\large " string "}"))
1791
1792(defun cal-tex-large-bf (string)
1793  "Insert STRING in large bf size."
1794  (insert "{\\large\\bf  " string "}"))
1795
1796(provide 'cal-tex)
1797
1798;;; arch-tag: ca8168a4-5a00-4508-a565-17e3bccce6d0
1799;;; cal-tex.el ends here
1800