Пример #1
0
wxString GroupCell::ToTeX(wxString imgDir, wxString filename, int *imgCounter)
{
  wxString str;
  bool SuppressLeadingNewlines = true;
  // Now we might want to introduce some markdown:
  MarkDownTeX MarkDownParser;

  bool exportInput = true;
  wxConfig::Get()->Read(wxT("exportInput"), &exportInput);

  // pagebreak
  if (m_groupType == GC_TYPE_PAGEBREAK) {
    str = wxT("\\pagebreak\n");
    SuppressLeadingNewlines = true;
  }

  // IMAGE CELLS
  else if (m_groupType == GC_TYPE_IMAGE && imgDir != wxEmptyString) {
    MathCell *copy = m_output->Copy();
    (*imgCounter)++;
    wxString image = filename + wxString::Format(wxT("_%d"), *imgCounter);
    wxString file = imgDir + wxT("/") + image + wxT(".png");

    Bitmap bmp;
    bmp.SetData(copy);

    if (!wxDirExists(imgDir))
      wxMkdir(imgDir);

    if (bmp.ToFile(file).x>=0)
    {
      str << wxT("\\begin{figure}[htb]\n")
          << wxT("  \\begin{center}\n")
          << wxT("    \\includeimage{")
          << filename << wxT("_img/") << image << wxT("}\n")
          << wxT("  \\caption{") << PrepareForTeX(m_input->m_next->GetValue()) << wxT("}\n")
          << wxT("  \\end{center}\n")
          << wxT("\\end{figure}");
    }
  }
  else if (m_groupType == GC_TYPE_IMAGE)
    str << wxT("\n\\vert|<<GRAPHICS>>|\n");

  // CODE CELLS
  else if (m_groupType == GC_TYPE_CODE) {
    // Input cells
    if(exportInput)
    {
      str = wxT("\n\\noindent\n%%%%%%%%%%%%%%%\n")
        wxT("%%% INPUT:\n")
        wxT("\\begin{minipage}[t]{8ex}{\\color{red}\\bf\n")
        wxT("\\begin{verbatim}\n") +
        m_input->ToString() +
        wxT("\n\\end{verbatim}}\n\\end{minipage}");
      
      if (m_input->m_next!=NULL)
      {
        
        wxString input = m_input->m_next->ToString();
#if wxUSE_UNICODE
        input.Replace(wxT("\x2212"), wxT("-")); // unicode minus sign
        input.Replace(wxT("\xDCB6"), wxT(" ")); // Some weird unicode space character
#endif
        str += wxT("\n\\begin{minipage}[t]{\\textwidth}{\\color{blue}\n\\begin{verbatim}\n") +
          input +
             wxT("\n\\end{verbatim}}\n\\end{minipage}");
      }
      
      str += wxT("\n");
    }
    else str = wxEmptyString;

    if (m_output != NULL) {
      str += wxT("%%% OUTPUT:\n");
      // Need to define labelcolor if this is Copy as LaTeX!
      if (imgCounter == NULL)
        str += wxT("\\definecolor{labelcolor}{RGB}{100,0,0}\n");
      MathCell *tmp = m_output;

      bool mathMode = false;

      while (tmp != NULL)
      {
        if (tmp->GetType() == MC_TYPE_IMAGE || tmp->GetType() == MC_TYPE_SLIDE)
        {
          if (imgDir != wxEmptyString)
          {
            MathCell *copy = tmp->Copy();
            (*imgCounter)++;
            wxString image = filename + wxString::Format(wxT("_%d"), *imgCounter);
	    
            if (!wxDirExists(imgDir))
              if (!wxMkdir(imgDir))
                continue;
	    
	    // Do we want to output LaTeX animations?
	    bool AnimateLaTeX=true;
	    wxConfig::Get()->Read(wxT("AnimateLaTeX"), &AnimateLaTeX);
	    if((tmp->GetType() == MC_TYPE_SLIDE)&&(AnimateLaTeX))
            {
              SlideShow* src=(SlideShow *)tmp;
              str << wxT("\\begin{animateinline}{")+wxString::Format(wxT("%i"), src->GetFrameRate())+wxT("}\n");
              for(int i=0;i<src->Length();i++)
              {
                wxString Frame = imgDir + wxT("/") + image + wxString::Format(wxT("_%i"), i);
                if((src->GetBitmap(i)).SaveFile(Frame+wxT(".png")))
                  str << wxT("\\includegraphics[width=.95\\linewidth,height=.80\\textheight,keepaspectratio]{")+Frame+wxT("}\n");
                else
                  str << wxT("\n\\verb|<<GRAPHICS>>|\n");
                if(i<src->Length()-1)
                  str << wxT("\\newframe");
              }
              str << wxT("\\end{animateinline}");
            }
	    else
            {
              wxString file = imgDir + wxT("/") + image + wxT(".png");
              
              Bitmap bmp;
              bmp.SetData(copy);
              if (bmp.ToFile(file).x>=0)
                str += wxT("\\includegraphics[width=.95\\linewidth,height=.80\\textheight,keepaspectratio]{") +
                  filename + wxT("_img/") + image + wxT("}");
              else
                str << wxT("\n\\verb|<<GRAPHICS>>|\n");
            }
	  }
	}
        else if (tmp->GetStyle() == TS_LABEL)
        {
	  if(mathMode)
	    str += wxT("\\end{math}\n\n\\begin{math}\\displaystyle\n");
	  else
	    {
	      str += wxT("\n\n\\begin{math}\\displaystyle\n");
	      mathMode=true;
	    }
          str += wxT("\\parbox{8ex}{\\color{labelcolor}") + tmp->ToTeX() + wxT("}\n");
        }
	
        else
	  {
	    if((tmp->GetStyle() == TS_DEFAULT)||(tmp->GetStyle() == TS_STRING))
	      {
		// We either got a bracket or a parenthesis or regular Text
		// that shouldn't be displayed as math.
		if((mathMode)&&(tmp->ToString().Length()>2))
		  {
		    str += wxT("\\mbox{}");
		    str += wxT("\n\\end{math}\n");
		    mathMode = false;
		  }
	      }
	    else
	      {
		if(!mathMode)
		  {
		    str += wxT("\n\n\\begin{math}\\displaystyle\n");
		    mathMode = true;
		  }
	      }		
	    str += TexEscapeOutputCell(tmp->ToTeX());
	  }
        tmp = tmp->m_nextToDraw;
      }
      if(mathMode)
	{
	  // Some invisible dummy content that keeps TeX happy if there really is
	  // no output to display.
	  str += wxT("\\mbox{}");
	  str += wxT("\n\\end{math}\n%%%%%%%%%%%%%%%\n");
	}
    }
  }

  // TITLES, SECTIONS, SUBSECTIONS, SUBSUBSECTIONS, TEXT
  else if (GetEditable() != NULL && !m_hide) {
    str = GetEditable()->ListToTeX();
    switch (GetEditable()->GetStyle()) {
      case TS_TITLE:
        str = wxT("\n\\pagebreak{}\n{\\Huge {\\sc ") + PrepareForTeX(str) + wxT("}}\n");
        str += wxT("\\setcounter{section}{0}\n\\setcounter{subsection}{0}\n");
        str += wxT("\\setcounter{figure}{0}\n\n");
        break;
      case TS_SECTION:
        str = wxT("\n\\section{") + PrepareForTeX(str) + wxT("}\n\n");
        break;
      case TS_SUBSECTION:
        str = wxT("\n\\subsection{") + PrepareForTeX(str) + wxT("}\n\n");
        break;
      case TS_SUBSUBSECTION:
        str = wxT("\n\\subsubsection{") + PrepareForTeX(str) + wxT("}\n\n");
        break;
      default:
        if (str.StartsWith(wxT("TeX:")))
          str = str.Mid(5, str.Length());
        else {
          str = PrepareForTeX(str);
	  str = MarkDownParser.MarkDown(str);
        }
        break;
    }
  }
    
  return str;
}
Пример #2
0
wxString GroupCell::ToTeX(bool all, wxString imgDir, wxString filename, int *imgCounter)
{
  wxString str;

  // pagebreak
  if (m_groupType == GC_TYPE_PAGEBREAK) {
    str = wxT("\\pagebreak\n");
  }

  // IMAGE CELLS
  else if (m_groupType == GC_TYPE_IMAGE && imgDir != wxEmptyString) {
    MathCell *copy = m_output->Copy(false);
    (*imgCounter)++;
    wxString image = filename + wxString::Format(wxT("_%d.png"), *imgCounter);
    wxString file = imgDir + wxT("/") + image;

    Bitmap bmp;
    bmp.SetData(copy);

    if (!wxDirExists(imgDir))
      wxMkdir(imgDir);

    if (bmp.ToFile(file))
    {
      str << wxT("\\begin{figure}[htb]\n")
          << wxT("  \\begin{center}\n")
          << wxT("    \\includegraphics{")
          << filename << wxT("_img/") << image << wxT("}\n")
          << wxT("  \\caption{") << PrepareForTeX(m_input->m_next->GetValue()) << wxT("}\n")
          << wxT("  \\end{center}\n")
          << wxT("\\end{figure}");
    }
  }
  else if (m_groupType == GC_TYPE_IMAGE)
    str << wxT("\n\\vert|<<GRAPHICS>>|\n");

  // CODE CELLS
  else if (m_groupType == GC_TYPE_CODE) {
    // Input cells
    str = wxT("\n\\noindent\n%%%%%%%%%%%%%%%\n")
          wxT("%%% INPUT:\n")
          wxT("\\begin{minipage}[t]{8ex}{\\color{red}\\bf\n")
          wxT("\\begin{verbatim}\n") +
          m_input->ToString(false) +
          wxT("\n\\end{verbatim}}\n\\end{minipage}");

    if (m_input->m_next!=NULL)
    {
      str += wxT("\n\\begin{minipage}[t]{\\textwidth}{\\color{blue}\n\\begin{verbatim}\n") +
             m_input->m_next->ToString(true) +
             wxT("\n\\end{verbatim}}\n\\end{minipage}");
    }

    str += wxT("\n");

    if (m_output != NULL) {
      str += wxT("%%% OUTPUT:\n");
      // Need to define labelcolor if this is Copy as LaTeX!
      if (imgCounter == NULL)
        str += wxT("\\definecolor{labelcolor}{RGB}{100,0,0}\n");
      str += wxT("\\begin{math}\\displaystyle\n");
      MathCell *tmp = m_output;

      while (tmp != NULL) {

        if (tmp->GetType() == MC_TYPE_IMAGE || tmp->GetType() == MC_TYPE_SLIDE)
        {
          if (imgDir != wxEmptyString)
          {
            MathCell *copy = tmp->Copy(false);
            (*imgCounter)++;
            wxString image = filename + wxString::Format(wxT("_%d.png"), *imgCounter);
            wxString file = imgDir + wxT("/") + image;

            Bitmap bmp;
            bmp.SetData(copy);

            if (!wxDirExists(imgDir))
              if (!wxMkdir(imgDir))
                continue;

            if (bmp.ToFile(file))
              str += wxT("\\includegraphics[width=9cm]{") +
                  filename + wxT("_img/") + image + wxT("}");
          }
          else
            str << wxT("\n\\verb|<<GRAPHICS>>|\n");
        }

        else if (tmp->GetStyle() == TS_LABEL)
        {
          if (str.Right(13) != wxT("displaystyle\n"))
            str += wxT("\n\\end{math}\n\n\\begin{math}\\displaystyle\n");
          str += wxT("\\parbox{8ex}{\\color{labelcolor}") + tmp->ToTeX(false) + wxT("}\n");
        }

        else
          str += tmp->ToTeX(false);

        tmp = tmp->m_nextToDraw;
      }
      str += wxT("\n\\end{math}\n%%%%%%%%%%%%%%%\n");
    }
  }

  // TITLES, SECTIONS, SUBSECTIONS, TEXT
  else if (GetEditable() != NULL && !m_hide) {
    str = GetEditable()->ToTeX(true);
    switch (GetEditable()->GetStyle()) {
      case TS_TITLE:
        str = wxT("\n\\pagebreak{}\n{\\Huge {\\sc ") + PrepareForTeX(str) + wxT("}}\n");
        str += wxT("\\setcounter{section}{0}\n\\setcounter{subsection}{0}\n");
        str += wxT("\\setcounter{figure}{0}\n\n");
        break;
      case TS_SECTION:
        str = wxT("\n\\section{") + PrepareForTeX(str) + wxT("}\n\n");
        break;
      case TS_SUBSECTION:
        str = wxT("\n\\subsection{") + PrepareForTeX(str) + wxT("}\n\n");
        break;
      default:
        if (str.StartsWith(wxT("TeX:")))
          str = str.Mid(5, str.Length());
        else {
          str = PrepareForTeX(str);
        }
        break;
    }
  }

  return str + MathCell::ToTeX(all);
}