BedroomLANTilde~A: Alexios' HomepageSearch |
![]() |
||
Pascal's Triangle for LaTeXYou need to display Pascal's Triangle in a LaTeX document and whenever you hear ‘ampersand’ or ‘smallskip’, you go into a homicidal rage, leaving behind you a trail of viscera and blood-stained Lion Book pages. Also, you're lazy. Put that chainsaw down, this recipe is for you. Although, really, with that kind of reaction to ampersands, what are you doing using the world's best Turing-complete typesetting software? SolutionThis recipe uses a small Python script to generate a Pascal triangle of arbitrary size in a import sys def pascal(n): """ Yield up to row ``n`` of Pascal's triangle, one row at a time. The first row is row 0. """ if not isinstance (n, int): raise TypeError ('n must be an integer') if n < 0: raise ValueError ('n must be an integer >= 0') def newrow(row): "Calculate a row of Pascal's triangle given the previous one." prev = 0 for x in row: yield prev + x prev = x yield 1 prevrow = [1] yield prevrow for x in xrange(n): prevrow = list(newrow(prevrow)) yield prevrow def pascal_latex(n, out=sys.stdout): """ Generate a Pascal triangle for LaTeX embedding. Sends output to the file-like object ``out`` (default: sys.stdout). """ out.write('\\begin{tabular}{r%s}\n' % ('c' * (2 * n + 1))) for i, row in enumerate(pascal(n)): out.write('$n=%d$:& ' % i) out.write(' & ' * (n - i)) out.write(' & & '.join ('%2d' % coeff for coeff in row)) out.write('\\\\\\noalign{\\smallskip\\smallskip}\n') out.write ('\\end{tabular}\n') if __name__ == '__main__': try: pascal_latex (int(sys.argv[1])) except ValueError: sys.stderr.write ('Please specify a positive integer.\n') sys.exit (1) UsageTurn this into an executable script (this depends on your operating system — on Unix, python some_filename.py 4If you made this into an executable, you can leave \begin{tabular}{rccccccccc} $n=0$:& & & & & 1\\\noalign{\smallskip\smallskip} $n=1$:& & & & 1 & & 1\\\noalign{\smallskip\smallskip} $n=2$:& & & 1 & & 2 & & 1\\\noalign{\smallskip\smallskip} $n=3$:& & 1 & & 3 & & 3 & & 1\\\noalign{\smallskip\smallskip} $n=4$:& 1 & & 4 & & 6 & & 4 & & 1\\\noalign{\smallskip\smallskip} \end{tabular} Which, of course, renders this:
You can place the LaTeX code fragment in a \input{pascals_triangle} Obviously, you can also cut and paste it. You can also call the triangle-generating routine from another Python program by saying DiscussionThe script is written in three, visibly distinct parts. The first part is the The second function, The rest of the function merely prints out consecutive rows of the triangle, prepending The third and smallest part of the script simply parses (without much checking) the command-line arguments, when the program is used as a script (not a module to be included in another Python program). |
|
|
|
Recent comments
24 weeks 6 days ago
32 weeks 5 days ago
42 weeks 4 days ago
43 weeks 5 days ago
44 weeks 6 days ago
46 weeks 4 days ago
47 weeks 13 hours ago
48 weeks 3 days ago
1 year 1 week ago
1 year 1 week ago