summaryrefslogtreecommitdiff
path: root/info/digests/texhax/txh/martensson.txh
blob: 8a7bb77b39030a18173cc590d5bb331bf50532e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
 4-Mar-88 04:21:48-PST,10031;000000000001
Return-Path: <unido!ubrinf!mond!bengt@uunet.UU.NET>
Date: Thu, 3 Mar 88 15:49:50 PST
From: unido!ubrinf!mond!bengt@uunet.UU.NET (Bengt Martensson)
Subject: Re: delatex 

Delatex etc is frequently discussed in this forum.  Last summer I
wrote some GNU Emacs functions for this.  It does a more complete job
than other approaches I have seen, and it is customizable.  It can
both be used as is (although it is slightly--but not
prohibitively--slow of course), but in particular I would like to
regard it as a part of the discussion.  Among its features is that all
environments in a user modifiable list are deleted.  A selected number
of commands (defined in another user definable list) will get their
arguments junked.  It is easily adaptable to other TeX-dialects, using
all kinds of weird characters for escape and grouping.

I include the file in this mailing, although I suspect that the
moderator will find it to large and file it at a ftp-able location
instead.

Bengt Martensson					  +49 421 218-2948
Institut fur Dynamische Systeme, Universitat Bremen 	  +49 421 171713 (home)
Postfach 330 440, D-2800 Bremen 33
F.R.G.	    				       UUCP: ...unido!ubrinf!mond!bengt


;;; Detex
;;; Bengt Martensson
;;; LastEditDate="Sun Oct  4 17:54:06 1987"
     
;;; For redistribution the same rules apply as for GNU Emacs.

