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"
26 extern char const *latex_mathenv[];
27 extern char *latex_mathspace[];
29 // quite a hack i know. Should be done with return values...
30 static int number_of_newlines;
32 char const *math_font_name[] = {
44 MathSpaceInset::Write(FILE *outf)
46 if (space>=0 && space<6) {
48 MathSpaceInset::Write(output);
49 fprintf(outf, "%s", output.c_str());
54 MathSpaceInset::Write(string &outf)
56 if (space>=0 && space<6) {
58 outf += latex_mathspace[space];
65 MathDotsInset::Write(FILE *outf)
68 MathDotsInset::Write(output);
69 fprintf(outf, "%s", output.c_str());
73 MathDotsInset::Write(string &outf)
81 void MathSqrtInset::Write(FILE *outf)
84 MathSqrtInset::Write(output);
85 fprintf(outf, "%s", output.c_str());
88 void MathSqrtInset::Write(string &outf)
93 MathParInset::Write(outf);
98 void MathDelimInset::Write(FILE *outf)
101 MathDelimInset::Write(output);
102 fprintf(outf, "%s", output.c_str());
105 void MathDelimInset::Write(string &outf)
107 latexkeys* l = (left != '|') ? lm_get_key_by_id(left, LM_TK_SYM): 0;
108 latexkeys* r = (right != '|') ? lm_get_key_by_id(right, LM_TK_SYM): 0;
115 if (left=='{' || left=='}') {
124 MathParInset::Write(outf);
131 if (right=='{' || right=='}') {
133 outf += (char) right;
136 outf += (char) right;
143 void MathDecorationInset::Write(FILE *outf)
146 MathDecorationInset::Write(output);
147 fprintf(outf, "%s", output.c_str());
150 void MathDecorationInset::Write(string &outf)
152 latexkeys* l = lm_get_key_by_id(deco, LM_TK_WIDE);
156 MathParInset::Write(outf);
161 void MathAccentInset::Write(FILE *outf)
164 MathAccentInset::Write(output);
165 fprintf(outf, "%s", output.c_str());
168 void MathAccentInset::Write(string &outf)
170 latexkeys* l = lm_get_key_by_id(code, LM_TK_ACCENT);
181 if (fn>=LM_TC_RM && fn<=LM_TC_TEXTRM) {
183 outf += math_font_name[fn-LM_TC_RM];
186 if (MathIsSymbol(fn)) {
187 latexkeys *l = lm_get_key_by_id(c, LM_TK_SYM);
196 if (fn>=LM_TC_RM && fn<=LM_TC_TEXTRM)
205 void MathBigopInset::Write(FILE *outf)
208 MathBigopInset::Write(output);
209 fprintf(outf, "%s", output.c_str());
212 void MathBigopInset::Write(string &outf)
214 bool limp = GetLimits();
219 if (limp && !(sym!=LM_int && sym!=LM_oint && (GetStyle()==LM_ST_DISPLAY)))
222 if (!limp && (sym!=LM_int && sym!=LM_oint && (GetStyle()==LM_ST_DISPLAY)))
223 outf += "\\nolimits ";
229 void MathFracInset::Write(FILE *outf)
232 MathFracInset::Write(output);
233 fprintf(outf, "%s", output.c_str());
236 void MathFracInset::Write(string &outf)
241 MathParInset::Write(outf);
248 void MathParInset::Write(FILE *outf)
252 MathParInset::Write(output);
253 fprintf(outf, "%s", output.c_str());
257 void MathParInset::Write(string &outf)
262 MathedIter data(array);
264 MathedRowSt const* crow = getRowSt();
267 if (!Permit(LMPF_FIXED_SIZE)) {
268 l = lm_get_key_by_id(size, LM_TK_STY);
276 byte cx = data.GetChar();
279 byte *s = data.GetString(ls);
281 if (data.FCode()>=LM_TC_RM && data.FCode()<=LM_TC_TEXTRM) {
283 outf += math_font_name[data.FCode()-LM_TC_RM];
287 if (MathIsSymbol(data.FCode())) {
288 l = lm_get_key_by_id(*s,(data.FCode()==LM_TC_BSYM)?LM_TK_BIGSYM:LM_TK_SYM);
294 lyxerr << "Illegal symbol code[" << *s
295 << " " << ls << " " << data.FCode() << "]";
298 // Is there a standard logical XOR?
299 if ((data.FCode()==LM_TC_TEX && *s!='{' && *s!='}') ||
300 (data.FCode()==LM_TC_SPECIAL))
303 if (*s=='{') brace++;
304 if (*s=='}') brace--;
306 if (*s=='}' && data.FCode()==LM_TC_TEX && brace<0)
307 lyxerr <<"Math warning: Unexpected closing brace."
314 if (data.FCode()>=LM_TC_RM && data.FCode()<=LM_TC_TEXTRM)
317 if (MathIsInset(cx)) {
318 MathedInset *p = data.GetInset();
324 if (cx==LM_TC_UP || cx==LM_TC_DOWN)
338 if (!crow->isNumbered()) {
339 outf +="\\nonumber ";
341 if (crow->getLabel()) {
343 outf += crow->getLabel();
346 crow = crow->getNext();
349 number_of_newlines++;
354 lyxerr << "WMath Error: unrecognized code[" << cx << "]";
360 if (!crow->isNumbered()) {
361 outf +="\\nonumber ";
363 if (crow->getLabel()) {
365 outf += crow->getLabel();
376 void MathMatrixInset::Write(FILE *outf)
379 MathMatrixInset::Write(output);
380 fprintf(outf, "%s", output.c_str());
383 void MathMatrixInset::Write(string &outf)
385 if (GetType() == LM_OT_MATRIX){
389 if (v_align=='t' || v_align=='b') {
391 outf += (char) v_align;
397 number_of_newlines++;
399 MathParInset::Write(outf);
400 if (GetType() == LM_OT_MATRIX){
404 number_of_newlines++;
409 void mathed_write(MathParInset* p, FILE* outf, int* newlines, char fragile, char const* label)
412 mathed_write(p, output, newlines, fragile, label);
413 fprintf(outf, "%s", output.c_str());
416 extern int tex_code_break_column;
418 void mathed_write(MathParInset* p, string& outf, int* newlines,
419 char fragile, char const* label)
421 number_of_newlines = 0;
422 short mathed_env = p->GetType();
424 if (mathed_env==LM_EN_INTEXT) {
425 if (fragile) outf += "\\protect";
426 outf += "\\( "; // changed from " \\( " (Albrecht Dress)
429 if (!suffixIs(outf, '\n')) {
430 // in batchmode we need to make sure
431 // a space before an equation doesn't
432 // make the LaTeX output different
433 // compared to "Exported LaTeX" ARRae
434 // Modified to work in a cleaner and hopefully more general way
437 number_of_newlines++;
439 if (mathed_env==LM_EN_DISPLAY){
444 outf += latex_mathenv[mathed_env];
447 number_of_newlines++;
450 if (label && label[0]>' ' && mathed_env==LM_EN_EQUATION){
454 number_of_newlines++;
459 if (mathed_env==LM_EN_INTEXT){
460 if (fragile) outf += "\\protect";
463 else if (mathed_env==LM_EN_DISPLAY){
465 number_of_newlines++;
469 outf += latex_mathenv[mathed_env];
471 number_of_newlines+=2;
473 *newlines = number_of_newlines;