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.
21 #include "math_inset.h"
22 #include "math_iter.h"
23 #include "math_parser.h"
24 #include "support/lstrings.h"
27 extern char const * latex_mathenv[];
28 extern char * latex_mathspace[];
30 // quite a hack i know. Should be done with return values...
31 static int number_of_newlines;
33 char const * math_font_name[] = {
45 MathSpaceInset::Write(ostream & os)
47 if (space >= 0 && space < 6) {
49 MathSpaceInset::Write(output);
56 MathSpaceInset::Write(string & outf)
58 if (space >= 0 && space < 6) {
60 outf += latex_mathspace[space];
67 MathDotsInset::Write(ostream & os)
70 MathDotsInset::Write(output);
76 MathDotsInset::Write(string & outf)
84 void MathSqrtInset::Write(ostream & os)
87 MathSqrtInset::Write(output);
92 void MathSqrtInset::Write(string & outf)
97 MathParInset::Write(outf);
102 void MathDelimInset::Write(ostream & os)
105 MathDelimInset::Write(output);
110 void MathDelimInset::Write(string & outf)
112 latexkeys * l = (left != '|') ? lm_get_key_by_id(left, LM_TK_SYM): 0;
113 latexkeys * r = (right != '|') ? lm_get_key_by_id(right, LM_TK_SYM): 0;
120 if (left == '{' || left == '}') {
129 MathParInset::Write(outf);
136 if (right == '{' || right == '}') {
148 void MathDecorationInset::Write(ostream & os)
151 MathDecorationInset::Write(output);
156 void MathDecorationInset::Write(string & outf)
158 latexkeys * l = lm_get_key_by_id(deco, LM_TK_WIDE);
162 MathParInset::Write(outf);
167 void MathAccentInset::Write(ostream & os)
170 MathAccentInset::Write(output);
175 void MathAccentInset::Write(string & outf)
177 latexkeys * l = lm_get_key_by_id(code, LM_TK_ACCENT);
188 if (fn>= LM_TC_RM && fn<= LM_TC_TEXTRM) {
190 outf += math_font_name[fn-LM_TC_RM];
193 if (MathIsSymbol(fn)) {
194 latexkeys *l = lm_get_key_by_id(c, LM_TK_SYM);
203 if (fn>= LM_TC_RM && fn<= LM_TC_TEXTRM)
212 void MathBigopInset::Write(ostream & os)
215 MathBigopInset::Write(output);
220 void MathBigopInset::Write(string & outf)
222 bool limp = GetLimits();
227 if (limp && !(sym!= LM_int && sym!= LM_oint && (GetStyle() == LM_ST_DISPLAY)))
230 if (!limp && (sym!= LM_int && sym!= LM_oint && (GetStyle() == LM_ST_DISPLAY)))
231 outf += "\\nolimits ";
237 void MathFracInset::Write(ostream & os)
240 MathFracInset::Write(output);
245 void MathFracInset::Write(string & outf)
250 MathParInset::Write(outf);
257 void MathParInset::Write(ostream & os)
261 MathParInset::Write(output);
266 void MathParInset::Write(string & outf)
271 MathedIter data(array);
273 MathedRowSt const * crow = getRowSt();
276 if (!Permit(LMPF_FIXED_SIZE)) {
277 l = lm_get_key_by_id(size, LM_TK_STY);
285 byte cx = data.GetChar();
288 byte * s = data.GetString(ls);
290 if (data.FCode()>= LM_TC_RM && data.FCode()<= LM_TC_TEXTRM) {
292 outf += math_font_name[data.FCode()-LM_TC_RM];
296 if (MathIsSymbol(data.FCode())) {
297 l = lm_get_key_by_id(*s,(data.FCode() == LM_TC_BSYM)?LM_TK_BIGSYM:LM_TK_SYM);
303 lyxerr << "Illegal symbol code[" << *s
304 << " " << ls << " " << data.FCode() << "]";
307 // Is there a standard logical XOR?
308 if ((data.FCode() == LM_TC_TEX && *s!= '{' && *s!= '}') ||
309 (data.FCode() == LM_TC_SPECIAL))
312 if (*s == '{') brace++;
313 if (*s == '}') brace--;
315 if (*s == '}' && data.FCode() == LM_TC_TEX && brace<0)
316 lyxerr <<"Math warning: Unexpected closing brace."
323 if (data.FCode()>= LM_TC_RM && data.FCode()<= LM_TC_TEXTRM)
326 if (MathIsInset(cx)) {
327 MathedInset *p = data.GetInset();
330 if (cx == LM_TC_DOWN)
333 if (cx == LM_TC_UP || cx == LM_TC_DOWN)
347 if (!crow->isNumbered()) {
348 outf += "\\nonumber ";
350 if (crow->getLabel()) {
352 outf += crow->getLabel();
355 crow = crow->getNext();
358 number_of_newlines++;
363 lyxerr << "WMath Error: unrecognized code[" << cx << "]";
369 if (!crow->isNumbered()) {
370 outf += "\\nonumber ";
372 if (crow->getLabel()) {
374 outf += crow->getLabel();
385 void MathMatrixInset::Write(ostream & os)
388 MathMatrixInset::Write(output);
393 void MathMatrixInset::Write(string & outf)
395 if (GetType() == LM_OT_MATRIX){
399 if (v_align == 't' || v_align == 'b') {
401 outf += char(v_align);
407 number_of_newlines++;
409 MathParInset::Write(outf);
410 if (GetType() == LM_OT_MATRIX){
414 number_of_newlines++;
419 void mathed_write(MathParInset * p, ostream & os, int * newlines,
420 char fragile, char const * label)
423 mathed_write(p, output, newlines, fragile, label);
428 extern int tex_code_break_column;
430 void mathed_write(MathParInset * p, string & outf, int * newlines,
431 char fragile, char const * label)
433 number_of_newlines = 0;
434 short mathed_env = p->GetType();
436 if (mathed_env == LM_EN_INTEXT) {
437 if (fragile) outf += "\\protect";
438 outf += "\\( "; // changed from " \\( " (Albrecht Dress)
441 if (!suffixIs(outf, '\n')) {
442 // in batchmode we need to make sure
443 // a space before an equation doesn't
444 // make the LaTeX output different
445 // compared to "Exported LaTeX" ARRae
446 // Modified to work in a cleaner and hopefully more general way
449 number_of_newlines++;
451 if (mathed_env == LM_EN_DISPLAY){
456 outf += latex_mathenv[mathed_env];
459 number_of_newlines++;
462 if (label && label[0]>' ' && mathed_env == LM_EN_EQUATION){
466 number_of_newlines++;
471 if (mathed_env == LM_EN_INTEXT){
472 if (fragile) outf += "\\protect";
475 else if (mathed_env == LM_EN_DISPLAY){
477 number_of_newlines++;
481 outf += latex_mathenv[mathed_env];
483 number_of_newlines+= 2;
485 *newlines = number_of_newlines;