Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

There is a package I'm trying to modify that essentially displays a grid. It uses counters to increment the distance. I want to change the distance each step instead of adding a constant value. In fact, I simply want to multiply the current distance by a constant.

Anyways, I'm confused about how to use counters in TeX, compute things, and mixing LaTeX. When I try to use \setcounter or \newcounter in the package I get errors about redefinitions.

I've tried to use the calc package but cannot get it to mingle with the text.

The code

\advance\pos by 1

which instead of advancing it by 1 I want to advance it by 1, then 0.5, 0.25, etc... (it is in a loop)

I've even tried to advance it by a constant 0.5 and it gives an error. (as if one can only advance by integer amounts)

What I really want is some tutorial or guidance on what a package is and how to go about writing one. Is it TeX only and nothing special (I don't know much about TeX but have used LaTeX for typesetting for a several years)? Is there some special thing about packages? Can one mix TeX and LaTeX in a package? (doesn't seem so)

What I really need to know is how to do simple programmatical things like loops, variables, calculations, etc...

In the package I'm trying to modify I see things like \@whilenum, \multiply, \divide, etc. Which seem to correspond to the things I'm interested in but I can't find any references on the syntax or simple examples. Typing those into Google just returns a bunch of random packages that use them.

What I need to accomplish now is two things. I need to "advance" a counter by a geometrical progression instead of arithmetical (multiply instead of add) and I need to linearly interpolate between two points instead of using a constant.

In fact, in case anyone is interested, the package I'm trying to modify is


What I want to do is have the fret lines drawn like a real fretboard (or approximately). Make the width of the fretboard start lower and gradually increase (it should be independent of the number of frets). Allow one to color the dots and add fret markers (3, 5, 7, 9, 12,.... frets).

I'd rather learn to write my own. Would be a piece of cake in something like C/C++ but I have no idea about TeX.

share|improve this question
the \pos in this package is defined as a count register, which can take only integer values. what you need is a dimen register. dimen registers can be manipulated with \multiply and \divide so that shouldn't be a problem. but converting all the registers in this package from count to dimen is probably going to be somewhat daunting. –  barbara beeton Mar 15 '12 at 21:01
I don't need to change everything. Just hte positions from integer locations to floating point. I have already created a function that calculates the positions for me. The \put macro just needs to use it instead of the counter it currently uses. –  Uiy Mar 15 '12 at 22:58
Positions are usually done in dimensions, not counts. Dimensions can take fp values in pt, although internally they are integers in sp = 'very small distances'. So for example \newdimen\mydimen then \advance\mydimen0.5pt . –  Joseph Wright Mar 16 '12 at 8:47

2 Answers 2

You can start learning a bit about TeX programming by reading Notes On Programming in TEX by Dr. Christian Feuersänger. It is an excellent introductory guide. I then suggest you move on to Knuth's TeXbook.

Would be a piece of cake to modify the code after you read and master these guides or you can narrow your questions to a point that someone can help you more easily.

The package you are trying to modify is still using commands such as \advance and \multiply which are TeX primitives. With current engines this can be done easier using dimexpr etc.

share|improve this answer
This doesn't seem to be very useful. The commands you give are integer only. I used the fp package to write a macro to do my calculations. Kinda hard to believe there is no parser for simple mathematical expressions... –  Uiy Mar 15 '12 at 23:00
@Uiy This wonderful system that Knuth designed was all with integer arithmetic. Dimensions can be decimals i.e. 2.75pt etc. You can do calcs with this and then strip the point or as you did use the fp package. However, \advance etc are basic TeX primitives for integers. (Have a look at the fp.sty file). –  Yiannis Lazarides Mar 15 '12 at 23:14
I don't feel it is so wonderful when it takes 100 times more code to do something that can be expressed as single simple expression ;) The typesetting is awesome but the method to achieve it was not. –  Uiy Mar 15 '12 at 23:26
@Uiy re C#LaTeX.NET, well, feel free to implement that. People may use it. Perhaps. –  Bruno Le Floch Mar 21 '12 at 22:05
up vote 1 down vote accepted

This answer is actually what I ended up doing because it so much easier. USE LuaLaTeX!! You can write a package in lua and get into a real programming atmosphere and not deal with all the crap TeX has to go through to do simple things. To compute a simple function may take 20+ lines in TeX using FP while it takes only 1 in lua.

  • TeX wasn't designed as a programming language and LuaLaTeX adds a true programming aspect to TeX(not hacks).
  • It is much faster (about 10 to 100 times in my experience)
  • Easier to maintain (if not simply because of readability
  • Easier to debug(you can create a library in lua that you can debug outside of TeX in a lua ide and get exact errors)

The biggest drawback to luatex is simply that your tied in to using it and dependent on the developers of LuaLaTeX and Lua. This is not that big a deal though.

What LuaLaTeX does is allow you to combine typesetting in a typesetting language(TeX) with a programming language(Lua). i.e., you get the best of both worlds.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.