Can you iterate in LaTeX? - latex

I'm new to LaTeX and I must say that I am really struggling with it. I discovered the \newcommand command that is kind of like a function/method in regular programming languages. You can give it arguments and everything.
I was wondering though, can I somehow iterate in LaTeX? Basically, what I would like to do is create a table with N+1 columns where the first row just contains a blank cell and then the numbers 1, 2, ..., N in the other columns. I only want to give N as an argument to this 'function' (newcommand).
Here is an example of something that might look like what I'm looking for (although obviously this won't work):
\newcommand{\mytable}[2]{
\begin{tabular}{l|*{#1}{c|}} % table with first argument+1 columns
for(int i = 1; i <= #1; i++) "& i" % 'output' numbers in different columns
\\\hline
letters & #2 % second argument should contain actual content for row
\\\hline
\end{tabular}
}
Call it with:
\mytable{3}{a & b & c}
Output should be:
| 1 | 2 | 3 |
--------+---+---+---+
letters | a | b | c |
--------+---+---+---+
Does anyone know if something like this is possible?
Thanks!

Just make the following into a new command and be sure to use package ifthen.
\begin{tabular}{l|*{10}{c|}}
\newcounter{count}
\whiledo{\value{count}<10}{
\ifthenelse{\value{count}=0}{}{\the\value{count}}
\ifthenelse{\value{count}<9}{&}{\\}
\stepcounter{count}
}
letters&a&b&c&d&e&f&g&h&i\\
\end{tabular}

Auntie Google says yes.

You can use the \loop or \repeat tokens. Or the multido package.

Sure it's possible. You can also recur. eplain has iteration macros in it, see, eg, here.

Another possibility (if you're lazy like me) is perltex

Related

lua pattern matching: delimited captures

I am trying to parse a string such as: &1 first &2 second &4 fourth \\, and from it to build a table
t = {1=first, 2=second, 4=fourth}
I'm not very experienced with regex in general so my naive try (disregarding the \\ and table parts for the moment) was
local s = [[&1 first &2 second &4 fourth \\]]
for k,v in string.gmatch(s, "&(%d+)(.-)&") do
print("k = "..k..", v = "..v)
end
which gives only the first captured pair when I was expecting to see two captured pairs. I've done some reading and found the lpeg library, but it's massively unfamiliar to me. Is lpeg needed here? Could anyone explain my error?
&(%d+)(.-)& matches &1 first &
Leaving 2 second &4 fourth \\ to be matched on
Your pattern does not match any further items
If you know that the values are one word, this should work:
string.gmatch(s, "&(%d+)%s+([^%s&]+)")
Take "&", followed by 1 or more digits (captured), followed by one or more space and then one or more non-space, non-& characters (captured).

wide and long table in latex

I am using a wide and long table. I am not sure how to put it in my thesis. Since it is wide, sidewaystable may be the choice. Meanwhile it is also too long to fit in one page, so longtable comes into my mind. However, I cannot make sidewaystable and longtable working together for one table, e.g.
\begin{sidewaystable}
\begin{longtable}{| c ||c| c| c |c| c|| c |c| c|c|c| }
\caption{A glance of images.}
\centering
% table content
\end{longtable}
\end{sidewaystable}
What shall I do?
Use lscape package with longtable or supertabular.
use a p{width} in place of l,r, or c in your column declaration, like this:
\begin{longtable}{ | p{0.2\textwidth} | p{0.5\textwidth | }
Then, to keep your content centered, add a >{\centering} before the p{width}, like this:
\begin{longtable}{ | >{\centering}p{0.2\textwidth} | >{\centering}p{0.5\textwidth | }

How do I change the font size of a table column?

I have a LaTeX table and would like the third column to be a different font size (smaller) than the others. I've always done this with a special-purpose macro that takes a parameter for each column and executes a font change for one of the columns. Is there an easier way to do this?
Use the package array and specify the font just after the \begin{tabular}, e.g.:
\usepackage{array}
...
\begin{tabular}{|>{\small}c|>{\Huge}c}
a & b \\
c & d
\end{tabular}
this makes the first column font small and the second Huge. >{decl} is used before l, r, c, p, m, or b and inserts decl directly in from of each entry of the column.
You can find further details of this package (e.g. the >{decl} to put decl after each entry) here.

LaTeX: How to adjust how the Sum symbol turns out

Not sure if LaTeX counts as programming, or if my question even makes sense, but I have this LaTeX expression (or what you call it):
\sum_{k=1}^n k^2 = 1+4+9+\ldots +n^2 =
\frac{1}{3}n^3 + \frac{1}{2}n^2 + \frac{1}{6}n
The problem is that the k=1 and n comes next to, i.e. after, the sum symbol, instead of above and below it. Is there a way I can change this? I have tried to show visually what I mean below. The sum symbol represented as Xs :
n
XXX XXX n
XXX vs XXX
XXX XXX k=1
k=1
I want the first kind, but am getting the second.
Try
\sum\limits_{k=1}^n k^2
if you want the sum limits to appear above and below the sum sign in an inline equation.
A more general solution to force a formula that is appearing in inline style to appear in display style is to start the formula with a \displaystyle declaration, e.g.
$\displaystyle \sum_{k=1}^n k^2$
This will work for any expression that appears differently in inline and display environments, such as \frac, \int, \lim, etc. You can also control the scope of the \displaystyle command by enclosing the desired expression in braces. For example, if you want the sum in your example formula to appear in display style but not the fractions, you could use
${\displaystyle \sum_{k=1}^n k^2} = 1+4+9+\ldots +n^2 =
\frac{1}{3}n^3 + \frac{1}{2}n^2 + \frac{1}{6}n$
I think you will need to use the equation environment for that:
\begin{equation}
\sum_{k=1}^n k^2 = 1+4+9+\ldots +n^2 = \frac{1}{3}n^3 + \frac{1}{2}n^2 + \frac{1}{6}n
\end{equation}

aligning equations in latex

I have the following code in an attempt to align things in latex using amsmath package:
\begin{alignat}{3}
\text{max} && c^Tx &=\\
\text{s.t.} && Ax &\leq b \\
&& x &\geq 0
\end{alignat}
Basically, i would like for max and s.t. to be in one column, c^Tx, Ax, x to be in second column, and lastly b and 0 to be in the last column. I'm not sure why it doesn't work (it clumps max and c^Tx together for some reason.
if anyone can help me out with this it would be much appreciated!
With math-mode \text{} you should put in some explicit whitespace such as \quad. But max smells like a log-like symbol so you should be using pre-defined \max or self-defined \operatorname{max} instead of \text{max}.
Additionally, the parameter for the alignat environment should be 2 in this case. The param is the number of alignment structures and can be calculated by solving n from a=2n-1 where a is the number of ampersands on a row. However, it doesn't seem to have a difference in this case.
I believe what's happening here is that max and s.t. are being right-aligned, which is running them right up next to c^Tx and Ax. If you just add some whitespace to the right-hand-side of max and s.t., you should be in business.
(Also, laalto is right, you should definitely use \operatorname{max} if max is some sort of operator or function. Though I'm not really sure what you're doing, so maybe it isn't.)

Resources