[Tex/LaTex] Create package with C/C++

package-writing

How can I build a new LaTeX package written in C or C++? I'd like to add some new commands to LaTeX, but LaTeX is not for advanced programming, so I should write my commands in another language.

Best Answer

You can't add new commands to LaTeX using another programming language directly. You could patch the underlying TeX compiler which is written in a programming language called WEB, which is AFAIK translated to C before it is compiled. However, all people using your package would then need to use your special version of TeX, which is unlikely to happen.

Have a look at e-TeX which already extended TeX. You see that this isn't something trivial which is done to add a few commands for a package.

LuaTeX would allow you to use Lua code inside LaTeX macros. This again, would force people to use lualatex and not work with pdflatex or xelatex etc.

There are also tries to use other programming language by calling them external using the shell escape option of (La)TeX. perltex does this with Perl. In theory it might be possible to write some tool in C/C++ which is called from inside a LaTeX document and returns the result (e.g. in form of an input file), but this is mostly cumbersome.

In general, if you looking to implement some advanced things like OO programming etc. in LaTeX you probably see the problem or LaTeX from the wrong perspective. It is possible to implement very complex things in TeX (see e.g. PGF/TikZ). The macro way to program things needs to get used to but can be very powerful (but also challenging).

Related Question