When someone is writing a book that contains code snippets, the question of (automatically) keeping those in sync quickly becomes very imporant. There's already lots of different solutions to this problem (every author has probably it's own), here's yet another one for C# that we've developed to author the Pex documentation.
Goals
A couple things that we wanted to acheive with this tool:
'#region' based solution
This solution uses the #region directive to define a snippet. The region describe contains the snippet name, which will be used to dump it into a file. For example, given this piece of C#,
...#region snippet StackExamplePart3stack.Push(new object);#endregion...
Our parse will extract the code in the region and write it to StackExamplePart3.tex, which gets pulled in our LaTeX scripts.
\begin{verbatim}stack.Push(new object);\end{verbatim}
That's it?
Yes, you can author snippets that stay compilable and up to date:
#region snippet UnitTest#region snippet DeclaringUnitTest[TestMethod]void Test(int i)#endregion{ }#endregion
Page rendered at Thursday, December 04, 2008 8:21:49 AM UTC
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.