用于Live Writer的Tex转图片插件

mac2022-06-30  29

Technorati 标签: live writer, tex, latex, plugin

主要参考了两篇文章:

为Windows Live Writer开发插件——InsertSearchPageLink

为Windows Live Writer添加latex公式插件

代码与第二篇文章非常像(本来就那么点代码,想不像都难),大家就别说什么了。

如果想具体了解Live Writer插件开发强烈建议看看第一篇文章

代码基于.NET Framework 2.0,编译环境为Win7 + Visual Studio 2010 + Live Writer 14.0

效果图

公式:\sum\limits_{i=1}^{n}=\frac{n(n+1)}{2}

插件界面:

最终插入的图:

原理

使用Google Chart API,Google chart api显示latex的接口地址如下:http://chart.apis.google.com/chart?cht=tx&chl={latex}。{latex}部分就是显示要显示的公式的latex代码。

参考连接:

Google Chart Tools

Google Chart API 参考 中文版

主要步骤和代码

主要步骤:

首先新建一个窗体应用程序。然后把点击插件后要弹出的窗体做好并测试一下。将项目类型由窗体改为类库添加对WindowsLive.Writer.Api的引用删除Program.cs新建一个类,继承自ContentSource,并覆盖相应方法。

窗体部分代码:

InsertLatexForm namespace LiveWriterLatexPlugin { public partial class InsertLatexForm : Form { private static readonly string PicUrlPattern = @"http://chart.apis.google.com/chart?cht=tx&chl={0}"; private static readonly string ImgTag = "<img src=\"{0}\" alt=\"{1}\" />"; public InsertLatexForm() { InitializeComponent(); } private void button_PasteFromClip_Click(object sender, EventArgs e) { string content = Clipboard.GetText(); textBox_tex.Text = content; } private void textBox_tex_Validating(object sender, CancelEventArgs e) { TextBox tb = sender as TextBox; if (String.IsNullOrEmpty(tb.Text)) { errorProvider1.SetError(tb, "Tex公式不能为空!"); e.Cancel = true; } else { errorProvider1.Clear(); } } private void button_Preview_Click(object sender, EventArgs e) { pictureBox_Preview.ImageLocation = ImgUrl; } public string ImgUrl { get { return String.Format(PicUrlPattern, HttpUtility.UrlEncode(textBox_tex.Text)); } } public string ImgInTag { get { return String.Format(ImgTag, ImgUrl, textBox_tex.Text); } } private void button_Insert_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.OK; } private void button_Cancel_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.Cancel; } } }

接口类代码:

LiveWriterLatexPlugin[WriterPlugin("111B77FD-5C6E-469D-990E-9501393BC02C", "Latex", PublisherUrl = "http://hcoona.cnblogs.com", Description = "Insert latex", ImagePath="tex.png") ] [InsertableContentSource("Latex")] public class LiveWriterLatexPlugin : ContentSource { public override DialogResult CreateContent(IWin32Window dialogOwner, ref string content) { using(InsertLatexForm form = new InsertLatexForm()) { DialogResult result = form.ShowDialog(); content = form.ImgInTag; return result; } } }

WriterPlugin中的参数第一个为Plugin的ID,一般用GUID生成器生成一个就行了。

PublicsherUrl为发布者的主页地址。

Description就是插件的描述。

ImagePath为插件前的小图标的图像地址,需要注意的是,这个小图标必须嵌入到生成里面,即生成操作选择“嵌入的资源”。

小图标

图标生成操作

源代码和插件Release文件

如果只是想使用我提供的这个插件,下载这个文件后解压缩到C:\Program Files (x86)\Windows Live\Writer\Plugins下即可(大家自己找找路径啊,别真照着我这个路径解压,万一错了可别怪我)

如果想修改源代码,点击这里下载工程文件(Visual Studio 2010类库工程,.NET Framework 2.0),修改后需注明出处!

本作品采用知识共享署名-非商业性使用-相同方式共享 2.5 中国大陆许可协议进行许可。

转载于:https://www.cnblogs.com/HCOONa/archive/2010/07/05/tex2image-plugin-for-live-writer.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)