# How to customize Tex/latex commands to perform system calls from shared libraries / dlls

Alright - let me make this simple and straight:

How can I take advantage of the tex/latex parsing mechanism in a custom c/c++ application that need scripting/parsing support?

I see two ways: one is extracting the parsing code and placing it in the custom application and go from there, or the second way of - tex/latex being extensible, write a new document style with commands defined to be place holders for the C/C++ calls.

The background is like this: I have this CFugue music note parser that parses the music notes and plays them on MIDI. The basic note parser works fine in that CFugue. But, we need to add support for users to be able to define and extend their own commands (just like tex/latex allows its users). The other alternative scripting mechanisms - I dont like them. Nothing comes close to the simplicity and extensibility of this tex style of scripting.

So, instead of reinventing the wheel, I want to reuse the existing tex framework so that the commands when parsed and executed, instead of computing layouts for paper or book, they do something else that I want (may be play a midi command on a device, or write midi note to a file on disk).

In this sense, I am not sure the tex/latex code is modular enough to be placed directly into third party applications and reuse it directly by configuring the output device. So, seems the second alternative of defining custom commands is the way.

For example, how to write a command that can load a shared library (dll on windows) and make a call to one of its methods?

``````\newcommand{Call_C_Method}[2]{\LoadSharedDll{#1}\GetProcAddress{#2}\SomeHowCalltheProc}

Call_C_Method{\usr\bin\shared.dll}{PlayMidi}
``````

I know this is not something tex/latex is designed for from its usage point - but, as an opensource developer and architect I would expect the tex/latex code-design certainly to be capable of doing this if designed correctly.

Is there any work already done / existing on these lines? Or no other way than to start digging into the tex code and see what can be done to redesign it to be modular enough to segregate the parsing from the input and output devices?

In other words, how to make the tex/latex generate not the postscript commands onto a file, but some other commands understandable by some other device?

Thanks.

-
TeX can certainly write out files with ASCII text, but beyond that I do not understand your question. Perhaps if you compose an example of what the TeX file should look like, and what the desired output is might clarify. See How can other programming languages and tools be used to create content for TeX documents? which seems to be the reverse of what you are looking for, but might be helpful to you. –  Peter Grill Mar 27 '12 at 16:30
Thanks Peter. As you said, we are looking for the reverse of what you pointed - and this question is mostly from the tex-engine-source code and design perspective so that tex-engine can be reused for purposes beyond typesetting. Anyway, I found the answer, at least close to that, and shared it for others here. Thank you all for sharing your inputs. –  Gopalakrishna Palem Mar 28 '12 at 5:39

Here is a slightly different approach.

`````` |application|->|texworks|->|TeX|->|writes to file|->|application reads files|
|executes .dlls etc     |
``````

You need to divide the problem into >30 year immutable parts (TeX and macros) and the ephemeral and ethereal parts that keep on changing (i.e., your application, .dlls, GUIs, Windows etc).

Your application calls an editor say TeXWorks. Your package is used to process a script composed of TeX macros and output whatever you want onto a file (operating system commands etc). Your application then further process these files.

Another approach is to use a browser GUI.

``````|Browser GUI|->|your application|->|TeX|->|writes to file|->|application reads files|
|executes .dlls etc     |
| feedback to browser   |
``````

The browser interface is used to get the user input (i.e the TeX part). It then sends info to server which then writes a file to be used in your application. Your application calls the TeX engine, which in turn parses the input and writes the output to a file. This is is further processed by your application. A browser interface is easier to write than other GUIs and although it means firing up some `localhost`, this is a very easy task to achieve. You can also easily design a chromeless browser application.

Current security models, make it almost impossible to activate a `.dll` via a TeX engine.

-
Thank you. Unfortunately, adding 3rd party dependencies (such as texworks etc.) is not an option, and your indication that security models make it impossible for dll activation removes that option too. Seems directly integrating with tex engine code to customize the parser reactions is the only way. Any existing work or pointers on how to customize tex engine behavior for customizing the parser behavior (so that custom events can be generated instead of generating dvi/ps output)? –  Gopalakrishna Palem Mar 24 '12 at 16:40
@GopalakrishnaPalem TeXworks is just an editor, based on Qt and open source, you can bring that code in your application if you wanted or use C++ to build one. The TeX code is in C, look under web2c in your distribution. If you customize it you cannot call it TeX. This is a requirement of the license by Knuth. With my suggested method you don't generate any dvi/ps output only write to text files. –  Yiannis Lazarides Mar 24 '12 at 16:44
Thank you Yiannis. I think I got almost - but can use some help on the statement: "Your package is used to process a script composed of TeX macros and output whatever you want onto a file" - how do I output my output from tex package? You mean use things like write18 etc...? I was of the idea that running Tex-engine on the TeX files inevitably ends up with some dvi files - but your sentence "my suggested method you don't generate any dvi/ps output only write to text files" is interesting - how does this happen? Any special commandline args? –  Gopalakrishna Palem Mar 25 '12 at 1:15

Ok. After hunting down the web for close to a week finally got something close to what I am looking for. Since I found the answer, I am just sharing it here for anyone looking for similar for my requirement: That is, a C++ Tex/Latex parser that can be customized to produce the output of their own.

I found this on the site: http://jblevins.org/log/xml-tools and the tool is named as: Tralics. The description there reads:

Tralics is written in C++ and also directly parses the LaTeX source (and it’s also extremely fast).

I was able to download and compile it on windows with mingw32 fine. And the code is in C++ and very small. Perfect for understanding and customizing it. Couple of listener based classes for custom output renders could be the best bet.

-