本文将讲解如何通过codecogs.com和Google.com提供的API接口来将LaTeX数学函数表达式转化为图片形式。具体思路如下:

(1)通过TextBox获取用户输入的LaTeX数学表达式,然后对表达式格式化使之便于网络传输。

(2)将格式化之后的字符串,通过Http请求发送至codecogs.com或者Google.com。

(3)获取网站返回的数据流,将其转化为图片,并显示在PictureBox上。

具体过程为:

首先,我们在这个网站输入LaTeX数学公式然后返回图片时,即“http://latex.codecogs.com/gif.latex?“后面跟上我们输入的公式内容。比如”http://latex.codecogs.com/gif.latex?\alpha”就显示一个希腊字母。所以我们可以在其后加上我们希望转换的公式即可。但是需要注意的是,网络URL中的空格有时候会自动转化为加号”+“。所以,我们在传输的时候需要将空格去掉。或者将其转换为”%20“。

建立如图所示的Form。一个TextBox,六个Button和一个PictureBox。

用例为著名的“薛定谔方程”:

i\hbar\frac{\partial \psi}{\partial {t}} = \frac{-\hbar^2}{2m} \left( \frac{\partial^2}{\partial {x^2}} + \frac{\partial^2}{\partial {y^2}} + \frac{\partial^2}{\partial {z^2}} \right) \psi + V \psi

“粘贴文本”按钮添加如下单击事件。

           private void btnPasteText_Click(object sender, EventArgs e)
{
string content = Clipboard.GetText(); // 获取剪切板文本信息
textBox.Text = content; // 将信息显示到TextBox
}
private bool check()
{
if(textBox.Text.Trim() == "") // 如果TextBox为空
{
MessageBox.Show(this, "请填写 LaTeX 函数代码!");
return false;
}
return true;
}

“Google预览”按钮添加如下事件。

           private void btnPreviewGoogle_Click(object sender, EventArgs e)
{
if (check())
{
// 首先将文本信息格式化,作为URL信息。
string ImgUrl = String.Format(PicUrlGoogle, HttpUtility.UrlPathEncode(textBox.Text));
pictureBox.ImageLocation = ImgUrl; // 加载网络图片到PictureBox
btnCopyImg.Enabled = true; // 使“复制图像”按钮可用
}
else
btnCopyImg.Enabled = false; // 否则使“复制图像”按钮不可用
}

“Cogs预览”按钮添加如下事件。

         private void btnPreviewCogs_Click(object sender, EventArgs e)
{
if (check())
{
// 首先将文本信息格式化,作为URL信息。
string ImgUrl = String.Format(PicUrlCogs, HttpUtility.UrlPathEncode(textBox.Text));
pictureBox.ImageLocation = ImgUrl; // 加载网络图片到PictureBox
btnCopyImg.Enabled = true; // 使“复制图像”按钮可用
}
else
btnCopyImg.Enabled = false; // 否则使“复制图像”按钮不可用
}

“复制图像”按钮添加如下单击事件。

        private void btnCopyImg_Click(object sender, EventArgs e)
{
if(pictureBox.Image != null)
Clipboard.SetImage(pictureBox.Image); // 将Picture图片复制到剪切板
}

“显示帮助”按钮添加如下事件。

        private void btnHelp_Click(object sender, EventArgs e)
{
textBox.Text = "1、LaTex 公式前后无需 $ 符号;\r\n"
+ "2、需要联网,Google丑,Cogs慢;\r\n"
+ "3、尽量多使用 {} 将字段括起来;\r\n"
+ "4、于 2015年11月13日。";
}

“退出”按钮添加如下事件。

        private void btnExit_Click(object sender, EventArgs e)
{
System.Environment.Exit(0); // 退出程序
}

完整代码如下:

using System;
using System.Windows.Forms;
using System.Web; namespace LaTeX_Win
{
public partial class Form1 : Form
{
private static string PicUrlGoogle = @"http://chart.apis.google.com/chart?cht=tx&chl={0}";
private static string PicUrlCogs = @"http://latex.codecogs.com/gif.latex?{0}";
public Form1()
{
InitializeComponent();
} private void btnPasteText_Click(object sender, EventArgs e)
{
string content = Clipboard.GetText();
textBox.Text = content;
} private void btnPreviewGoogle_Click(object sender, EventArgs e)
{
if (check())
{
string ImgUrl = String.Format(PicUrlGoogle, HttpUtility.UrlPathEncode(textBox.Text));
pictureBox.ImageLocation = ImgUrl;
btnCopyImg.Enabled = true;
}
else
btnCopyImg.Enabled = false;
}
private void btnPreviewCogs_Click(object sender, EventArgs e)
{
if (check())
{
string ImgUrl = String.Format(PicUrlCogs, HttpUtility.UrlPathEncode(textBox.Text));
pictureBox.ImageLocation = ImgUrl;
btnCopyImg.Enabled = true;
}
else
btnCopyImg.Enabled = false;
}
private void btnCopyImg_Click(object sender, EventArgs e)
{
if(pictureBox.Image != null)
Clipboard.SetImage(pictureBox.Image);
}
private bool check()
{
if(textBox.Text.Trim() == "")
{
MessageBox.Show(this, "请填写 LaTeX 函数代码!");
return false;
}
return true;
} private void btnExit_Click(object sender, EventArgs e)
{
System.Environment.Exit(0);
} private void btnHelp_Click(object sender, EventArgs e)
{
textBox.Text = "1、LaTex 公式前后无需 $ 符号;\r\n"
+ "2、需要联网,Google丑,Cogs慢;\r\n"
+ "3、尽量多使用 {} 将字段括起来;\r\n"
+ "4、于 2015年11月13日。";
}
}
}

