Some Experiences in the Display of Mathematical Equations in WPF

I recently needed to display ‘notes’ in a WPF application – the interesting detail was that the notes contained both text and mathematical equations. I did not find a tremendous amount of information about presenting mathematical equations in WPF – so I thought I would share my experiences.

The notes for the application were delivered by the client as a Microsoft Word document.

Because I do not have any experience with Adobe Illustrator or any of the Expression products none of the solutions I investigated involves those products.

 

jsMath – My first idea was to display the notes in a browser control with the hope of reusing them on the web. I quickly found jsMath which generates equations from TeX input (if formulas in TeX are not a familiar idea see Help:Displaying a Formula). Setup was quick and my test page looked nice in Firefox. However, the rendering in the WPF Browser control was unacceptable. It is completely possible that I did not have something configured correctly and/or that there were settings I needed to change, but I decided to move on…

 

MathML – As an alternative to having jsMath handle the equations I briefly looked into using MathML. Unfortunately this page failed to open correctly on my machine in IE8, Chrome, Safari and Opera. That was enough to steer me away from MathML. The page opened without problems in Firefox 3.5…

 

WPF Chromium WebBrowser control – I was not quite ready to give up on the idea of presenting the notes in a browser control with jsMath and thought the rendering in this control might be better. I did not get very far with this and did not try the control in my project – not because of problems with the control, but because the Awesomium project that this control uses is going closed source. I was unsure about the future of the WPF Chromium WebBrowser control without updated versions of Awesomium so I moved on to my next idea.

 

DocumentViewer -  I already had nicely formatted text and equations in the original Word document and thought that I might be able to leverage that. The WPF DocumentViewer control can display XPS documents – and Word documents can be saved as XPS. I saved the first note from the Word document as a .XPS file and – after figuring out that System.Windows.Xps.Packaging is in ReachFramework.dll – quickly put together a test. The file displayed quickly and easily (including equations) and for some applications this could be a great option!  However, I felt the DocumentViewer was too page and print focused for this application. I do not think – at least in it’s default configuration – that the DocumentViewer is a very good solution for displaying shorter items for easy on-screen reading (esp. in a smaller screen area). I am sure I could have tweaked options to improve the DocumentViewer for use in this application, but this seemed like the wrong approach.

 

Word/XPS converted to FlowDocument – I thought these articles were interesting, but was skeptical about the time/results, and did not try this approach – Converting FixedDocument (XPSDocument too) to FlowDocument and WordML to FlowDocument – how to convert docx files to WPF FlowDocument. I would be really curious to hear from anyone how the conversion of Word equations works with the ideas/code presented in these articles.

 

Flow Document  + Images – Images seem like an obvious/traditional choice for presenting the equations and the FlowDocument seemed like an interesting choice for presenting text and images for on-screen reading. One easy way to produce the images is Roger’s Online Equation Editor which transforms LaTeX equations into images. It is easy to work with images in WPF – but the problem I had was getting the images to look as sharp as the text in the FlowDocument when they were resized. If this was a document for printing, or had specific static layout requirements, I think that the images would have worked great. But as the images were resized (either because of dynamic layout changes or changes to the applications design) I was not able to keep them as sharp as the surrounding text. If I had more graphics expertise I may have been able to find a better image type/resolution/rescaling combination.

 

FlowDocument + Shapes – a good friend suggested that I look for ways to represent the equations as SVG. What I eventually found was Inkscape + textext. textext works as an add-in to Inkscape that allows you to render LaTeX. The install and troubleshooting steps near the bottom of the textext page are critical in finding the dependencies (MikTex, Pstoedit, Ghostscript) and dealing with errors (I had to add a directory to my path to get textext working). Once it is up and running textext worked great – it is very easy to create an equation and save it as an SVG file. The next problem is moving the equation from SVG into XAML.

 

