(How to)Windows Live Writer插入Latex公式
1、首先构建出如下窗体:
private void InitializeComponent() { this.buttonOK = new System.Windows.Forms.Button(); this.textBoxLaTeX = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.SuspendLayout(); ...}
2、在buttonOK_Click事件中构造要插入的代码:
public partial class LaTeXInput : Form { // Constructor public LaTeXInput() { InitializeComponent(); } // public properties public string ResultHTML { get { return m_result_gchart_html; } } // data members string m_result_gchart_html = ""; private void buttonOK_Click(object sender, EventArgs e) { // get original latex string, and encode it string raw_latex = textBoxLaTeX.Text; string encoded_str = HttpUtility.UrlEncode(raw_latex); string gchart_url = "http://chart.googleapis.com/chart?cht=tx&chl=" + encoded_str; m_result_gchart_html = String.Format("<p><img src=\"{0}\" /></p>", gchart_url); this.DialogResult = DialogResult.OK; } }
3、根据Windows Live Writer插件编写规范编写自己的插件类:
[WriterPluginAttribute( "24E0269C-D61B-11E0-A846-6D254824019B", "LaTeX Chart", Description = "A simple LaTeX render plugin for Windows Live Writer", HasEditableOptions = false, ImagePath = "formula_icon.jpg", PublisherUrl = "http://liselsia.org" )] [InsertableContentSourceAttribute("LaTeX Chart")] public class LaTeXPlugin : ContentSource { public override DialogResult CreateContent(IWin32Window dialogOwner, ref string content) { LaTeXInput input_form = new LaTeXInput(); if (DialogResult.OK == input_form.ShowDialog()) { content = input_form.ResultHTML; } input_form.Dispose(); return DialogResult.OK; } }
注:只要改动第二个ref传入的content参数,就能直接改变最终在Live Writer里要加入的HTML代码。而为了弹出如下的一个输入LaTeX的对话框,又需要添加一个Windows Form窗口,同时加上System.Windows.Forms.dll的.NET库的引用。然后以上述代码所示的input_form.ShowDialog()的方式弹出模态对话框(modal dialog)。