Take the 2-minute tour ×

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.

share|improve this question
What is it so advanced that cannot be written in TeX or LaTeX can you give an example? –  Yiannis Lazarides Nov 6 '11 at 11:52
How do you calculate sin 12.3 with LaTeX commands? OK, maybe it's possible to do this. But programming in LaTeX is very uncomfortable. –  balping Nov 6 '11 at 12:09
@balping: I think you may be surprised by how many things have already been realised in LaTeX. For instance, pgfmath, which is part of pgf includes quite an extensive collection of math functions that can be used directly in LaTeX. –  Jake Nov 6 '11 at 12:15
@balping: use LuaTeX and then you can program nearly everything in Lua –  Herbert Nov 6 '11 at 12:16
@balping: \usepackage{pgfmath} … \pgfmathparse{sin(12.3)} \pgfmathresult. Almost everything (reasonable) you can think of is already implemented, you just have to find the right package. Also note that LaTeX is a typesetting system in the first place, not a general purpose programming language. –  Andrey Vihrov Nov 6 '11 at 12:19

1 Answer 1

up vote 11 down vote accepted

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).

share|improve this answer
Isn't WEB translated in Pascal? I think you mean CWEB and then you shouldn't call it TeX. –  Yiannis Lazarides Nov 6 '11 at 11:50
@YiannisLazarides: Yes, WEB is AFAIK Pascal, but there is web2c. I added this link now to my post. –  Martin Scharrer Nov 6 '11 at 11:53
and don't forget WEAVE! –  Yiannis Lazarides Nov 6 '11 at 11:56
Sad , but understandable. Thanks for your answer –  balping Nov 6 '11 at 11:58
@MartinScharrer: I think in relation to tikz/pgf you can mention postscript as a standalone language. –  Marco Daniel Nov 6 '11 at 12:05

Your Answer