SVG to XAML Canvas – All of the methods below can be used to convert an SVG file  into shapes in a XAML Canvas object. In the case of the equations I was producing all of the solutions below generally produced usable output. The Canvas output could be inserted directly into your application – or it could be placed in a resource file by stripping out the Name elements and adding a Key. In addition to using the Canvas directly it could also be used as a VisualBrush.

Xsl – I was able to use the xsl on this page to get canvas based output (and do a some xsl debugging in Visual Studio – a great feature and the first time I have used it).

Inkscape – Inkscape has save as XAML built-in. In the past, for simple images, this worked great for me. For the equations that I was producing in this project the canvas that Inkscape produced apparently lacked a RenderTransform that was crucial to correct display. The problem was fairly easy to fix, but mainly because I had the output from XamlTune as a comparison.

XamlTune – The command line executable in this project worked great – and could potentially be integrated into a build script which is a nice bonus. I had trouble with the gui portion of this project crashing.

The Canvas with shapes output is ok – but I want to reuse the same equation multiple times, use the same equations both as larger stand-alone figures and inline with the text and rescale the larger images as the application’s size changes. I assume that I could get the canvas based resources working for all of those requirements (although I not sure exactly how and imagine a bit of awkward code…). However, I know that having the equations as DrawingImages in a resource file would allow me to use the equations inside an Image tag, solving the problems above.

 

FlowDocument + DrawingImage – The only programs I found that produced DrawingImage output from SVG input were ViewerSvg Professional or Ab2d.ReaderSvg Professional (the ‘Basic’ versions are not listed as having ‘Export as Geometry’ support) from WPF Graphics (demo downloads available – note these will also convert from SVG to a Canvas). This software did nice conversions from SVG to DrawingImage with a good array of options. The Ab2d.ReaderSvg class library also includes a sample application that allows you to combine multiple SVGs into DrawingImages inside a ResourceDictionary, which is almost exactly what I wanted to do! For some situations these applications will not be appropriate since they are commercially licensed software with a cost of $169/$399.

 

Conclusion: The options above present several viable solutions for presenting mathematical equations in a WPF application. Every project will have different requirements that will dictate the best solution. With that said – in my opinion -  the combination of Inkscape and textext to create equations in SVG and Ab2d.ReaderSvg Professional to convert the SVG into DrawingImage resources is the best combination currently available for presenting mathematical  equations for on-screen reading in a WPF application.

 

Enjoy!

CM

A beginner’s view – Textual DSL with OSLO

For just over a month I have been working on a project at work that lets users select inventory items from a database using a textual DSL. This is the first DSL I have ever produced! I thought that it was interesting that with no significant experience in building DSLs I was able to produce a useful application in a relatively short period of time. Below are the best resources I found and a few remarks:

 

Overview/Perspective: The Herding Code interview with Markus Völter (who you might know from Software Engineering Radio) is interesting – while not strictly focused on textual DSLs he gives a great overview/critique of Microsoft’s OSLO effort and interesting information on the Eclipse tools in this space (some recent news related to topics in the interview from Douglas Purdy and a short post by Markus). If you want to see some of the Eclipse tools he mentions the second link will take you to a nice presentation.

Useful MGrammar/OSLO focused podcast: This podcast features Shawn Wildermuth and has some great discussion around DSLs with a focus on OSLO:

Bits and samples: The Oslo Developer Center – official downloads and samples (I thought the MGrammar samples were good study material):

Focused start: This three part series by Shawn Wildermuth was incredibly valuable because the focus of the articles matched what I was trying to accomplish quite well:

Digging deeper: There is a good chance that you will need to dig into some of the details of MGraph and MGrammar – I found the following references useful:

