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.
[Tex/LaTex] Create package with C/C++
package-writing
Related Solutions
I wrote my first LaTeX package (idxlayout
) about half a year ago (January 2010). I didn't know a comprehensive class/package writing guide then (and do not now), but I can offer a few hopefully useful hints:
- You may not need advanced TeX programming skills, but you should be familiar with the existing solutions (and their shortcomings) in the subject area you're interested in. Delve in the respective code of the LaTeX kernel, study the packages that may already exist.
- Try to straighten out the desired features of your package and their interdependencies before you write lots of code. Come up with intuitive names for options and user commands.
- Consider a key-value-interface for the options you're going to offer. There are quite a few support packages for key-value-syntax; I've used
kvoptions
. - You've already mentioned the importance of documentation. I'll add that writing a comprehensible user manual is necessary, but that your top priority should be documenting the intricacies of your source code - neglecting this will come to haunt you later.
- For distributing your package you may use the
dtx
-format which packs source code and documentation in one file. A good tutorial is Scott Pakins article.
EDIT: One year later and with version 0.1 of my second package (quoting
) published, I want to stress some other points. Note that I'm only a LaTeX "power user" and that my recommendations probably are self-evident for programmers.
As you develop your class/package, write an accompanying test document that allows for a quick overview if the (major) features (still) work as expected or if you broke something with your latest build. Whenever you stumble upon a new edge use case, amend your test document to include it.
Consider to use a version control system. I have kept copies of "major" development versions of my packages, but not used a full-fledged system like subversion, and do somehat regret it now. (I had programmed a small feature for
quoting
, then dismissed it within the same "major" package version, and would now very much like to reuse the code.)
Let me provide a simple (but full) example:
\ProvidesPackage{myemph}[2011/03/12 v1.0 a test package]
\providecommand\my@emphstyle{\em}
% Note that the argument must be expandable,
% or use xkvltxp package before \documentclass (see manual of xkeyval)
\RequirePackage{xkeyval}
\DeclareOptionX{style}{%
\def\my@emphstyle{\csname my@style@#1\endcsname}}
% predefined styles
\providecommand\my@style@default{\em}
\providecommand\my@style@bold{\bfseries}
\ProcessOptionsX
% For simple key-value commands, keyval would suffie
\define@key{myemph}{code}{%
\def\my@emphstyle{#1}}
\define@key{myemph}{style}{%
\def\my@emphstyle{\csname my@style@#1\endcsname}}
\newcommand\setemph[1]{%
\setkeys{myemph}{#1}}
\renewcommand\emph[1]{%
{\my@emphstyle #1}}
\endinput
Test file:
\documentclass{article}
\usepackage[style=default]{myemph}
\begin{document}
Something \emph{important}
\setemph{style=bold}
Something \emph{important}
\setemph{code=\Large\sffamily}
Something \emph{important}
\end{document}
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 withpdflatex
orxelatex
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).