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(ostream & os)
46 if (space >= 0 && space < 6) {
48 MathSpaceInset::Write(output);
55 MathSpaceInset::Write(string & outf)
57 if (space >= 0 && space < 6) {
59 outf += latex_mathspace[space];
66 MathDotsInset::Write(ostream & os)
69 MathDotsInset::Write(output);
75 MathDotsInset::Write(string & outf)
83 void MathSqrtInset::Write(ostream & os)
86 MathSqrtInset::Write(output);
91 void MathSqrtInset::Write(string & outf)
96 MathParInset::Write(outf);
101 void MathDelimInset::Write(ostream & os)
104 MathDelimInset::Write(output);
109 void MathDelimInset::Write(string & outf)
111 latexkeys * l = (left != '|') ? lm_get_key_by_id(left, LM_TK_SYM): 0;
112 latexkeys * r = (right != '|') ? lm_get_key_by_id(right, LM_TK_SYM): 0;
119 if (left == '{' || left == '}') {
128 MathParInset::Write(outf);
135 if (right == '{' || right == '}') {
147 void MathDecorationInset::Write(ostream & os)
150 MathDecorationInset::Write(output);
155 void MathDecorationInset::Write(string & outf)
157 latexkeys * l = lm_get_key_by_id(deco, LM_TK_WIDE);
161 MathParInset::Write(outf);
166 void MathAccentInset::Write(ostream & os)
169 MathAccentInset::Write(output);
174 void MathAccentInset::Write(string & outf)
176 latexkeys * l = lm_get_key_by_id(code, LM_TK_ACCENT);
187 if (fn>= LM_TC_RM && fn<= LM_TC_TEXTRM) {
189 outf += math_font_name[fn-LM_TC_RM];
192 if (MathIsSymbol(fn)) {
193 latexkeys *l = lm_get_key_by_id(c, LM_TK_SYM);
202 if (fn>= LM_TC_RM && fn<= LM_TC_TEXTRM)
211 void MathBigopInset::Write(ostream & os)
214 MathBigopInset::Write(output);
219 void MathBigopInset::Write(string & outf)
221 bool limp = GetLimits();
226 if (limp && !(sym!= LM_int && sym!= LM_oint && (GetStyle() == LM_ST_DISPLAY)))
229 if (!limp && (sym!= LM_int && sym!= LM_oint && (GetStyle() == LM_ST_DISPLAY)))
230 outf += "\\nolimits ";
236 void MathFracInset::Write(ostream & os)
239 MathFracInset::Write(output);
244 void MathFracInset::Write(string & outf)
249 MathParInset::Write(outf);
256 void MathParInset::Write(ostream & os)
260 MathParInset::Write(output);
265 void MathParInset::Write(string & outf)
270 MathedIter data(array);
272 MathedRowSt const * crow = getRowSt();
275 if (!Permit(LMPF_FIXED_SIZE)) {
276 l = lm_get_key_by_id(size, LM_TK_STY);
284 byte cx = data.GetChar();
287 byte * s = data.GetString(ls);
289 if (data.FCode()>= LM_TC_RM && data.FCode()<= LM_TC_TEXTRM) {
291 outf += math_font_name[data.FCode()-LM_TC_RM];
295 if (MathIsSymbol(data.FCode())) {
296 l = lm_get_key_by_id(*s,(data.FCode() == LM_TC_BSYM)?LM_TK_BIGSYM:LM_TK_SYM);
302 lyxerr << "Illegal symbol code[" << *s
303 << " " << ls << " " << data.FCode() << "]";
306 // Is there a standard logical XOR?
307 if ((data.FCode() == LM_TC_TEX && *s!= '{' && *s!= '}') ||
308 (data.FCode() == LM_TC_SPECIAL))
311 if (*s == '{') brace++;
312 if (*s == '}') brace--;
314 if (*s == '}' && data.FCode() == LM_TC_TEX && brace<0)
315 lyxerr <<"Math warning: Unexpected closing brace."
322 if (data.FCode()>= LM_TC_RM && data.FCode()<= LM_TC_TEXTRM)
325 if (MathIsInset(cx)) {
326 MathedInset *p = data.GetInset();
329 if (cx == LM_TC_DOWN)
332 if (cx == LM_TC_UP || cx == LM_TC_DOWN)
346 if (!crow->isNumbered()) {
347 outf += "\\nonumber ";
349 if (crow->getLabel()) {
351 outf += crow->getLabel();
354 crow = crow->getNext();
357 number_of_newlines++;
362 lyxerr << "WMath Error: unrecognized code[" << cx << "]";
368 if (!crow->isNumbered()) {
369 outf += "\\nonumber ";
371 if (crow->getLabel()) {
373 outf += crow->getLabel();
384 void MathMatrixInset::Write(ostream & os)
387 MathMatrixInset::Write(output);
392 void MathMatrixInset::Write(string & outf)
394 if (GetType() == LM_OT_MATRIX){
398 if (v_align == 't' || v_align == 'b') {
400 outf += char(v_align);
406 number_of_newlines++;
408 MathParInset::Write(outf);
409 if (GetType() == LM_OT_MATRIX){
413 number_of_newlines++;
418 void mathed_write(MathParInset * p, ostream & os, int * newlines,
419 char fragile, char const * label)
422 mathed_write(p, output, newlines, fragile, label);
427 extern int tex_code_break_column;
429 void mathed_write(MathParInset * p, string & outf, int * newlines,
430 char fragile, char const * label)
432 number_of_newlines = 0;
433 short mathed_env = p->GetType();
435 if (mathed_env == LM_EN_INTEXT) {
436 if (fragile) outf += "\\protect";
437 outf += "\\( "; // changed from " \\( " (Albrecht Dress)
440 if (!suffixIs(outf, '\n')) {
441 // in batchmode we need to make sure
442 // a space before an equation doesn't
443 // make the LaTeX output different
444 // compared to "Exported LaTeX" ARRae
445 // Modified to work in a cleaner and hopefully more general way
448 number_of_newlines++;
450 if (mathed_env == LM_EN_DISPLAY){
455 outf += latex_mathenv[mathed_env];
458 number_of_newlines++;
461 if (label && label[0]>' ' && mathed_env == LM_EN_EQUATION){
465 number_of_newlines++;
470 if (mathed_env == LM_EN_INTEXT){
471 if (fragile) outf += "\\protect";
474 else if (mathed_env == LM_EN_DISPLAY){
476 number_of_newlines++;
480 outf += latex_mathenv[mathed_env];
482 number_of_newlines+= 2;
484 *newlines = number_of_newlines;