Skeptical? The application I developed is currently being tested by a handful of users, so far these are the points I would make in support of the DSL:

  • Less dev time than you might guess: this is a small business app with a tight focus, it was coded by one dev working part-time on the app in about 4 weeks. There are always more features to implement, but the app is useful now with a fairly low investment.
  • Powerful/Flexible Searching: For advanced users the DSL exposes many more search fields/categories/options than I know how to expose via a reasonable list/wizard/combo box/text box style query build screen, and it is not nearly as tedious! The textual interface allows advanced users to be more efficient and creative and gives them access to more options than they had in the past.
  • Conventional UI feeds the DSL: I found the ‘conventional’ query build screen to be fast and pleasant to develop because it simply constructs the text to be parsed! This means that anything I build for the conventional interface needs to be supported by the DSL – this forces all features to remain ‘exposed’ in the textual interface for more advanced users.

Pain points:

  • I was surprised by the amount of work that I had to to to turn the AST produced by parsing the user’s input into usable objects/actions. There are some samples/posts/ideas out that can help build objects automatically – but this seems like an obvious area for Microsoft to enhance since we are talking about an all MS tool chain. I imagine that most scenarios are going to involve walking the tree to build/modify objects? Is there an approach or scenario that really does not do that?
  • The lack of a text editor component to put in your application in conjunction with your DSL is disappointing. There is an obvious need to deliver an editor for your DSL embedded in your app with syntax highlighting and keyword completion, if that is currently available I am not aware of it… While inputting text into a vanilla textbox may be a good test of the simplicity and logic of your DSL I doubt it is really the user experience you want. If a commercial product is an option then Actipro’s WPF SyntaxEditor might be interesting – they have MGrammar support which sounds encouraging, but I have not made time to test it out.

While the jury is still out if the application mentioned in this post will be successful I think that it was the right choice to build and try a DSL – hopefully if you are interested in trying out a textual DSL in .NET the links and information above will be interesting!

CM

2008!

My blog has been silent during almost all of 2008 and I thought it was time for an update!

Hikes/Outdoor adventures: In 2007 I put a number of my outdoor activities on this blog – it was fun, but I found over time that flickr sets were perfect for what I wanted to do: pictures with tags + a short description. 2008 Sets:

San Pedro, Thumb Canyon, Molino Area, Box Camp, Box Camp, Seven Cataracts, Lower Highway Drainages, Zion, Ridgeline, Palisades, Milagrosa, Molino, Seattle, Romero Pools, Seven Cataracts

Reading: In addition to some technical books and some sci-fi (nothing astounding  this year in sci-fi books for me) the highlights were: Richard Powers Echo Maker, David Mitchell Cloud Atlas and Number 9 Dream, Haruki Murakami Kafka on the Shore. All of those were really good and I am astounded I found all of those in a single year (suggestions welcome!) – if I had to pick a favorite I think it would be Cloud Atlas, if you pick it up and find yourself slightly underwhelmed 1/3 of the way in KEEP READING, trust me…

Music: Steinbruchel (and spelling variation…) and Vampire Weekend were the finds for me this year – very different but both amazing.

Programming: In 2008 I worked in C# and VB.NET on Linq to Sql/SqlMetal, Sql Server/Express User Instances and WPF/Composite Application Guidance. I am certainly not an expert in any of these technologies – but have deployed working applications that are useful to their users and great motivation to keep learning! I also worked thru the first 10 Euler problems in F#.

Classes: I have been taking classes at Pima Community College to keep learning, this year I completed Introduction to VB.NET, Web Publishing and Programming & Problem Solving II.

Bike Commuting: Another year! I feel really lucky to live in a spot where I can commute to work, to school and to most of my errands by bike – it is really a great part of my day! Favorite items that go with me: X61 Tablet (in the no longer available hi-res screen), Cradlepoint PHS300+ Verizon UM150 for instant internet hotspot, Tom Bihn Smart Alec with Vertical Brain Cell and Vertical Freudian Slip.

 

Happy Holidays!

Charles

Soldier Canyon – Prison Camp to Hairpin Turn