C#开发基于Http的LaTeX数学公式转换器的更多相关文章

  1. Android开发手记(29) 基于Http的LaTeX数学公式转换器

    本文将讲解如何通过codecogs.com和Google.com提供的API接口来将LaTeX数学函数表达式转化为图片形式.具体思路如下: (1)通过EditText获取用户输入的LaTeX数学表达式 ...

  2. Spring7——开发基于注解形式的spring

    开发基于注解形式的spring SpringIOC容器的2种形式: (1)xml配置文件:applicationContext.xml; 存bean:<bean> 取bean: Appli ...

  3. [Intel Edison开发板] 05、Edison开发基于MRAA实现IO控制,特别是UART通信

    一.前言 下面是本系列文章的前几篇: [Intel Edison开发板] 01.Edison开发板性能简述 [Intel Edison开发板] 02.Edison开发板入门 [Intel Edison ...

  4. {VS2010C#}{WinForm}{ActiveX}VS2010C#开发基于WinForm的ActiveX控件

    在VS2010中使用C#开发基于WinForm的ActiveX控件 常见的一些ActiveX大部分是使用VB.Delphi.C++开发,使用C#开发ActiveX要解决下面三个问题: 使.NET组件可 ...

  5. markdown下编辑latex数学公式

    在利用为知笔记编写笔记的时候,有时需要用的markdown,只要把文件名加上后缀.md,就可以使用markdown语法,以下介绍在markdown下编辑latex数学公式. 使用LaTeX写公式的基本 ...

  6. Form_Form Builder开发基于视图页面和自动代码生成包(案例)

     2014-01-06 Created By BaoXinjian

  7. 转】Mahout分步式程序开发 基于物品的协同过滤ItemCF

    原博文出自于: http://blog.fens.me/hadoop-mahout-mapreduce-itemcf/ 感谢! Posted: Oct 14, 2013 Tags: Hadoopite ...

  8. Markdown 添加 Latex 数学公式

    添加公式的方法 Latex 数学公式语法 添加公式的方法 行内公式 $行内公式$ 行间公式 $$行间公式$$ Latex 数学公式语法 角标(上下标) 上标命令^{} 下标命令_{} 上下标命令用来放 ...

  9. 最简单的基于FFMPEG的封装格式转换器(无编解码)

    本文介绍一个基于FFMPEG的封装格式转换器.所谓的封装格式转换,就是在AVI,FLV,MKV,MP4这些格式之间转换(相应.avi,.flv,.mkv,.mp4文件).须要注意的是,本程序并不进行视 ...

随机推荐

  1. Choose the best route

    hdu 2680:http://acm.hdu.edu.cn/showproblem.php?pid=2680 这道题值得一提的两点:在图论中注意重边问题是必须的,有向无向也是同等重要的,如这道题 f ...

  2. NOT EXISTS优化

    INSERT INTO F_PTY_INDIV (PTY_ID, PTY_NAME, GENDER_CD, BIRTHDAY, CERT_TYPE, CERT_NO, SOCINSUR_NO, COU ...

  3. sort merge join,hash join,netsloop join

    Join Operations ? SORT-MERGE JOIN – Sorts tables on the join key and then merges them together – Sor ...

  4. Bluetooth LE(低功耗蓝牙) - 第三部分

    回顾 在本系列的前两篇文章中,我们已经了解了一些关于Bluetooth LE的背景并建立一个简单的Activity / Service框架.   在这篇文章中,我们将探讨Bluetooth LE的细节 ...

  5. .classpath 和.project文件含义

    .classpath文件是在建立eclipse工程时创建的描述工程配置情况的文件,包括:  * 源码路径  * 编译结果的输出路径  * 所使用的外部库的路径 -----------------下面是 ...

  6. 【HDOJ】1422 重温世界杯

    简单题. #include <stdio.h> #define MAXN 100005 int wi[MAXN], li[MAXN]; ]; int main() { int n, tot ...

  7. 【转】Eclipse中设置ButterKnife进行注解式开发步骤 -- 不错

    原文网址:http://www.bubuko.com/infodetail-974262.html 最近在进行Android注解式开发的学习,正在尝试用ButterKnife.ButterKnife的 ...

  8. 嵌入式系统烧写uboot/bootloader/kernel的一般方法

    嵌入式系统烧写uboot/bootloader/kernel的一般方法   本文介绍了在嵌入式系统中烧写uboot/bootloader/kernel 的一般方法,以及如果uboot或者内核出现错误, ...

  9. Delphi WebService 需要注意 转

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://gang4415.blog.51cto.com/225775/251997 Web ...

  10. 由链表初始化看C语言的二级指针

    先来看C语言创建链表.插入节点和遍历链表的一段代码: #include <stdio.h> #include <stdlib.h> typedef int ElemType; ...