# Tag Info

130

Being aware of the fact that this post is quite old, I would like to add two further resources which might be interesting when it comes to programming. I think there are three categories which need to be mastered (perhaps not all in the same degree) in order to become comfortable around TeX programming: TeX programming. That's very basic, it deals with ...

98

My not-oft-enough-expressed opinion is that TeX is a terrible programming language and that anyone learning to program is well advised to avoid learning anything from it. In fact, it is much more helpful to have experience programming in order to write TeX than the opposite. Various points major and minor come to mind: When writing TeX, one has to pay ...

63

(I guess I could be called a member of one of the teams ;-) this is my view) I thought of staying out of this debate, but perhaps some words of clarification or, let's say, some thoughts are in order after all. LaTeX3 versus pure Lua First of all this is the wrong question imho: LaTeX3 has different goals to LuaTeX and those goals may well be still a ...

52

You could start with LaTeX2ε for class and package writers aka clsguide. We already have some information regarding this on the site, for example: Style/class tutorials Is there a comprehensive and complete LaTeX reference? You could learn much if you would read the commentes LaTeX2ε sources aka source2e. To understand it best, you could learn TeX or ...

46

Use the right tool for the job: \documentclass{article} \usepackage{python} \begin{document} \begin{python} import itertools for p in itertools.permutations("suvat"): print ''.join(p) \end{python} \end{document} Run it with pdflatex -shell-escape.

39

This question is about several things at once, which makes answering it somewhat interesting. others have taken one tack, I'll take a different one. First, bear in mind that TeX is a macro expansion language, not a functional language. Secondly, note that TeX has very few built-in variable types. A lot of 'variables' are therefore macros with appropriate ...

33

Here's a better version, with lots of comments! Incidentally, the method of generating all permutations is based on the Knuth shuffle. The latest version of the code avoids nested loops and the separator is a \par token every now and again to avoid building too large boxes (see discussion in comments). I ran it with abcdefghij and it worked, although the ...

33

(This is another long comment along the lines of Aditya's answer.) Yes, it can help you understand other languages (in a theoretical sense). And for all the reasons that Ryan gives. In mathematics, we're always on the look-out for the counterexample, the outlier, the slightly-odd-thing-that-still-satisfies-the-definition. Indeed, we have whole books ...

32

This is more of a long comment rather than an answer. TeX is an excellent source of learning programming. It tells you how not to write code! One of the basic tenets of programming is that you should write code that is easy to read by others. I find that a lot of TeX code, especially TeX macro code written during the early days of TeX, is impossible to ...

32

Both Patrick and Frank have provided good answers: I'd like to add a few complementary points which seem too long for comments. One aspect of expl3 that is important is that it can be used now to program LaTeX2e packages (the 'l3in2e' approach). That is seen for example in my own siunitx package, which I would not want to make LuaTeX-only. A particularly ...

30


30

Ok, here's my approach. The basic idea is to typeset both columns as one box which is split afterwards. The outer form of the text is predefined as one big \parshape construct. The algorithm in more detail: Measure the picture to be included and calculate the number of lines it will occupy, as well as the vertical position where it will be placed. From ...

29

I ran your code but it appeared to be very slow, I suspect from all the \pgfmathtruncatemacro. But here we can do all calculations with \numexpr easily. This code is based on the TeX primitives \ifnum, \ifcase and \csname..\endcsname. I have used \foreach loops in the first two code samples as I wanted to stay close to your original framework. In the third ...

28

TeX is written in WEB (a literate programming language created by D. E. Knuth). Originally, WEB source was translated into Pascal (a subset of Pascal) via tangle to produce executable and translated into TeX via weave to produce documentation of the code. Both programs have themselves been written in WEB. Today, TeXLive uses web2c to translate directly WEB ...

28

You can use the fmtcount package to achieve that: \documentclass{minimal} \usepackage{fmtcount} \begin{document} 4: \numberstringnum{4} 31: \numberstringnum{31} \end{document} Use \Numberstringnum and \NUMBERstringnum respectively for capitalized and full-caps versions.

28

The \permute macro is plain TeX, so it's very portable. Why don't you want it cryptic? It does the job, cryptically! % New simplified code -- less cryptic than before :-) \documentclass{article} \def\recurse#1#2#3#4\endmarker{% \dopermute{#1#3}#2#4\endmarker \ifx\relax#4\relax\else % add a \par before \else if there are more than 8 items ...

28