After quite a few days of rain this weekend finally brought drier weather, it seemed like an interesting chance to see Soldier Canyon full of water – so we set out on a hike from Prison Camp to Hairpin Turn via Soldier Canyon. If you stay in the water course there are a number of possible rappels – but this is also very fun canyon to do without technical gear and it is possible to see practically all of the canyon without any rappelling. The weather was cold and the canyon full of water so we bypassed optional rappels 1-3. Here is a quick comparison of the water flow between January of 2006 and December 2007:

2006 Soldier Canyon Boulder

2007 Soldier Canyon Boulder

This hike requires a car shuttle – the lower car should be parked in the pull-out along the right side of the road just before the first hairpin turn heading up the mountain (before the road crosses Soldier canyon). Climbers will know this as the parking area for the Hairpin Turn and Rivendale areas. After dropping off the car head for the the Gordon Hirabayashi Recreation Area (often called ‘Prison Camp’) where, just as in past hikes to this area (same hike Jan. 2006, partial version of this hike 2007), we parked in one of the pull outs along the loop at the end of the road.

Start on Soldier Trail which will quickly take you to Soldier Canyon – leave Soldier Trail as it heads up and left and follow the canyon. Bypass the first drops and falls on a climber’s trail on the right side of the canyon and continue hiking and scrambling into and down the canyon. The scrambling in this section is generally easy, but it is also easy to escape the canyon bottom to bypass any difficulties.

Eventually the canyon flattens out as you hike thru the area where Soldier Trail crosses the canyon several times. Soldier Trail leaves the canyon for the last time and climbs up to the cliffs on the left side of the canyon just before the canyon begins to narrow into another beautiful – and possibly technical – section.

As you work down the narrowing canyon bottom you will come to a short drop, optional rappel 1 (depending on water flow stronger climbers may bypass this rappel). Shortly down canyon you will come to optional rappel 2 – this is more easily down climbed than the previous drop but flowing water could make the climbing quite slick. Work past another short drop on the left side of the canyon and down to optional rappel 3 which takes you around a huge boulder that creates one of the most beautiful spots in the canyon. It is awkward – but possible – to bypass optional rappel 3 by working up onto the cliffs on the left side of the canyon. All of the optional rappels can be easily avoided by hiking along the cliffs above the canyon and thrashing back down into the canyon bottom.

After the giant boulder there is one more set of cliffs down canyon that form optional rappel 4 – when the cliffs are dry this could be a down climb for confident climbers (there are also options to escape and reenter to the canyon). Past these cliffs the canyon is a beautiful hike down to Hairpin Turn with a number of small scrambles and occasional route finding puzzles. Map.

Enjoy!
CM

Arizona Canyoneering Books

I updated a few of my posts today to note that there were alternate descriptions in the latest book about canyoneering in Arizona:

Arizona Technical Canyoneering – by Todd Martin (you may know his website – Todd’s Desert Hiking Guide – lots of good information and hike/trip reports)

The other book that is not to be missed in this category is:

Canyoneering Arizona
– by Tyler Williams (revised edition)

I have not done enough of the hikes in either book to offer a review – but I have had a few good adventures with Canyoneering Arizona and am already excited about some of the destinations in Arizona Technical Canyoneering.

Enjoy!
CM

Agua Caliente Canyon Loop Hike

Agua Caliente and Milagrosa canyons are beautiful destinations at the base of the Santa Catalina Mountains on the east side of Tucson. Interesting sections of both canyons can be enjoyed as loop hikes starting from the end of Suzenu Road – this weekend we decided to visit Agua Caliente Canyon. Map.

To get to the trail follow Snyder east – after Synder divides look for a left turn onto Suzenu Road. Park on the right side of the road before the T intersection. Start the hike by taking a right – thru the gate – and to the end of the road. At the end of the road continue past a gate (several ‘No Parking’ signs) and across a wash via an old road – after a few minutes (and heading downhill) look for the trail on your left (if you start to cross the wash you have gone too far).

