|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| package org.qedeq.kernel.latex; |
|
19 |
| |
|
20 |
| import java.io.IOException; |
|
21 |
| import java.text.SimpleDateFormat; |
|
22 |
| import java.util.Date; |
|
23 |
| import java.util.HashMap; |
|
24 |
| import java.util.Map; |
|
25 |
| |
|
26 |
| import org.qedeq.kernel.base.list.Element; |
|
27 |
| import org.qedeq.kernel.base.list.ElementList; |
|
28 |
| import org.qedeq.kernel.base.module.Author; |
|
29 |
| import org.qedeq.kernel.base.module.AuthorList; |
|
30 |
| import org.qedeq.kernel.base.module.Axiom; |
|
31 |
| import org.qedeq.kernel.base.module.Chapter; |
|
32 |
| import org.qedeq.kernel.base.module.FunctionDefinition; |
|
33 |
| import org.qedeq.kernel.base.module.Header; |
|
34 |
| import org.qedeq.kernel.base.module.Import; |
|
35 |
| import org.qedeq.kernel.base.module.ImportList; |
|
36 |
| import org.qedeq.kernel.base.module.Latex; |
|
37 |
| import org.qedeq.kernel.base.module.LatexList; |
|
38 |
| import org.qedeq.kernel.base.module.LinkList; |
|
39 |
| import org.qedeq.kernel.base.module.LiteratureItem; |
|
40 |
| import org.qedeq.kernel.base.module.LiteratureItemList; |
|
41 |
| import org.qedeq.kernel.base.module.LocationList; |
|
42 |
| import org.qedeq.kernel.base.module.Node; |
|
43 |
| import org.qedeq.kernel.base.module.PredicateDefinition; |
|
44 |
| import org.qedeq.kernel.base.module.Proof; |
|
45 |
| import org.qedeq.kernel.base.module.Proposition; |
|
46 |
| import org.qedeq.kernel.base.module.Qedeq; |
|
47 |
| import org.qedeq.kernel.base.module.Rule; |
|
48 |
| import org.qedeq.kernel.base.module.Section; |
|
49 |
| import org.qedeq.kernel.base.module.SectionList; |
|
50 |
| import org.qedeq.kernel.base.module.Specification; |
|
51 |
| import org.qedeq.kernel.base.module.Subsection; |
|
52 |
| import org.qedeq.kernel.base.module.UsedByList; |
|
53 |
| import org.qedeq.kernel.base.module.VariableList; |
|
54 |
| import org.qedeq.kernel.bo.module.ModuleDataException; |
|
55 |
| import org.qedeq.kernel.bo.module.QedeqBo; |
|
56 |
| import org.qedeq.kernel.bo.visitor.AbstractModuleVisitor; |
|
57 |
| import org.qedeq.kernel.bo.visitor.QedeqNotNullTransverser; |
|
58 |
| import org.qedeq.kernel.dto.module.PredicateDefinitionVo; |
|
59 |
| import org.qedeq.kernel.trace.Trace; |
|
60 |
| import org.qedeq.kernel.utility.IoUtility; |
|
61 |
| import org.qedeq.kernel.utility.ReplaceUtility; |
|
62 |
| import org.qedeq.kernel.utility.TextOutput; |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| public final class Qedeq2Latex extends AbstractModuleVisitor { |
|
80 |
| |
|
81 |
| |
|
82 |
| private final QedeqNotNullTransverser transverser; |
|
83 |
| |
|
84 |
| |
|
85 |
| private final TextOutput printer; |
|
86 |
| |
|
87 |
| |
|
88 |
| private final QedeqBo qedeq; |
|
89 |
| |
|
90 |
| |
|
91 |
| private final String language; |
|
92 |
| |
|
93 |
| |
|
94 |
| private final String level; |
|
95 |
| |
|
96 |
| |
|
97 |
| private final Map predicateDefinitions = new HashMap(); |
|
98 |
| |
|
99 |
| |
|
100 |
| private final Map functionDefinitions = new HashMap(); |
|
101 |
| |
|
102 |
| |
|
103 |
| private int chapterNumber; |
|
104 |
| |
|
105 |
| |
|
106 |
| private int sectionNumber; |
|
107 |
| |
|
108 |
| |
|
109 |
| private String id; |
|
110 |
| |
|
111 |
| |
|
112 |
| private String title; |
|
113 |
| |
|
114 |
| |
|
115 |
| private static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz"; |
|
116 |
| |
|
117 |
| |
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
| |
|
122 |
| |
|
123 |
| |
|
124 |
| |
|
125 |
| |
|
126 |
11
| private Qedeq2Latex(final QedeqBo qedeq, final String globalContext,
|
|
127 |
| final TextOutput printer, final String language, final String level) { |
|
128 |
11
| this.qedeq = qedeq;
|
|
129 |
11
| this.transverser = new QedeqNotNullTransverser(globalContext, this);
|
|
130 |
11
| this.printer = printer;
|
|
131 |
11
| if (language == null) {
|
|
132 |
0
| this.language = "en";
|
|
133 |
| } else { |
|
134 |
11
| this.language = language;
|
|
135 |
| } |
|
136 |
11
| if (level == null) {
|
|
137 |
0
| this.level = "1";
|
|
138 |
| } else { |
|
139 |
11
| this.level = level;
|
|
140 |
| } |
|
141 |
11
| final PredicateDefinitionVo equal = new PredicateDefinitionVo();
|
|
142 |
| |
|
143 |
11
| equal.setArgumentNumber("2");
|
|
144 |
11
| equal.setName("equal");
|
|
145 |
11
| equal.setLatexPattern("#1 \\ = \\ #2");
|
|
146 |
11
| predicateDefinitions.put("equal_2", equal);
|
|
147 |
| |
|
148 |
11
| final PredicateDefinitionVo notEqual = new PredicateDefinitionVo();
|
|
149 |
11
| notEqual.setArgumentNumber("2");
|
|
150 |
11
| notEqual.setName("notEqual");
|
|
151 |
11
| notEqual.setLatexPattern("#1 \\ \\neq \\ #2");
|
|
152 |
11
| predicateDefinitions.put("notEqual_2", notEqual);
|
|
153 |
| } |
|
154 |
| |
|
155 |
| |
|
156 |
| |
|
157 |
| |
|
158 |
| |
|
159 |
| |
|
160 |
| |
|
161 |
| |
|
162 |
| |
|
163 |
| |
|
164 |
| |
|
165 |
| |
|
166 |
11
| public static void print(final String globalContext, final QedeqBo qedeq,
|
|
167 |
| final TextOutput printer, final String language, final String level) |
|
168 |
| throws ModuleDataException, IOException { |
|
169 |
11
| final Qedeq2Latex converter = new Qedeq2Latex(qedeq, globalContext, printer,
|
|
170 |
| language, level); |
|
171 |
11
| converter.printLatex();
|
|
172 |
| } |
|
173 |
| |
|
174 |
| |
|
175 |
| |
|
176 |
| |
|
177 |
| |
|
178 |
| |
|
179 |
| |
|
180 |
| |
|
181 |
11
| private final void printLatex() throws IOException, ModuleDataException {
|
|
182 |
11
| transverser.accept(qedeq);
|
|
183 |
11
| printer.flush();
|
|
184 |
11
| if (printer.checkError()) {
|
|
185 |
0
| throw printer.getError();
|
|
186 |
| } |
|
187 |
| } |
|
188 |
| |
|
189 |
11
| public final void visitEnter(final Qedeq qedeq) {
|
|
190 |
11
| printer.println("% -*- TeX:" + language.toUpperCase() + " -*-");
|
|
191 |
11
| printer.println("%%% ====================================================================");
|
|
192 |
11
| printer.println("%%% @LaTeX-file " + printer.getName());
|
|
193 |
11
| printer.println("%%% Generated at " + getTimestamp());
|
|
194 |
11
| printer.println("%%% ====================================================================");
|
|
195 |
11
| printer.println();
|
|
196 |
11
| printer.println(
|
|
197 |
| "%%% Permission is granted to copy, distribute and/or modify this document"); |
|
198 |
11
| printer.println("%%% under the terms of the GNU Free Documentation License, Version 1.2");
|
|
199 |
11
| printer.println("%%% or any later version published by the Free Software Foundation;");
|
|
200 |
11
| printer.println(
|
|
201 |
| "%%% with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts."); |
|
202 |
11
| printer.println();
|
|
203 |
11
| printer.println("\\documentclass[a4paper,german,10pt,twoside]{book}");
|
|
204 |
11
| if ("de".equals(language)) {
|
|
205 |
5
| printer.println("\\usepackage[german]{babel}");
|
|
206 |
| } else { |
|
207 |
6
| if (!"en".equals(language)) {
|
|
208 |
0
| printer.println("%%% TODO unknown language: " + language);
|
|
209 |
| } |
|
210 |
6
| printer.println("\\usepackage[english]{babel}");
|
|
211 |
| } |
|
212 |
11
| printer.println("\\usepackage{makeidx}");
|
|
213 |
11
| printer.println("\\usepackage{amsmath,amsthm,amssymb}");
|
|
214 |
11
| printer.println("\\usepackage{color}");
|
|
215 |
11
| printer.println("\\usepackage[bookmarks,bookmarksnumbered,bookmarksopen,");
|
|
216 |
11
| printer.println(" colorlinks,linkcolor=webgreen,pagebackref]{hyperref}");
|
|
217 |
11
| printer.println("\\definecolor{webgreen}{rgb}{0,.5,0}");
|
|
218 |
11
| printer.println("\\usepackage{graphicx}");
|
|
219 |
11
| printer.println("\\usepackage{xr}");
|
|
220 |
11
| printer.println("\\usepackage{epsfig,longtable}");
|
|
221 |
11
| printer.println("\\usepackage{tabularx}");
|
|
222 |
11
| printer.println();
|
|
223 |
11
| if ("de".equals(language)) {
|
|
224 |
5
| printer.println("\\newtheorem{thm}{Theorem}[chapter]");
|
|
225 |
5
| printer.println("\\newtheorem{cor}[thm]{Korollar}");
|
|
226 |
5
| printer.println("\\newtheorem{lem}[thm]{Lemma}");
|
|
227 |
5
| printer.println("\\newtheorem{prop}[thm]{Proposition}");
|
|
228 |
5
| printer.println("\\newtheorem{ax}{Axiom}");
|
|
229 |
5
| printer.println("\\newtheorem{rul}{Regel}");
|
|
230 |
5
| printer.println();
|
|
231 |
5
| printer.println("\\theoremstyle{definition}");
|
|
232 |
5
| printer.println("\\newtheorem{defn}[thm]{Definition}");
|
|
233 |
5
| printer.println("\\newtheorem{idefn}[thm]{Initiale Definition}");
|
|
234 |
5
| printer.println();
|
|
235 |
5
| printer.println("\\theoremstyle{remark}");
|
|
236 |
5
| printer.println("\\newtheorem{rem}[thm]{Bemerkung}");
|
|
237 |
5
| printer.println("\\newtheorem*{notation}{Notation}");
|
|
238 |
| } else { |
|
239 |
6
| if (!"en".equals(language)) {
|
|
240 |
0
| printer.println("%%% TODO unknown language: " + language);
|
|
241 |
| } |
|
242 |
6
| printer.println("\\newtheorem{thm}{Theorem}[chapter]");
|
|
243 |
6
| printer.println("\\newtheorem{cor}[thm]{Corollary}");
|
|
244 |
6
| printer.println("\\newtheorem{lem}[thm]{Lemma}");
|
|
245 |
6
| printer.println("\\newtheorem{prop}[thm]{Proposition}");
|
|
246 |
6
| printer.println("\\newtheorem{ax}{Axiom}");
|
|
247 |
6
| printer.println("\\newtheorem{rul}{Rule}");
|
|
248 |
6
| printer.println();
|
|
249 |
6
| printer.println("\\theoremstyle{definition}");
|
|
250 |
6
| printer.println("\\newtheorem{defn}[thm]{Definition}");
|
|
251 |
6
| printer.println("\\newtheorem{idefn}[thm]{Initial Definition}");
|
|
252 |
6
| printer.println();
|
|
253 |
6
| printer.println("\\theoremstyle{remark}");
|
|
254 |
6
| printer.println("\\newtheorem{rem}[thm]{Remark}");
|
|
255 |
6
| printer.println("\\newtheorem*{notation}{Notation}");
|
|
256 |
| } |
|
257 |
11
| printer.println();
|
|
258 |
11
| printer.println("\\addtolength{\\textheight}{7\\baselineskip}");
|
|
259 |
11
| printer.println("\\addtolength{\\topmargin}{-5\\baselineskip}");
|
|
260 |
11
| printer.println();
|
|
261 |
11
| printer.println("\\setlength{\\parindent}{0pt}");
|
|
262 |
11
| printer.println();
|
|
263 |
11
| printer.println("\\frenchspacing \\sloppy");
|
|
264 |
11
| printer.println();
|
|
265 |
11
| printer.println("\\makeindex");
|
|
266 |
11
| printer.println();
|
|
267 |
11
| printer.println();
|
|
268 |
| } |
|
269 |
| |
|
270 |
11
| public final void visitLeave(final Qedeq qedeq) {
|
|
271 |
11
| printer.println("\\backmatter");
|
|
272 |
11
| printer.println();
|
|
273 |
11
| printer.println("\\addcontentsline{toc}{chapter}{\\indexname} \\printindex");
|
|
274 |
11
| printer.println();
|
|
275 |
11
| printer.println("\\end{document}");
|
|
276 |
11
| printer.println();
|
|
277 |
| } |
|
278 |
| |
|
279 |
11
| public void visitEnter(final Header header) {
|
|
280 |
11
| final LatexList title = header.getTitle();
|
|
281 |
11
| printer.print("\\title{");
|
|
282 |
11
| printer.print(getLatexListEntry(title));
|
|
283 |
11
| printer.println("}");
|
|
284 |
11
| printer.println("\\author{");
|
|
285 |
11
| final AuthorList authors = qedeq.getHeader().getAuthorList();
|
|
286 |
11
| for (int i = 0; i < authors.size(); i++) {
|
|
287 |
11
| if (i > 0) {
|
|
288 |
0
| printer.println(", ");
|
|
289 |
| } |
|
290 |
11
| final Author author = authors.get(i);
|
|
291 |
11
| printer.print(author.getName().getLatex());
|
|
292 |
| |
|
293 |
| } |
|
294 |
11
| printer.println();
|
|
295 |
11
| printer.println("}");
|
|
296 |
11
| printer.println();
|
|
297 |
11
| printer.println("\\begin{document}");
|
|
298 |
11
| printer.println();
|
|
299 |
11
| printer.println("\\maketitle");
|
|
300 |
11
| printer.println();
|
|
301 |
11
| printer.println("\\setlength{\\parskip}{5pt plus 2pt minus 1pt}");
|
|
302 |
11
| printer.println("\\mbox{}");
|
|
303 |
11
| printer.println("\\vfill");
|
|
304 |
11
| printer.println();
|
|
305 |
11
| final String url = getUrl(header.getSpecification());
|
|
306 |
11
| if (url != null && url.length() > 0) {
|
|
307 |
11
| printer.println("\\par");
|
|
308 |
11
| if ("de".equals(language)) {
|
|
309 |
5
| printer.println("Die Quelle f{\"ur} dieses Dokument ist hier zu finden:");
|
|
310 |
| } else { |
|
311 |
6
| if (!"en".equals(language)) {
|
|
312 |
0
| printer.println("%%% TODO unknown language: " + language);
|
|
313 |
| } |
|
314 |
6
| printer.println("The source for this document can be found here:");
|
|
315 |
| } |
|
316 |
11
| printer.println("\\par");
|
|
317 |
11
| printer.println("\\url{" + getUrl(header.getSpecification()) + "}");
|
|
318 |
11
| printer.println();
|
|
319 |
| } |
|
320 |
| { |
|
321 |
11
| printer.println("\\par");
|
|
322 |
11
| if ("de".equals(language)) {
|
|
323 |
5
| printer.println("Die vorliegende Publikation ist urheberrechtlich gesch{\"u}tzt.");
|
|
324 |
| } else { |
|
325 |
6
| if (!"en".equals(language)) {
|
|
326 |
0
| printer.println("%%% TODO unknown language: " + language);
|
|
327 |
| } |
|
328 |
6
| printer.println("Copyright by the authors. All rights reserved.");
|
|
329 |
| } |
|
330 |
| } |
|
331 |
11
| final String email = header.getEmail();
|
|
332 |
11
| if (email != null && email.length() > 0) {
|
|
333 |
11
| final String emailUrl = "\\url{mailto:" + email + "}";
|
|
334 |
11
| printer.println("\\par");
|
|
335 |
11
| if ("de".equals(language)) {
|
|
336 |
5
| printer.println("Bei Fragen, Anregungen oder Bitte um Aufnahme in die Liste der"
|
|
337 |
| + " abh{\"a}ngigen Module schicken Sie bitte eine EMail an die Addresse " |
|
338 |
| + emailUrl); |
|
339 |
| } else { |
|
340 |
6
| if (!"en".equals(language)) {
|
|
341 |
0
| printer.println("%%% TODO unknown language: " + language);
|
|
342 |
| } |
|
343 |
6
| printer.println("If you have any questions, suggestions or want to add something"
|
|
344 |
| + " to the list of modules that use this one, please send an email to the" |
|
345 |
| + " address " + emailUrl); |
|
346 |
| } |
|
347 |
11
| printer.println();
|
|
348 |
| } |
|
349 |
11
| printer.println("\\setlength{\\parskip}{0pt}");
|
|
350 |
11
| printer.println("\\tableofcontents");
|
|
351 |
11
| printer.println();
|
|
352 |
11
| printer.println("\\setlength{\\parskip}{5pt plus 2pt minus 1pt}");
|
|
353 |
11
| printer.println();
|
|
354 |
| } |
|
355 |
| |
|
356 |
| |
|
357 |
| |
|
358 |
| |
|
359 |
| |
|
360 |
| |
|
361 |
| |
|
362 |
24
| private String getUrl(final Specification specification) {
|
|
363 |
24
| final LocationList list = specification.getLocationList();
|
|
364 |
24
| if (list == null || list.size() <= 0
|
|
365 |
| || list.get(0).getLocation().length() <= "http://a.b".length()) { |
|
366 |
2
| return "";
|
|
367 |
| } |
|
368 |
22
| String location = list.get(0).getLocation();
|
|
369 |
22
| if (!location.endsWith("/")) {
|
|
370 |
22
| location += "/";
|
|
371 |
| } |
|
372 |
22
| location += specification.getName();
|
|
373 |
22
| location += ".xml";
|
|
374 |
22
| return location;
|
|
375 |
| } |
|
376 |
| |
|
377 |
62
| public void visitEnter(final Chapter chapter) {
|
|
378 |
62
| printer.print("\\chapter");
|
|
379 |
62
| if (chapter.getNoNumber() != null && chapter.getNoNumber().booleanValue()) {
|
|
380 |
18
| printer.print("*");
|
|
381 |
| } |
|
382 |
62
| printer.print("{");
|
|
383 |
62
| printer.print(getLatexListEntry(chapter.getTitle()));
|
|
384 |
62
| final String label = "chapter" + chapterNumber;
|
|
385 |
62
| printer.println("} \\label{" + label + "} \\hypertarget{" + label + "}{}");
|
|
386 |
62
| if (chapter.getNoNumber() != null && chapter.getNoNumber().booleanValue()) {
|
|
387 |
18
| printer.println("\\addcontentsline{toc}{chapter}{"
|
|
388 |
| + getLatexListEntry(chapter.getTitle()) + "}"); |
|
389 |
| } |
|
390 |
62
| printer.println();
|
|
391 |
62
| if (chapter.getIntroduction() != null) {
|
|
392 |
62
| printer.println(getLatexListEntry(chapter.getIntroduction()));
|
|
393 |
62
| printer.println();
|
|
394 |
| } |
|
395 |
| } |
|
396 |
| |
|
397 |
62
| public void visitLeave(final Chapter chapter) {
|
|
398 |
62
| printer.println("%% end of chapter " + getLatexListEntry(chapter.getTitle()));
|
|
399 |
62
| printer.println();
|
|
400 |
62
| chapterNumber++;
|
|
401 |
62
| sectionNumber = 0;
|
|
402 |
| } |
|
403 |
| |
|
404 |
37
| public void visitLeave(final SectionList list) {
|
|
405 |
37
| printer.println();
|
|
406 |
| } |
|
407 |
| |
|
408 |
116
| public void visitEnter(final Section section) {
|
|
409 |
| |
|
410 |
| |
|
411 |
| |
|
412 |
| |
|
413 |
| |
|
414 |
116
| printer.print("\\section{");
|
|
415 |
116
| printer.print(getLatexListEntry(section.getTitle()));
|
|
416 |
116
| final String label = "chapter" + chapterNumber + "_section" + sectionNumber;
|
|
417 |
116
| printer.println("} \\label{" + label + "} \\hypertarget{" + label + "}{}");
|
|
418 |
116
| printer.println(getLatexListEntry(section.getIntroduction()));
|
|
419 |
116
| printer.println();
|
|
420 |
| } |
|
421 |
| |
|
422 |
116
| public void visitLeave(final Section section) {
|
|
423 |
116
| sectionNumber++;
|
|
424 |
| } |
|
425 |
| |
|
426 |
47
| public void visitEnter(final Subsection subsection) {
|
|
427 |
| |
|
428 |
| |
|
429 |
| |
|
430 |
| |
|
431 |
| |
|
432 |
|