home *** CD-ROM | disk | FTP | other *** search
/ ftp.disi.unige.it / 2015-02-11.ftp.disi.unige.it.tar / ftp.disi.unige.it / pub / software / unix / lyx-0.10.7.tar.gz / lyx-0.10.7.tar / lyx-0.10.7 / math_write.C < prev    next >
C/C++ Source or Header  |  1996-09-19  |  7KB  |  284 lines

  1. /*
  2.  *  File:        math_write.h
  3.  *  Purpose:     Write math paragraphs in LaTeX
  4.  *  Author:      Alejandro Aguilar Sierra <asierra@servidor.unam.mx> 
  5.  *  Created:     January 1996
  6.  *  Description: 
  7.  *
  8.  *  Dependencies: Xlib, XForms
  9.  *
  10.  *  Copyright: (c) 1996, Alejandro Aguilar Sierra
  11.  *
  12.  *   Version: 0.5beta, Mathed & Lyx project.
  13.  *
  14.  *   You are free to use and modify this code under the terms of
  15.  *   the GNU General Public Licence version 2 or later.
  16.  */
  17.  
  18. #include "config.h"
  19. #include "math_inset.h"
  20. #include "math_parser.h"
  21.  
  22.  
  23. extern const char *latex_mathenv[];
  24. extern char *latex_mathspace[];
  25.  
  26. // quite a hack i know. Should be done with return values...
  27. static int number_of_newlines;
  28.  
  29. char *math_font_name[] = {
  30.    "mathrm",
  31.    "mathcal",
  32.    "mathbf",
  33.    "mathsf",
  34.    "mathtt",
  35.    "mathit",
  36.    "textrm"
  37. };
  38.  
  39. void
  40. MathSpaceInset::Write(FILE *outf)
  41.    if (space>=0 && space<6)
  42.      fprintf(outf, "\\%s ", latex_mathspace[space]);
  43. }
  44.  
  45.  
  46. void
  47. MathDotsInset::Write(FILE *outf)
  48. {
  49.    fprintf(outf, "\\%s ", name);
  50. }   
  51.  
  52. void MathSqrtInset::Write(FILE *outf)
  53.    fprintf(outf, "\\%s{", name);
  54.    MathParInset::Write(outf);  
  55.    fprintf(outf, "}");
  56. }
  57.  
  58. void MathDelimInset::Write(FILE *outf)
  59.    latexkeys* l = lm_get_key_by_id(left, LM_TK_SYM);
  60.    latexkeys* r = lm_get_key_by_id(right, LM_TK_SYM);
  61.    fprintf(outf, "\\left");
  62.    if (l) 
  63.      fprintf(outf, "\\%s ", l->name);
  64.    else {
  65.       if (left=='{' || left=='}')
  66.     fprintf(outf, "\\%c ", left);
  67.       else
  68.     fprintf(outf, "%c ", left);
  69.    }
  70.    MathParInset::Write(outf);  
  71.    fprintf(outf, "\\right");
  72.    if (r) 
  73.      fprintf(outf, "\\%s ", r->name);
  74.    else {
  75.       if (right=='{' || right=='}')
  76.         fprintf(outf, "\\%c ", right);
  77.       else
  78.         fprintf(outf, "%c ", right);
  79.    }        
  80. }
  81.  
  82.  
  83. void MathDecorationInset::Write(FILE *outf)
  84.    latexkeys* l = lm_get_key_by_id(deco, LM_TK_WIDE);
  85.    fprintf(outf, "\\%s{", l->name);
  86.    MathParInset::Write(outf);  
  87.    fprintf(outf, "}");
  88. }
  89.  
  90. void MathAccentInset::Write(FILE *outf)
  91.    latexkeys* l = lm_get_key_by_id(code, LM_TK_ACCENT);
  92.    fprintf(outf, "\\%s", l->name);
  93.    if (fn==LM_TC_SYMB) {
  94.       latexkeys *l = lm_get_key_by_id(c, LM_TK_SYM);
  95.       if (l)
  96.     fprintf(outf, "\\%s ", l->name);
  97.       else 
  98.     fprintf(stderr, "Illegal symbol code[%u %d]", c, fn);
  99.    } else 
  100.      fprintf(outf, " %c ", c);
  101. }
  102.  
  103.  
  104. void MathBigopInset::Write(FILE *outf)
  105.    fprintf(outf, "\\%s", name);
  106.    if (lims && (sym==LM_int || sym==LM_oint))
  107.      fprintf(outf, "\\limits ");
  108.    else 
  109.    if (!lims && !(sym==LM_int || sym==LM_oint))
  110.      fprintf(outf, "\\nolimits ");
  111.    else 
  112.      fprintf(outf, " ");
  113. }
  114.  
  115.  
  116. void MathFracInset::Write(FILE *outf)
  117.    fprintf(outf, "\\%s{", name);
  118.    MathParInset::Write(outf);  
  119.    fprintf(outf, "}{");
  120.    den->Write(outf);  
  121.    fprintf(outf, "}");
  122. }
  123.  
  124.  
  125. void MathParInset::Write(FILE *outf)
  126. {
  127.    if (!array) return;
  128.    int brace = 0;
  129.    latexkeys *l;
  130.    LyxMathIter data(array);
  131.    
  132.    data.Reset();
  133.     
  134.    if (fixedsz) { 
  135.        l = lm_get_key_by_id(size, LM_TK_STY);
  136.        if (l) fprintf(outf, "\\%s ", l->name);
  137.    }
  138.    while (data.OK()) {
  139.       byte cx = data.GetChar();
  140.       if (cx>=' ') {
  141. //     fprintf(outf, "OUT<%d %d>", data.FCode(), cx);
  142.      int ls;
  143.      byte *s = data.GetString(ls);
  144.        
  145.      if (data.FCode()>=LM_TC_RM && data.FCode()<=LM_TC_TEXTRM)
  146.        fprintf(outf, "\\%s{", math_font_name[data.FCode()-LM_TC_RM]);
  147.      while (ls>0) {
  148.         if (MathIsSymbol(data.FCode())) {
  149.         l = lm_get_key_by_id(*s,(data.FCode()==LM_TC_BSYM)?LM_TK_BIGSYM:LM_TK_SYM);
  150.            if (l)
  151.          fprintf(outf, "\\%s ", l->name);
  152.            else 
  153.          fprintf(stderr, "Illegal symbol code[%u %d %d]", *s, ls, data.FCode());
  154.         } else {
  155.            // Is there a standard logical XOR?
  156.            if ((*s=='{' || *s=='}') && (data.FCode()!=LM_TC_TEX) ||
  157.            !(*s=='{' || *s=='}') && (data.FCode()==LM_TC_TEX))
  158.          fprintf(outf, "\\");
  159.            else {
  160.           if (*s=='{') brace++;
  161.           if (*s=='}') brace--;
  162.            }
  163.            if (*s=='}' && data.FCode()==LM_TC_TEX && brace<0) 
  164.          fprintf(stderr, "Math warning: Unexpected closing brace.\n");
  165.            else           
  166.          fprintf(outf, "%c", *s);
  167.         }
  168.         s++; ls--;
  169.      }
  170.      if (data.FCode()>=LM_TC_RM && data.FCode()<=LM_TC_TEXTRM)
  171.        fprintf(outf, "}");     
  172.       } else     
  173.       if (MathIsInset(cx)) {
  174.      LyxMathInset *p = data.GetInset();
  175.      if (cx==LM_TC_UP)
  176.        fprintf(outf, "^{");
  177.      if (cx==LM_TC_DOWN)
  178.        fprintf(outf, "_{");
  179.      p->Write(outf);
  180.      if (cx==LM_TC_UP || cx==LM_TC_DOWN)
  181.        fprintf(outf, "}");
  182.      data.Next();
  183.       } else
  184.           switch(cx) {
  185.      case LM_TC_TAB:
  186.         {
  187.            fprintf(outf, " & ");
  188.            data.Next();
  189.            break;
  190.         }
  191.      case LM_TC_CR:
  192.         { 
  193.            fprintf(outf, "\\\\\n");
  194.            number_of_newlines++;
  195.            data.Next();
  196.            break;
  197.         }
  198.      default:
  199.        fprintf(stderr, "WMath Error: unrecognized code[%d]", cx);
  200.        return;
  201.     }     
  202.    }
  203.    while (brace>0) {
  204.       fprintf(outf, "}");
  205.       brace--;
  206.    }
  207. }
  208.  
  209. void MathMatrixInset::Write(FILE *outf)
  210. {
  211.    rowst *r = row;
  212.    //char s[80]; // unused
  213.  
  214.    if (GetType() == LM_OT_MATRIX){
  215.      fprintf(outf, "\\begin{%s}{%s}\n", name, h_align);
  216.      number_of_newlines++;
  217.    }
  218.    while (r) {
  219.       array = r->array;
  220.       if (r->label) 
  221.      fprintf(outf,"\\label{%s} ", r->label);
  222.       MathParInset::Write(outf);
  223.       if (!r->numbered) 
  224.     fprintf(outf,"\\nonumber ");
  225.       if (r->next) {
  226.     fprintf(outf,"\\\\\n");
  227.     number_of_newlines++;
  228.       }
  229.       r = r->next;      
  230.    }
  231.    if (GetType() == LM_OT_MATRIX){
  232.      fprintf(outf, "\n\\end{%s}\n", name);  
  233.      number_of_newlines+=2;
  234.    }
  235. }
  236.  
  237. void mathed_write(MathParInset* p,  FILE* outf, int* newlines,  char fragile, const char* label)
  238. {  
  239.    number_of_newlines = 0;
  240.    short mathed_env = p->GetType();
  241.  
  242.    if (mathed_env==LM_EN_INTEXT) {
  243.      if (fragile) fprintf(outf, "\\protect");
  244.      fprintf(outf,"\\( "); // changed from " \\( " (Albrecht Dress)
  245.    }
  246.    else if (mathed_env==LM_EN_DISPLAY){
  247.      fprintf(outf,"\n\\[\n");
  248.      number_of_newlines+=2;
  249.    }
  250.    else {
  251.      fprintf(outf,"\n\\begin{%s}\n", latex_mathenv[mathed_env]);
  252.      number_of_newlines+=2;
  253.    }
  254.    
  255.    if (label && mathed_env==LM_EN_EQUATION){
  256.      fprintf(outf,"\\label{%s}\n", label);
  257.      number_of_newlines++;
  258.    }
  259.  
  260.    p->Write(outf);
  261.    
  262.    if (mathed_env==LM_EN_INTEXT){
  263.      if (fragile) fprintf(outf, "\\protect");
  264.      fprintf(outf," \\)");
  265.    }
  266.    else if (mathed_env==LM_EN_DISPLAY){
  267.      fprintf(outf,"\\]\n");
  268.      number_of_newlines++;
  269.    }
  270.    else {
  271.      fprintf(outf,"\n\\end{%s}\n", latex_mathenv[mathed_env]);
  272.      number_of_newlines+=2;
  273.    }
  274.    fflush(outf);
  275.    *newlines = number_of_newlines;
  276. }
  277.