Boulders jamming Agua Caliente Canyon

The trail has great views of the Santa Catalinas and the Agua Caliente Hill area – the trail also has very little shade and can be quite hot (be prepared!). It is fairly easy to follow the trail into the bottom of Agua Caliente canyon. The canyon bottom is unremarkable for the first minutes, but keep hiking and you will arrive at a rocky section with falls/drops/pools. There are several obstacles to bypass down canyon – these will likely require bushwhacking, scrambling and/or following faint trails on the loose/sandy/exposed/bushy side of the canyon (be very careful!). The canyon gradually mellows as you head towards the junction with Milagrosa canyon – we spotted an impressive beehive high on the cliffs during our hike. When you reach the junction work your way back up onto the trail and head back to the parking area.

Reflections of the canyon walls

Update! 2007-10-28 – For another description of Agua Caliente Canyon (and Milagrosa) see Arizona Technical Canyoneering.

Enjoy!
CM

Montrose Canyon

The Romero Canyon Trail in Santa Catalina State park can be a busy location – on a hot day you will undoubtedly pass groups of hikers focused on getting to the Romero Pools and enjoying the cool water. For much of the hike to the Romero Pools you can look down into Montrose Canyon. On Sunday we hiked up the Romero Canyon Trail until the last prominent saddle before the pools and bushwhacked/boulder hopped down the steep hillside into Montrose Canyon. Getting off the trail quickly separated us from the crowds and we took advantage of the quiet to have a nice nap in the shade beside the water. From where we entered the canyon traveling down is mostly easy hiking and scrambling – but we did come to several places where we had to exit the canyon to bypass obstacles. Eventually we ended up back at the Overlook to the Montrose Pools and returned to the car via the Romero Canyon Trail.

CM

Sweetwater Wetlands – Blackbirds!

We visited the Sweetwater Wetlands this weekend and had fun watching the flocks of Yellow-headed and Red-winged Blackbirds flying at dusk.

Sweetwater-Blackbirds

The Sweetwater Wetlands is part of Tucson’s water treatment facilities and the odor can be bit daunting at first. You will probably forget about the smell since the birding is fun and the habitat is different from many other Tucson area birding locations – highly recommended.

Yellow-headed Blackbird

Wesabe, PasswordSafe, hosted-projects.com, S3 Backup, HotSpotVPN

Last month I ordered a Lenovo X61 Tablet (originally due to arrive last week – now delayed…) and I immediately started a folder of software that I wanted to install on my new machine. As part of that process I also looked for new web applications that would work for me as desktop application replacements and thought about services I want to continue to use:

Wesabe
web address: www.wesabe.com
length of time used: just over a month
for me replaces: Microsoft Money

I have used Microsoft Money for about 7 years. I am a ‘light’ user of the product – no investment tracking or anything complicated – and it worked well for me but did not have any must-have/killer features.

When I saw Wesabe I was intrigued, Wesabe provides basic account tracking (including pulling information from your bank via a desktop app or (better) a Firefox plug-in), has clean/flexible tagging of transactions, supports adding attachments to transactions (such as a scan of your reciept), very basic reporting/exporting and a nice clean interface. In addition Wesabe adds a twist to financial management software by tying you into a community of users who can provide tips and join groups on topics such as saving money.

I have been using Wesabe for about 1 1/2 months now and for me it is has worked great – no glitches or problems, simple to learn and has the features that I need! It seems fairly clear to me that Wesabe is not intended to (at this point anyway) track a complex financial portfolio, but for tracking/tagging transactions on a basic set of accounts it works great.

PasswordSafe
web address: www.passwordsafe.com
length of time used: about two months
for me replaces: PINs

I have seen many suggestions about different ways to manage the insane number of online accounts (not to mention ‘offline’ numbers/combinations/pins/etc.) that almost everyone seems to accumulate – but none that are perfect. I found PINs over a year ago and liked the simplicity and small footprint, but occasionally wanted access to my passwords when I did not have my computer or USB drive handy…

