|
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.io.OutputStream; |
|
22 |
| import java.io.PrintStream; |
|
23 |
| import java.text.SimpleDateFormat; |
|
24 |
| import java.util.Date; |
|
25 |
| import java.util.HashMap; |
|
26 |
| import java.util.Map; |
|
27 |
| |
|
28 |
| import org.qedeq.kernel.base.list.Element; |
|
29 |
| import org.qedeq.kernel.base.list.ElementList; |
|
30 |
| import org.qedeq.kernel.base.module.Author; |
|
31 |
| import org.qedeq.kernel.base.module.AuthorList; |
|
32 |
| import org.qedeq.kernel.base.module.Axiom; |
|
33 |
| import org.qedeq.kernel.base.module.Chapter; |
|
34 |
| import org.qedeq.kernel.base.module.ChapterList; |
|
35 |
| import org.qedeq.kernel.base.module.FunctionDefinition; |
|
36 |
| import org.qedeq.kernel.base.module.Latex; |
|
37 |
| import org.qedeq.kernel.base.module.LatexList; |
|
38 |
| import org.qedeq.kernel.base.module.LiteratureItem; |
|
39 |
| import org.qedeq.kernel.base.module.LiteratureItemList; |
|
40 |
| import org.qedeq.kernel.base.module.Node; |
|
41 |
| import org.qedeq.kernel.base.module.NodeType; |
|
42 |
| import org.qedeq.kernel.base.module.PredicateDefinition; |
|
43 |
| import org.qedeq.kernel.base.module.Proposition; |
|
44 |
| import org.qedeq.kernel.base.module.Qedeq; |
|
45 |
| import org.qedeq.kernel.base.module.Rule; |
|
46 |
| import org.qedeq.kernel.base.module.Section; |
|
47 |
| import org.qedeq.kernel.base.module.SectionList; |
|
48 |
| import org.qedeq.kernel.base.module.Subsection; |
|
49 |
| import org.qedeq.kernel.base.module.SubsectionList; |
|
50 |
| import org.qedeq.kernel.base.module.SubsectionType; |
|
51 |
| import org.qedeq.kernel.base.module.VariableList; |
|
52 |
| import org.qedeq.kernel.bo.control.QedeqBoFactory; |
|
53 |
| import org.qedeq.kernel.bo.module.ModuleDataException; |
|
54 |
| import org.qedeq.kernel.dto.module.PredicateDefinitionVo; |
|
55 |
| import org.qedeq.kernel.log.Trace; |
|
56 |
| import org.qedeq.kernel.utility.IoUtility; |
|
57 |
| import org.qedeq.kernel.utility.ReplaceUtility; |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| public final class Qedeq2LatexOld { |
|
77 |
| |
|
78 |
| |
|
79 |
| private static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz"; |
|
80 |
| |
|
81 |
| |
|
82 |
| private PrintStream printer; |
|
83 |
| |
|
84 |
| |
|
85 |
| private Qedeq qedeq; |
|
86 |
| |
|
87 |
| |
|
88 |
| private String language; |
|
89 |
| |
|
90 |
| |
|
91 |
| private Object level; |
|
92 |
| |
|
93 |
| |
|
94 |
| private Map definitions = new HashMap(); |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
| |
|
103 |
0
| public Qedeq2LatexOld(final String context, final Qedeq qedeq) throws ModuleDataException {
|
|
104 |
| |
|
105 |
0
| this.qedeq = QedeqBoFactory.createQedeq(context, qedeq);
|
|
106 |
0
| final PredicateDefinitionVo equal = new PredicateDefinitionVo();
|
|
107 |
| |
|
108 |
0
| equal.setArgumentNumber("2");
|
|
109 |
0
| equal.setName("equal");
|
|
110 |
0
| equal.setLatexPattern("#1 \\ = \\ #2");
|
|
111 |
0
| definitions.put("equal_2", equal);
|
|
112 |
| |
|
113 |
0
| final PredicateDefinitionVo notEqual = new PredicateDefinitionVo();
|
|
114 |
0
| notEqual.setArgumentNumber("2");
|
|
115 |
0
| notEqual.setName("notEqual");
|
|
116 |
0
| notEqual.setLatexPattern("#1 \\ \\neq \\ #2");
|
|
117 |
0
| definitions.put("notEqual_2", notEqual);
|
|
118 |
| } |
|
119 |
| |
|
120 |
| |
|
121 |
| |
|
122 |
| |
|
123 |
| |
|
124 |
| |
|
125 |
| |
|
126 |
| |
|
127 |
| |
|
128 |
0
| public final synchronized void printLatex(final String language, final String level,
|
|
129 |
| final OutputStream outputStream) throws IOException { |
|
130 |
0
| if (language == null) {
|
|
131 |
0
| this.language = "en";
|
|
132 |
| } else { |
|
133 |
0
| this.language = language;
|
|
134 |
| } |
|
135 |
0
| if (level == null) {
|
|
136 |
0
| this.level = "1";
|
|
137 |
| } else { |
|
138 |
0
| this.level = level;
|
|
139 |
| } |
|
140 |
0
| this.printer = new PrintStream(outputStream);
|
|
141 |
0
| printLatexHeader();
|
|
142 |
0
| printQedeqHeader();
|
|
143 |
0
| printQedeqChapters();
|
|
144 |
0
| printQedeqBibliography();
|
|
145 |
0
| printLatexTrailer();
|
|
146 |
0
| if (printer.checkError()) {
|
|
147 |
0
| throw new IOException("TODO mime: better use another OutputStream");
|
|
148 |
| } |
|
149 |
| } |
|
150 |
| |
|
151 |
| |
|
152 |
| |
|
153 |
| |
|
154 |
0
| private void printQedeqHeader() {
|
|
155 |
0
| final LatexList title = qedeq.getHeader().getTitle();
|
|
156 |
0
| printer.print("\\title{");
|
|
157 |
0
| printer.print(getLatexListEntry(title));
|
|
158 |
0
| printer.println("}");
|
|
159 |
0
| printer.println("\\author{");
|
|
160 |
0
| final AuthorList authors = qedeq.getHeader().getAuthorList();
|
|
161 |
0
| for (int i = 0; i < authors.size(); i++) {
|
|
162 |
0
| if (i > 0) {
|
|
163 |
0
| printer.println(", ");
|
|
164 |
| } |
|
165 |
0
| final Author author = authors.get(i);
|
|
166 |
0
| printer.print(author.getName().getLatex());
|
|
167 |
| } |
|
168 |
0
| printer.println();
|
|
169 |
0
| printer.println("}");
|
|
170 |
0
| printer.println();
|
|
171 |
0
| printer.println("\\begin{document}");
|
|
172 |
0
| printer.println();
|
|
173 |
0
| printer.println("\\maketitle");
|
|
174 |
0
| printer.println();
|
|
175 |
0
| printer.println("\\setlength{\\parskip}{0pt}");
|
|
176 |
0
| printer.println("\\tableofcontents");
|
|
177 |
0
| printer.println("\\setlength{\\parskip}{5pt plus 2pt minus 1pt}");
|
|
178 |
0
| printer.println();
|
|
179 |
| } |
|
180 |
| |
|
181 |
| |
|
182 |
| |
|
183 |
| |
|
184 |
0
| private void printQedeqChapters() {
|
|
185 |
0
| final ChapterList chapters = qedeq.getChapterList();
|
|
186 |
0
| for (int i = 0; i < chapters.size(); i++) {
|
|
187 |
0
| final Chapter chapter = chapters.get(i);
|
|
188 |
0
| printer.print("\\chapter");
|
|
189 |
0
| if (chapter.getNoNumber() != null && chapter.getNoNumber().booleanValue()) {
|
|
190 |
0
| printer.print("*");
|
|
191 |
| } |
|
192 |
0
| printer.print("{");
|
|
193 |
0
| printer.print(getLatexListEntry(chapter.getTitle()));
|
|
194 |
0
| final String label = "chapter" + i;
|
|
195 |
0
| printer.println("} \\label{" + label + "} \\hypertarget{" + label + "}{}");
|
|
196 |
0
| if (chapter.getNoNumber() != null && chapter.getNoNumber().booleanValue()) {
|
|
197 |
0
| printer.println("\\addcontentsline{toc}{chapter}{"
|
|
198 |
| + getLatexListEntry(chapter.getTitle()) + "}"); |
|
199 |
| } |
|
200 |
0
| printer.println();
|
|
201 |
0
| if (chapter.getIntroduction() != null) {
|
|
202 |
0
| printer.println(getLatexListEntry(chapter.getIntroduction()));
|
|
203 |
0
| printer.println();
|
|
204 |
| } |
|
205 |
0
| final SectionList sections = chapter.getSectionList();
|
|
206 |
0
| if (sections != null) {
|
|
207 |
0
| printSections(i, sections);
|
|
208 |
0
| printer.println();
|
|
209 |
| } |
|
210 |
0
| printer.println("%% end of chapter " + getLatexListEntry(chapter.getTitle()));
|
|
211 |
0
| printer.println();
|
|
212 |
| } |
|
213 |
| } |
|
214 |
| |
|
215 |
| |
|
216 |
| |
|
217 |
| |
|
218 |
0
| private void printQedeqBibliography() {
|
|
219 |
0
| final LiteratureItemList items = qedeq.getLiteratureItemList();
|
|
220 |
0
| if (items == null) {
|
|
221 |
0
| return;
|
|
222 |
| } |
|
223 |
0
| printer.println("\\begin{thebibliography}{99}");
|
|
224 |
0
| for (int i = 0; i < items.size(); i++) {
|
|
225 |
0
| final LiteratureItem item = items.get(i);
|
|
226 |
0
| printer.print("\\bibitem{" + item.getLabel() + "} ");
|
|
227 |
0
| printer.println(getLatexListEntry(item.getItem()));
|
|
228 |
0
| printer.println();
|
|
229 |
| } |
|
230 |
0
| printer.println("\\end{thebibliography}");
|
|
231 |
| |
|
232 |
0
| if ("de".equals(language)) {
|
|
233 |
0
| printer.println("\\addcontentsline{toc}{chapter}{Literaturverzeichnis}");
|
|
234 |
| } else { |
|
235 |
0
| printer.println("\\addcontentsline{toc}{chapter}{Bibliography}");
|
|
236 |
| } |
|
237 |
| |
|
238 |
| } |
|
239 |
| |
|
240 |
| |
|
241 |
| |
|
242 |
| |
|
243 |
| |
|
244 |
| |
|
245 |
| |
|
246 |
0
| private void printSections(final int chapter, final SectionList sections) {
|
|
247 |
0
| if (sections == null) {
|
|
248 |
0
| return;
|
|
249 |
| } |
|
250 |
0
| for (int i = 0; i < sections.size(); i++) {
|
|
251 |
0
| final Section section = sections.get(i);
|
|
252 |
0
| printer.print("\\section{");
|
|
253 |
0
| printer.print(getLatexListEntry(section.getTitle()));
|
|
254 |
0
| final String label = "chapter" + chapter + "_section" + i;
|
|
255 |
0
| printer.println("} \\label{" + label + "} \\hypertarget{" + label + "}{}");
|
|
256 |
0
| printer.println(getLatexListEntry(section.getIntroduction()));
|
|
257 |
0
| printer.println();
|
|
258 |
0
| printSubsections(section.getSubsectionList());
|
|
259 |
| } |
|
260 |
| } |
|
261 |
| |
|
262 |
| |
|
263 |
| |
|
264 |
| |
|
265 |
| |
|
266 |
| |
|
267 |
0
| private void printSubsections(final SubsectionList nodes) {
|
|
268 |
0
| if (nodes == null) {
|
|
269 |
0
| return;
|
|
270 |
| } |
|
271 |
0
| for (int i = 0; i < nodes.size(); i++) {
|
|
272 |
0
| final SubsectionType subsectionType = nodes.get(i);
|
|
273 |
0
| if (subsectionType instanceof Node) {
|
|
274 |
0
| final Node node = (Node) subsectionType;
|
|
275 |
0
| printer.println("\\par");
|
|
276 |
0
| printer.println(getLatexListEntry(node.getPrecedingText()));
|
|
277 |
0
| printer.println();
|
|
278 |
0
| final String id = node.getId();
|
|
279 |
0
| final NodeType type = node.getNodeType();
|
|
280 |
0
| String title = null;
|
|
281 |
0
| if (node.getTitle() != null) {
|
|
282 |
0
| title = getLatexListEntry(node.getTitle());
|
|
283 |
| } |
|
284 |
0
| if (type instanceof Axiom) {
|
|
285 |
0
| printAxiom((Axiom) type, title, id);
|
|
286 |
0
| } else if (type instanceof PredicateDefinition) {
|
|
287 |
0
| printPredicateDefinition((PredicateDefinition) type, title, id);
|
|
288 |
0
| } else if (type instanceof FunctionDefinition) {
|
|
289 |
0
| printFunctionDefinition((FunctionDefinition) type, title, id);
|
|
290 |
0
| } else if (type instanceof Proposition) {
|
|
291 |
0
| printProposition((Proposition) type, title, id);
|
|
292 |
0
| } else if (type instanceof Rule) {
|
|
293 |
0
| printRule((Rule) type, title, id);
|
|
294 |
| } else { |
|
295 |
0
| throw new RuntimeException((type != null ? "unknown type: "
|
|
296 |
| + type.getClass().getName() : "node type empty")); |
|
297 |
| } |
|
298 |
0
| printer.println();
|
|
299 |
0
| printer.println(getLatexListEntry(node.getSucceedingText()));
|
|
300 |
| } else { |
|
301 |
0
| final Subsection subsection = (Subsection) subsectionType;
|
|
302 |
0
| if (subsection.getTitle() != null) {
|
|
303 |
0
| printer.print("\\subsection{");
|
|
304 |
0
| printer.println(getLatexListEntry(subsection.getTitle()));
|
|
305 |
0
| printer.println("}");
|
|
306 |
| } |
|
307 |
0
| printer.println(getLatexListEntry(subsection.getLatex()));
|
|
308 |
| } |
|
309 |
0
| printer.println();
|
|
310 |
0
| printer.println();
|
|
311 |
| } |
|
312 |
| } |
|
313 |
| |
|
314 |
| |
|
315 |
| |
|
316 |
| |
|
317 |
| |
|
318 |
| |
|
319 |
| |
|
320 |
| |
|
321 |
0
| private void printAxiom(final Axiom axiom, final String title, final String id) {
|
|
322 |
0
| printer.println("\\begin{ax}" + (title != null ? "[" + title + "]" : ""));
|
|
323 |
0
| printer.println("\\label{" + id + "} \\hypertarget{" + id + "}{}");
|
|
324 |
0
| printFormula(axiom.getFormula().getElement());
|
|
325 |
0
| printer.println(getLatexListEntry(axiom.getDescription()));
|
|
326 |
0
| printer.println("\\end{ax}");
|
|
327 |
| } |
|
328 |
| |
|
329 |
| |
|
330 |
| |
|
331 |
| |
|
332 |
| |
|
333 |
| |
|
334 |
| |
|
335 |
| |
|
336 |
0
| private void printTopFormula(final Element element, final String mainLabel) {
|
|
337 |
0
| if (!element.isList() || !element.getList().getOperator().equals("AND")) {
|
|
338 |
0
| printFormula(element);
|
|
339 |
0
| return;
|
|
340 |
| } |
|
341 |
0
| final ElementList list = element.getList();
|
|
342 |
0
| printer.println("\\mbox{}");
|
|
343 |
0
| printer.println("\\begin{longtable}{{@{\\extracolsep{\\fill}}p{0.9\\linewidth}l}}");
|
|
344 |
0
| for (int i = 0; i < list.size(); i++) {
|
|
345 |
0
| final String label = (i < ALPHABET.length() ? "" + ALPHABET .charAt(i) : "" + i);
|
|
346 |
0
| printer.println("\\centering $" + getLatex(list.getElement(i)) + "$"
|
|
347 |
| + " & \\label{" + mainLabel + ":" + label + "} \\hypertarget{" + mainLabel + ":" |
|
348 |
| + label + "}{} \\mbox{\\emph{(" + label + ")}} " |
|
349 |
0
| + (i + 1 < list.size() ? "\\\\" : ""));
|
|
350 |
| } |
|
351 |
0
| printer.println("\\end{longtable}");
|
|
352 |
| } |
|
353 |
| |
|
354 |
| |
|
355 |
| |
|
356 |
| |
|
357 |
| |
|
358 |
| |
|
359 |
0
| private void printFormula(final Element element) {
|
|
360 |
0
| printer.println("\\mbox{}");
|
|
361 |
0
| printer.println("\\begin{longtable}{{@{\\extracolsep{\\fill}}p{\\linewidth}}}");
|
|
362 |
0
| printer.println("\\centering $" + getLatex(element) + "$");
|
|
363 |
0
| printer.println("\\end{longtable}");
|
|
364 |
| } |
|
365 |
| |
|
366 |
| |
|
367 |
| |
|
368 |
| |
|
369 |
| |
|
370 |
| |
|
371 |
| |
|
372 |
| |
|
373 |
0
| private void printPredicateDefinition(final PredicateDefinition definition, final String title,
|
|
374 |
| final String id) { |
|
375 |
0
| final StringBuffer define = new StringBuffer("$$" + definition.getLatexPattern());
|
|
376 |
0
| final VariableList list = definition.getVariableList();
|
|
377 |
0
| if (list != null) {
|
|
378 |
0
| for (int i = list.size() - 1; i >= 0; i--) {
|
|
379 |
0
| Trace.trace(this, "printPredicateDefinition", "replacing!");
|
|
380 |
0
| ReplaceUtility.replace(define, "#" + (i + 1), getLatex(list.get(i)));
|
|
381 |
| } |
|
382 |
| } |
|
383 |
0
| if (definition.getFormula() != null) {
|
|
384 |
0
| printer.println("\\begin{defn}" + (title != null ? "[" + title + "]" : ""));
|
|
385 |
0
| printer.println("\\label{" + id + "} \\hypertarget{" + id + "}{}");
|
|
386 |
0
| define.append("\\ :\\leftrightarrow \\ ");
|
|
387 |
0
| define.append(getLatex(definition.getFormula().getElement()));
|
|
388 |
| } else { |
|
389 |
0
| printer.println("\\begin{idefn}" + (title != null ? "[" + title + "]" : ""));
|
|
390 |
0
| printer.println("\\label{" + id + "} \\hypertarget{" + id + "}{}");
|
|
391 |
| } |
|
392 |
0
| define.append("$$");
|
|
393 |
0
| definitions.put(definition.getName() + "_" + definition.getArgumentNumber(), definition);
|
|
394 |
0
| Trace.param(this, "printPredicateDefinition", "define", define);
|
|
395 |
0
| printer.println(define);
|
|
396 |
0
| printer.println(getLatexListEntry(definition.getDescription()));
|
|
397 |
0
| if (definition.getFormula() != null) {
|
|
398 |
0
| printer.println("\\end{defn}");
|
|
399 |
| } else { |
|
400 |
0
| printer.println("\\end{idefn}");
|
|
401 |
| } |
|
402 |
| } |
|
403 |
| |
|
404 |
| |
|
405 |
| |
|
406 |
| |
|
407 |
| |
|
408 |
| |
|
409 |
| |
|
410 |
| |
|
411 |
0
| private void printFunctionDefinition(final FunctionDefinition definition, final String title,
|
|
412 |
| final String id) { |
|
413 |
0
| final StringBuffer define = new StringBuffer("$$" + definition.getLatexPattern());
|
|
414 |
0
| final VariableList list = definition.getVariableList();
|
|
415 |
0
| if (list != null) {
|
|
416 |
0
| for (int i = list.size() - 1; i >= 0; i--) {
|
|
417 |
0
| Trace.trace(this, "printFunctionDefinition", "replacing!");
|
|
418 |
0
| ReplaceUtility.replace(define, "#" + (i + 1), getLatex(list.get(i)));
|
|
419 |
| } |
|
420 |
| } |
|
421 |
0
| if (definition.getTerm() != null) {
|
|
422 |
0
| printer.println("\\begin{defn}" + (title != null ? "[" + title + "]" : ""));
|
|
423 |
0
| printer.println("\\label{" + id + "} \\hypertarget{" + id + "}{}");
|
|
424 |
0
| define.append("\\ := \\ ");
|
|
425 |
0
| define.append(getLatex(definition.getTerm().getElement()));
|
|
426 |
| } else { |
|
427 |
0
| printer.println("\\begin{idefn}" + (title != null ? "[" + title + "]" : ""));
|
|
428 |
0
| printer.println("\\label{" + id + "} \\hypertarget{" + id + "}{}");
|
|
429 |
| } |
|
430 |
0
| define.append("$$");
|
|
431 |
0
| definitions.put(definition.getName() + "_" + definition.getArgumentNumber(), definition);
|
|
432 |
0
| Trace.param(this, "printFunctionDefinition", "define", define);
|
|
433 |
|