3 * Purpose: Write math paragraphs in LaTeX
4 * Author: Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
5 * Created: January 1996
8 * Dependencies: Xlib, XForms
10 * Copyright: (c) 1996, 1997 Alejandro Aguilar Sierra
12 * Version: 0.8beta, Mathed & Lyx project.
14 * You are free to use and modify this code under the terms of
15 * the GNU General Public Licence version 2 or later.
20 #include "math_inset.h"
21 #include "math_iter.h"
22 #include "math_parser.h"
23 #include "support/lstrings.h"
25 extern char const *latex_mathenv[];
26 extern char *latex_mathspace[];
28 // quite a hack i know. Should be done with return values...
29 static int number_of_newlines;
31 char const *math_font_name[] = {
43 MathSpaceInset::Write(FILE *outf)
45 if (space>=0 && space<6) {
47 MathSpaceInset::Write(output);
48 fprintf(outf, "%s", output.c_str());
53 MathSpaceInset::Write(string &outf)
55 if (space>=0 && space<6) {
57 outf += latex_mathspace[space];
64 MathDotsInset::Write(FILE *outf)
67 MathDotsInset::Write(output);
68 fprintf(outf, "%s", output.c_str());
72 MathDotsInset::Write(string &outf)
80 void MathSqrtInset::Write(FILE *outf)
83 MathSqrtInset::Write(output);
84 fprintf(outf, "%s", output.c_str());
87 void MathSqrtInset::Write(string &outf)
92 MathParInset::Write(outf);
97 void MathDelimInset::Write(FILE *outf)
100 MathDelimInset::Write(output);
101 fprintf(outf, "%s", output.c_str());
104 void MathDelimInset::Write(string &outf)
106 latexkeys* l = (left != '|') ? lm_get_key_by_id(left, LM_TK_SYM): 0;
107 latexkeys* r = (right != '|') ? lm_get_key_by_id(right, LM_TK_SYM): 0;
114 if (left=='{' || left=='}') {
123 MathParInset::Write(outf);
130 if (right=='{' || right=='}') {
132 outf += (char) right;
135 outf += (char) right;
142 void MathDecorationInset::Write(FILE *outf)
145 MathDecorationInset::Write(output);
146 fprintf(outf, "%s", output.c_str());
149 void MathDecorationInset::Write(string &outf)
151 latexkeys* l = lm_get_key_by_id(deco, LM_TK_WIDE);
155 MathParInset::Write(outf);
160 void MathAccentInset::Write(FILE *outf)
163 MathAccentInset::Write(output);
164 fprintf(outf, "%s", output.c_str());
167 void MathAccentInset::Write(string &outf)
169 latexkeys* l = lm_get_key_by_id(code, LM_TK_ACCENT);
180 if (fn>=LM_TC_RM && fn<=LM_TC_TEXTRM) {
182 outf += math_font_name[fn-LM_TC_RM];
185 if (MathIsSymbol(fn)) {
186 latexkeys *l = lm_get_key_by_id(c, LM_TK_SYM);
195 if (fn>=LM_TC_RM && fn<=LM_TC_TEXTRM)
204 void MathBigopInset::Write(FILE *outf)
207 MathBigopInset::Write(output);
208 fprintf(outf, "%s", output.c_str());
211 void MathBigopInset::Write(string &outf)
213 bool limp = GetLimits();
218 if (limp && !(sym!=LM_int && sym!=LM_oint && (GetStyle()==LM_ST_DISPLAY)))
221 if (!limp && (sym!=LM_int && sym!=LM_oint && (GetStyle()==LM_ST_DISPLAY)))
222 outf += "\\nolimits ";
228 void MathFracInset::Write(FILE *outf)
231 MathFracInset::Write(output);
232 fprintf(outf, "%s", output.c_str());
235 void MathFracInset::Write(string &outf)
240 MathParInset::Write(outf);
247 void MathParInset::Write(FILE *outf)
251 MathParInset::Write(output);
252 fprintf(outf, "%s", output.c_str());
256 void MathParInset::Write(string &outf)
261 MathedIter data(array);
263 MathedRowSt const* crow = getRowSt();
266 if (!Permit(LMPF_FIXED_SIZE)) {
267 l = lm_get_key_by_id(size, LM_TK_STY);
275 byte cx = data.GetChar();
278 byte *s = data.GetString(ls);
280 if (data.FCode()>=LM_TC_RM && data.FCode()<=LM_TC_TEXTRM) {
282 outf += math_font_name[data.FCode()-LM_TC_RM];
286 if (MathIsSymbol(data.FCode())) {
287 l = lm_get_key_by_id(*s,(data.FCode()==LM_TC_BSYM)?LM_TK_BIGSYM:LM_TK_SYM);
293 fprintf(stderr, "Illegal symbol code[%u %d %d]", *s, ls, data.FCode());
296 // Is there a standard logical XOR?
297 if ((data.FCode()==LM_TC_TEX && *s!='{' && *s!='}') ||
298 (data.FCode()==LM_TC_SPECIAL))
301 if (*s=='{') brace++;
302 if (*s=='}') brace--;
304 if (*s=='}' && data.FCode()==LM_TC_TEX && brace<0)
305 fprintf(stderr, "Math warning: Unexpected closing brace.\n");
311 if (data.FCode()>=LM_TC_RM && data.FCode()<=LM_TC_TEXTRM)
314 if (MathIsInset(cx)) {
315 MathedInset *p = data.GetInset();
321 if (cx==LM_TC_UP || cx==LM_TC_DOWN)
335 if (!crow->isNumbered()) {
336 outf +="\\nonumber ";
338 if (crow->getLabel()) {
340 outf += crow->getLabel();
343 crow = crow->getNext();
346 number_of_newlines++;
351 fprintf(stderr, "WMath Error: unrecognized code[%d]", cx);
357 if (!crow->isNumbered()) {
358 outf +="\\nonumber ";
360 if (crow->getLabel()) {
362 outf += crow->getLabel();
373 void MathMatrixInset::Write(FILE *outf)
376 MathMatrixInset::Write(output);
377 fprintf(outf, "%s", output.c_str());
380 void MathMatrixInset::Write(string &outf)
382 if (GetType() == LM_OT_MATRIX){
386 if (v_align=='t' || v_align=='b') {
388 outf += (char) v_align;
394 number_of_newlines++;
396 MathParInset::Write(outf);
397 if (GetType() == LM_OT_MATRIX){
401 number_of_newlines++;
406 void mathed_write(MathParInset* p, FILE* outf, int* newlines, char fragile, char const* label)
409 mathed_write(p, output, newlines, fragile, label);
410 fprintf(outf, "%s", output.c_str());
413 extern int tex_code_break_column;
415 void mathed_write(MathParInset* p, string& outf, int* newlines,
416 char fragile, char const* label)
418 number_of_newlines = 0;
419 short mathed_env = p->GetType();
421 if (mathed_env==LM_EN_INTEXT) {
422 if (fragile) outf += "\\protect";
423 outf += "\\( "; // changed from " \\( " (Albrecht Dress)
426 if (!suffixIs(outf, '\n')) {
427 // in batchmode we need to make sure
428 // a space before an equation doesn't
429 // make the LaTeX output different
430 // compared to "Exported LaTeX" ARRae
431 // Modified to work in a cleaner and hopefully more general way
434 number_of_newlines++;
436 if (mathed_env==LM_EN_DISPLAY){
441 outf += latex_mathenv[mathed_env];
444 number_of_newlines++;
447 if (label && label[0]>' ' && mathed_env==LM_EN_EQUATION){
451 number_of_newlines++;
456 if (mathed_env==LM_EN_INTEXT){
457 if (fragile) outf += "\\protect";
460 else if (mathed_env==LM_EN_DISPLAY){
462 number_of_newlines++;
466 outf += latex_mathenv[mathed_env];
468 number_of_newlines+=2;
470 *newlines = number_of_newlines;