For me PasswordSafe is a nice solution. PasswordSafe offers a number of features, for me the ability to store usernames/passwords and search my account to bring up the information I need is great. PasswordSafe has a desktop app that can pull down all of your information into a format that is available offline and a (beta) favorites link that will bring up a browser window with your PasswordSafe information for the current site. This service has worked great for me and, like Wesabe, the features match quite nicely with what I need.

hosted-projects.com
web address: www.hosted-projects.com
length of time used: just over a year
for me replaces: hosting a local subversion server

For the past couple of years I have been trying to continually improve my programming skills and have several personal projects that I have been working on – even working 99.9% by myself it is hard to imagine not using a version control system.

Subversion (with tortoisesvn) is the system I picked and I started by running a subversion server on my own system. This was a great way to get started, but I was nervous about losing data if I had computer problems.

So I found an alternative – hosted-projects.com; the price is good and they have plans that are appropriate for a wide variety of needs. I have never seen the service go down, they set up a trac site for each module you create and the management interface is easy to use. I do not have much experience with other version control/project management hosts, but for my uses hosted-projects.com has been perfect.

[Caffey left a great comment about CVSDude.com offering both CVS and SVN hosting. Looks like the basic plan at CVSDude is $1 less than hosted-projects.com with slightly different options.]

S3 Backup
web address: http://www.maluke.com/s3man/
length of time used: about 6 months
for me replaces: external hard drive backup

Large, inexpensive external hard-drives and multi-gig DVD formats have made personal backup easier, but unless you store the backups somewhere secure and in a different location from your computer how useful are they if there is a break-in, fire, flood or a need to restore while you are traveling?

I have been very interested in backing up to online storage and Amazon’s S3 service makes it possible to backup up the amount of information I have for a reasonable cost. I have been backing up with S3 Backup for about 6 months and it has worked well. The software is still in Beta – I have had occasional small glitches – but so far the benefit of having secure online backup has been worth any small problems.

Amazon S3 is new enough that I am not aware of any well established software packages for working with it – it will be interesting to see what other options emerge. I want to try Jungle Disk (recently out of Beta) at some point… I have also seen Mozy recommended numerous times for online backup – but I like the idea of not being tied into one vendor for backup/access software.

HotSpotVPN
web address: http://hotspotvpn.com/
length of time used: about 18 months
for me replaces: unsecured browsing over public wireless

A few episodes of Security Now! may induce enough fear about wireless security that you will start looking at VPN solutions. Some of my friends simply VPN to their home/desktop computers when on the road, but I do not have (and do not intend to have) anything other than my laptop. So the solution for me when using unsecured wireless hotspots has been HotSpotVPN. I have occasionally had trouble connecting and have trouble at some airports with the login system – but overall HotSpotVPN has been easy to use and the extra security has been worth the cost.

Enjoy,
CM

Flickr vs. Picasa Web Albums

I am moving my photos over to Flickr from Picasa Web Albums and gradually changing all of my links – I thought I would share my personal opinion on the two services as a very casual user:

Flickr
Good – More people and better community!!!
Bad – There is not a full featured desktop client for Flickr (that I know of…) like Picasa – for me organizing and tagging is slightly painful in the Flickr web interface (compared to using Picasa on the desktop). [Lance points out below that Picasa is still useful for tags and captions if you do those before you upload!]
If sharing and community features are important to you I think Flickr is the better service.

Picasa Web Albums
Good – Decent integration with Picasa makes it easy to organize/tag/caption pictures on your desktop and then transfer them to Picasa.
Bad – Picasa does not have the same level of community or number of members that Flickr has.
If your primary concern is online personal photo storage and occasionally sharing photos then the integration with Picasa makes this a strong choice.

Other ideas?
CM