(defvar detex-tex-commands-with-garbage-argument
  '("input" "special" "message")
  "List of TeX-commands for which their argument should be filtered out
by detex.")

(defvar detex-latex-commands-with-garbage-argument
  (append detex-tex-commands-with-garbage-argument
	  '("documentstyle" "pagestyle" "thispagestyle" 
	    "pagenumbering" "typeout" "typein" "label" "ref" "pageref"
	    "bibliography" "bibitem" "bibliographystyle"
	    "cite" "include" "includeonly" "begin" "end"))
    "List of LaTeX-commands for which their argument should be filtered out
by delatex.")

(defvar detex-latex-garbage-environments
  '("array" "equation" "eqnarray" "eqnarray*" "verbatim" "displaymath")
  "List of LaTeX environments which will be deleted by delatex.")

(defvar detex-tex-escape "" "TeX escape character used by detex.")
(defvar detex-re-tex-escape ""
  "Regular expression for TeX escape character.  Used by detex.")
(defvar detex-not-tex-escape ""
  "Regular expression that matches anything but TeX escape character.
Used by detex.")
(defvar detex-latex-optional-left ""
  "LaTeX character denoting left bracket for optional argument.
Used by detex.")
(defvar detex-tex-left "" "TeX left group character used by detex.")
(defvar detex-tex-right "" "TeX right group character used by detex.")

(make-variable-buffer-local 'detex-tex-escape)
(make-variable-buffer-local 'detex-re-tex-escape)
(make-variable-buffer-local 'detex-not-tex-escape)
(make-variable-buffer-local 'detex-latex-optional-left)
(make-variable-buffer-local 'detex-tex-left)
(make-variable-buffer-local 'detex-tex-right)

(set-default 'detex-tex-escape "\\")
(set-default 'detex-re-tex-escape "\\\\")
(set-default 'detex-not-escape "[^\\\\]")
(set-default 'detex-latex-optional-left "\\[")
(set-default 'detex-tex-left "{")
(set-default 'detex-tex-right "}")

(defun detex-remove-latex-commands-with-garbage-argument ()
  "Deletes latex-commands in detex-latex-commands-with-garbage-argument
together with their arguments."
  ;;(message "detex-remove-latex-commands-with-garbage-argument...")
  (mapcar 'detex-remove-latex-command-with-garbage-argument
	  detex-latex-commands-with-garbage-argument))

(defun detex-remove-tex-commands-with-garbage-argument ()
  "Deletes tex-commands in detex-tex-commands-with-garbage-argument
together with their arguments."
  (mapcar 'detex-remove-tex-command-with-garbage-argument
	  detex-tex-commands-with-garbage-argument)) 

(defun detex-remove-latex-command-with-garbage-argument (com)
"Deletes latex-command COM together with its arguments."
  (let ((command (concat detex-tex-escape com))
	(re-command (concat detex-re-tex-escape com "\\b"))
	b)
    (goto-char (point-min))
    (while (re-search-forward re-command (point-max) t)
      (search-backward command)
      (setq b (point))
      (search-forward command)
      (re-search-forward "[ \t]*")
      (if (looking-at detex-latex-optional-left)
	  (forward-sexp 1))
      (re-search-forward "[ \t]*")
      (if (looking-at detex-tex-left)
	  (forward-sexp 1)
	(forward-word 1))
      (delete-region b (point)))))

(defun detex-remove-tex-command-with-garbage-argument (com)
"Deletes tex-command COM together with its arguments."
  (let ((command (concat detex-tex-escape com))
	(re-command (concat detex-re-tex-escape com "\\b"))
	b)
    (goto-char (point-min))
    (while (re-search-forward re-command (point-max) t)
      (search-backward command)
      (setq b (point))
      (search-forward command)
      (re-search-forward "[ \t]*")
      (if (looking-at detex-tex-left)
	  (forward-sexp 1)
	(forward-word 1))
      (delete-region b (point)))))

(defun detex-remove-latex-garbage-environments ()
  "Deletes the latex environments in detex-latex-garbage-environments."
  ;;(message "detex-remove-latex-garbage-environments")
  (mapcar 'detex-remove-latex-environment detex-latex-garbage-environments))

(defun detex-remove-latex-environment (env)
  "Deletes latex environment ENV."
  (goto-char (point-min))
  (while (search-forward
	  (concat detex-tex-escape "begin" detex-tex-left env) (point-max) t)
    (search-backward detex-tex-escape)
    (setq b (point))
    (if (search-forward
	 (concat detex-tex-escape "end" detex-tex-left env) (point-max) t)
	(if (looking-at detex-tex-right)
	    (delete-region b (1+ (point)))))))
  
(defun detex-remove-discretionary-hyphens ()
  "Removes \\-."
  ;;(message "detex-remove-hyphens...")
  (goto-char (point-min))
  (while (re-search-forward (concat detex-re-tex-escape "-") (point-max) t)
    (replace-match "")))

(defun detex-remove-display (&optional flavor)
  "Deletes displays delimited by ""$$"".  If optional argument FLAVOR is 
""latex"", then it also recognizes LaTeX displays."
  (let (beg)
    (goto-char (point-min))
    ;;(message "detex-remove-display...")
    (while (re-search-forward (concat detex-not-escape "\\$\\$") (point-max) t)
      (setq beg (- (point) 2))
      (if (re-search-forward (concat detex-not-escape "\\$\\$") (point-max) t)
	  (delete-region beg (point))))
    (if (equal flavor "latex")
	(progn
	  (goto-char (point-min))
	  (while (search-forward (concat detex-tex-escape "[") (point-max) t)
	    (setq beg (- (point) 2))
	    (if (search-forward (concat detex-tex-escape "]") (point-max) t)
		(delete-region beg (point))))))))

(defun detex-remove-math (&optional flavor)
  "Deletes math mode.  If optional argument FLAVOR is ""latex"", it also
recognizes LaTeX constructions."
  (let (beg)
    ;;(message "detex-remove-math...")
    (goto-char (point-min))
    (while (re-search-forward (concat detex-not-escape "\\$") (point-max) t)
      (setq beg (1- (point)))
      (if (re-search-forward (concat detex-not-escape "\\$") (point-max) t)
	  (delete-region beg (point))))
    (if (equal flavor "latex")
	(progn
	  (goto-char (point-min))
	  (while (search-forward (concat detex-tex-escape "(") (point-max) t)
	    (setq beg (- (point) 2))
	    (if (search-forward (concat detex-tex-escape ")") (point-max) t)
		(delete-region beg (point))))))))

(defun detex-remove-tex-commands ()
  ;;(message "detex-remove-tex-commands")
  (goto-char (point-min))
  (while (re-search-forward
	  (concat detex-re-tex-escape "[a-zA-Z]*") (point-max) t)
    (replace-match "")))
     
(defun detex-remove-comments ()
  ;;(message "detex-remove-comments...")
  (goto-char (point-min))
  (while (re-search-forward (concat detex-not-escape "%.*$") (point-max) t)
    (delete-region (1+ (match-beginning 0)) (match-end 0))))
 
(defun detex-remove-font ()
  (goto-char (point-min))
  (while (re-search-forward (concat detex-re-tex-escape "font[^=]* *= *\\w*")
			    (point-max) t)
    (replace-match "")))

(defun detex-remove-double-single-quotes ()
  (goto-char (point-min))
  (replace-string "''" "" nil))
			    
;;; If using a TeX/LaTeX dialect which uses other characters for 
;;; escape and grouping, this function should be rewritten to
;;; redefine TeX escape character etc, possibly mode-dependent.

(defun detex-regler ()
  (interactive)
  (setq detex-tex-escape "!")
  (setq detex-re-tex-escape "!")
  (setq detex-not-escape "[^!]")
  (setq detex-tex-left "<")
  (setq detex-tex-right ">")
  (detex))
  
(defun delatex ()
  (interactive)
  ;;(define-escape-and-group)
  (detex-remove-latex-garbage-environments)
  (detex-remove-display "latex")
  (detex-remove-math "latex")
  (detex-remove-comments)
  (detex-remove-latex-commands-with-garbage-argument)
  (detex-remove-discretionary-hyphens)
  (detex-remove-double-single-quotes)
  (detex-remove-tex-commands))

(defun detex ()
  (interactive)
  ;;(define-escape-and-group)
  (detex-remove-display "tex")
  (detex-remove-math "tex")
  (detex-remove-comments)
  (detex-remove-font)
  (detex-remove-tex-commands-with-garbage-argument)
  (detex-remove-discretionary-hyphens)
  (detex-remove-double-single-quotes)
  (detex-remove-tex-commands))

-------