Deyan Ginev edited section_LaTeX_Programming_Only_if__.tex  almost 9 years ago

Commit id: 47e62146482019b5b8fbb38bc660ff226b4b3a21

deletions | additions      

       

\section{{\LaTeX} Programming? Only if you really have to...}  \subsection{Generating a multiplication table}  To illustrate why you should \textbf{avoid} doing any low-level {\TeX} programming yourself\footnote{Unless you really know what you are doing, and you really must}} must}  here is an example of the hoops you need to jump through to typeset the multiplication table from one to five: \begin{lstlisting}  % We realize our loop variables via TeX counters  \newcounter{a}  \newcounter{b}  \newcount\ab  % And store the rows in a special token variable, to avoid typesetting troubles with tabular  \newtoks\allrows  % The darkest black magic is needed to make \addrow update the \allrows variable  % while also 1) keeping it global and 2) fetching the actual values from the TeX counters.  % Doing this "the right way" is neither intuitive nor easy.  \newcommand\addrow[3]{%  \begingroup\edef\container{\endgroup  \noexpand\global\noexpand\allrows{\the\allrows  #1 & #2 & #3\noexpand\\}}\container}  % Some simple macros for initializing and printing our tokens variable  \newcommand*\resetrows{\global\allrows{}}  \newcommand*\printrows{\the\allrows}  % We can now initialize and generate our table rows  \resetrows  \setcounter{a}{0}  % For A from 1 to 5  \loop\ifnum\thea<5  \stepcounter{a}  \setcounter{b}{0}  % and B from 1 to 9  {\loop \ifnum\theb<10 \ifnum\theb<9  \stepcounter{b}  \ab=\thea  % No shock, \ab is a times b  \multiply\ab by \theb  % Prepare the table row  \addrow{$\thea$}{$\theb$}{$\the\ab$}  % and loop  \repeat}  % We add a horizontal line to separate the different A blocks  % notice that even doing that properly requires "black magic"  % in order to expand \hline at the right place and time  \expandafter\global\expandafter\allrows\expandafter{\the\allrows\hline}  \repeat  % Finally, show our work, by writing a table with minimal markup:  \begin{tabular}{|rrr|}  \hline $A$ & $B$ & $A\times B$ \\ \hline  \printrows 

\loop\ifnum\thea<5  \stepcounter{a}  \setcounter{b}{0}  {\loop \ifnum\theb<10 \ifnum\theb<9  \stepcounter{b}  \ab=\thea  \multiply\ab by \theb