The \\ command is one of the most overloaded commands of LaTeX, i.e., its actual definition depends on the place where it is used. According to the the LaTeX manual by Leslie Lamport (which should be considered the source of truth here) its general definition is \\ produce an explicit line break (or indicate the end of a row etc) \\* produce line ...

28

TeX was rewritten in Pascal. Actually, using a literate programming system called WEB, invented by Knuth himself. This system uses Pascal for the programming an TeX for the documentation. The source code of TeX was published as a book: http://www.amazon.com/Computers-Typesetting-B-TeX-Program/dp/0201134373.

28

Let's look at three examples from latex.ltx (the LaTeX kernel). \@namedef \def\@namedef#1{\expandafter\def\csname #1\endcsname} This is entirely analogous to the \expandafter\newcommand that's the main object of the question. With \@namedef{foo} we get \expandafter\def\csname foo\endcsname and the control sequence \foo is built before \def comes into ...

28

NOTE: Real-world application added at end of post! RE-REVISED to re-extend capability (and significantly condense the coding), as detailed here: The following macros are introduced for left-aligned acrostics. Macros for mid-line acrostics are described later in this post. \RAWacrostic{acrostic} - to view acrostic in plain, unaltered mode \SHOWacrostic[# ...

27

The TeXBook (by Knuth himself) is actually great! It starts with examples and explains clearly all the things starting from the simplest things that you're most likely to need, and goes sufficiently deep. It is a strange book in that it needs to be read in multiple passes, but you can do it. It's worth reading, especially since it also gives you insights ...

26

One of the ideas of expl3 is to provide a number of standard programming tools, and data structures are included in this. Currently, the structured data types available are sequences, stacks and property lists. Sequences and stacks have the same underlying structure: they are different ways of looking at the same thing. So we might have \seq_new:N \l_my_seq ...

26

The short answer to this is that you need \expandafter whenever you need something expanded before the previous token (helpful, I know!). In Peter's example, the code was \expandafter\newcommand \csname name#1\endcsname{\emph{#2}} (where #1 and #2 were arguments to some surrounding macro). The reason you want \expandafter here is because the entire ...

24

If your command is truly a single macro without arguments, i.e. \cmd, then it gobbles spaces written after it anyway. To ensure that any command will absorb following spaces, just make the last thing to appear in it \ignorespaces, which causes TeX to consciously discard space characters until the first non-space appears. By the way: {\cmd}sometext does not ...

24

(I am not a member of either team, this is my view.) LaTeX3 is not only about providing a better output routine, but also about providing programming tools such as the famous quicksort algorithm (see l3prg.sty). This is obviously nicer to write in Lua (for most of us). Or consider string manipulation / regular expressions. This could be handled in Lua as ...

23

There are several ways to do it using different programming facilities of different packages. Here's what it looks like using the LaTeX2e kernel command \@for to map over a comma-separated list: \makeatletter \newcommand\MakeMathOperators[1]{% \@for\@ii:=#1\do{% \expandafter\DeclareMathOperator\@ii }% } \makeatother \MakeMathOperators{ \rep {Rep} ...

23

I've just found this paper, which seems to address some of the issues I want: 25 Years of TeX and Metafont: Looking back and looking forward — TUG 2003 keynote address by Nelson Beebe [TUGboat 25:1, 2004] Abstract: TeX has lasted longer than many other computer software technologies. This article reviews some of the history of TeX and ...

23

E-TeX: Guidelines for Future TeX Extensions by Frank Mittelbach [TUGboat 11:3, 1990] Abstract: With the announcement of TeX 3.0, Don Knuth acknowledged the need of the (ever growing) TeX community for an even better system. But at the same time, he made it clear, that he will not get involved in any further enhancements that would change The ...

23

Not to compete with @egreg's answer but to answer your comment on when not to use \expandafter. consider \def\foo{hello} which defines \foo to be hello. So that is safe and good. \expandafter\def\foo{hello} may not be wrong (but it may be, and often is). If it's the common case that \foo is not yet defined then you will get the error ! Undefined ...

22

It was when the code was written, but is not now (in my opinion). The current LaTeX2e kernel was released in 1992 and carries forward a lot of material from LaTeX2.09. Even with these optimisations and the old 'autoload' system, there were a lot of systems that LaTeX was too big for on release. So looked at in the early 1990s this was entirely sensible. I'd ...

Only top voted, non community-wiki answers of a minimum length are eligible