Hot answers tagged loops
28
The pgffor package allows for-loop style repetition using the following format:
\foreach \<cmd> in {1,...,<total>} {%
% stuff
}
Using this as base, the following is probably what you're after:
\documentclass{beamer}% http://ctan.org/pkg/beamer
\usepackage{graphicx}% http://ctan.org/pkg/graphicx
\usepackage{pgffor}% ...
27
Edit: To give some indications for this question (What's the maximum number of pages a PDF produced by pdfTeX can have?), I tried my second version with a limit of 2.500.000 pages. The PDF produced (in approximatively 30 minutes) contains 1112672090 bytes.
Here is a variation of your first solution (this document can't be terminated!):
...
25
Look, ma! No loops! :)
Borrowed from the TeXbook, exercise 21.8 (p. 225).
\documentclass{minimal}
\usepackage[a4paper,margin=2mm]{geometry}
\usepackage{graphicx}
\sbox0{%
\includegraphics{image}%
\kern2mm
\vrule width 0pt depth 2mm}
\setlength{\parindent}{0pt}
\begin{document}
\null
\cleaders\hbox to\textwidth{\leaders\copy0\hfill}\vfill
...
23
\documentclass{article}
\usepackage{graphicx}
\usepackage{pgffor}
\def\pdfappendix#1#2{%
\foreach \index in {1, ..., #2} {%
\includegraphics[scale=0.6]{#1\index.pdf}\par%
}}
\begin{document}
\pdfappendix{pdf/test}{4}
\end{document}
The above code will the thing you want. However I haven't tested it extensively. I just tested this minimal ...
21
How about using a chunk with <<results=tex, echo=FALSE>>=?
It will allow you to: (a) paste together the LaTeX code you'd like to run (i.e. 200 \begin{frame} ... \includegraphics ... \end{frame} constructs); and (b) place that constructed text verbatim in the *.tex file produced by Sweave(). For more details, see here. It's a FAQ!
...
17
A loop and a counter:
Code
\documentclass{article}
\makeatletter
\newcommand*{\length}[1]{%
\@tempcnta\z@
\@for\@tempa:=#1\do{\advance\@tempcnta\@ne}%
The length of the list #1 is \the\@tempcnta.%
}
\makeatother
\begin{document}
\length{0,1,54,1,3}
\def\mylist{0,1,54,1,3}
\length\mylist
\end{document}
Output
The length of the list ...
17
My first LaTeX3 answer! Yay! :)
The l3clist package has a lot of built-in commands to deal with comma-separated lists. Here's an attempt:
\documentclass{article}
\usepackage{expl3}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand \countItems { m } {
\clist_count:N #1
}
\NewDocumentCommand \countInlineItems { m } {
\clist_count:n {#1}
}
...
16
One way of doing this is using leaders:
\documentclass{article}
\newcommand\kitty{\leavevmode\xleaders\hbox{kitty!}\hfill\kern0pt}
\begin{document}
This is a test. \kitty \par
This is a test.
\noindent \kitty
\end{document}
The general format for constructing leaders is \leaders<box or rule><glue> (which repeats <box or rule>). ...
15
The following code will do what is asked, using the plain TeX construction \loop ... \if ... \repeat:
\documentclass{article}
\title{The Song That Never Ends}
\author{by Norman Martin}
\begin{document}
\maketitle
%
\noindent This is the song that never ends. \newline
\loop \iftrue
Yes, it goes on and on my friends. \newline
Some people started ...
13
You can use the additional facilities of foreach macro given in the manual by adding pgfmath package too. For some reason, (initially 4) option is not working if TikZ is not fully loaded so you can define it externally.
\documentclass{article}
\usepackage{pgffor,pgfmath}
\begin{document}
\def\lastx{4}
\foreach \x[count=\xi from 2,remember=\x as \lastx] ...
13
As David Carlisle has already said, do not fully expand \nocorrlist. LaTeX's code for
the italic correction looks for the next token using \futurelet (in macro \maybe@ic).
If the next token is an element of the list \nocorrlist, then the italic correction is suppressed. LaTeX's original definition only includes the comma and period as tokens
with catcode 12 ...
12
I've found that when passing options to TikZ elements as macros then I need to expand the macro before passing it in:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}
\foreach \i in {1,2,...,6}
{
\edef\optname{name path global=line\i}
\expandafter\draw\expandafter[\optname] (-3,\i) -- ...
12
I don't think that this is possible. However, in your case the / notations isn't really necessary:
\begin{tikzpicture}
\foreach \y [count=\i] in {0,0.25,...,1} {
\only<\i>{
\draw (0,0) rectangle (1,1);
\draw (0,0) -- (1,\y);
}
}
\end{tikzpicture}
For more complicated cases, when one variable is dependent on the other via some ...
12
This answer may be more generic than specifically relating to TikZ/PGF.
(La)TeX is a macro-based language, so it does not work as expected compared to other languages when dealing with "arrays". For example, while \names[2] should yield Laura where
\def\names{Katie, Frank, Laura, Joe}
(indexing from 0), (La)TeX considers [2] to have no connection to ...
12
You can define recursive macros that call themselves until some termination is found; a solution to your problem can be as follows:
\makeatletter % we need to use kernel commands
\newcommand{\twoimages}{%
\begin{figure}[!htb]
\@twoimagesi
}
\newcommand\@twoimagesi{\@ifnextchar\stopimages{\@twoimagesend}{\@twoimagesii}}
\newcommand\@twoimagesii[6]{%
...
11
To abort the loop after the current iteration simply \let the internal \iterate macro to \relax.
If you want to skip the rest of the loop code
you can use a macro defined to \fi\iffalse for this (as Bruno already said).
Abort at end of current iteration:
\documentclass{article}
\begin{document}
\newcount\mycount
\mycount=1
\loop\ifnum\mycount<13
...
11
You don't say which package you got your loop macro from. This just uses the \loop in LaTeX (originally from plain TeX)
> \calA=\long macro:
->\mathcal {A}.
l.18 \show\calA
?
> \calZ=\long macro:
->\mathcal {Z}.
l.20 \show\calZ
produced from
\documentclass{article}
\makeatletter
\def\foo#1{%
\expandafter\newcommand\csname ...
11
The problem is that french makes : into an active character (after \begin{document}.
Solutions.
Define your own list processor in the preamble
\makeatletter
\newcommand{\bflist}[1]{\@for\next:=#1\do{\textbf{\next}}}
\makeatother
and use \bflist{\names}
Force : to have the correct category code:
\documentclass{article}
\usepackage[francais]{babel}
...
11
Nothing wrong with the the answers so far but they all use big heavyweight packages, this version doesn't use any package at all and needs rather less code.
\documentclass{article}
\def\Fbox#1#2{\ifnum#1=0\mbox{#2}\else\fbox{\Fbox{\numexpr#1-1\relax}{#2}}\fi}
\begin{document}
\Fbox{0}{hello}
\Fbox{1}{hello}
\Fbox{2}{hello}
\Fbox{3}{hello}
...
10
As Altermundus said, you cannot add extra {} or \relax or other invisible material after the last row of the tabular. However, there is some extra material added after \@for. The implementation of \@for is not very good for this.
Since the \@for in LaTeX2e kernel has restrictions, I suggest \forcsvlist from etoolbox:
\documentclass{article}
...
10
You can combine the loop \foreach with the provided command \breakforeach. The following example is copied from the manual.
\begin{tikzpicture}
\foreach \x in {1,...,4}
\foreach \y in {1,...,4}
{
\fill[red!50] (\x,\y) ellipse (3pt and 6pt);
\ifnum \x<\y
\breakforeach
\fi
}
\end{tikzpicture}
10
The following example uses the syntax that the arguments are given inside the argument for the command: \images{{...}{...}{...}{...}{...}{...}...}. The macro \images then parses its argument and catches three arguments at a time:
\documentclass{article}
\newif\ifimagesSep
\newcommand*{\images}[1]{%
\par\noindent[begin images]\\\relax
\imagesSepfalse
...
10
The syntax for \@for is
\@for\cs:=<list>\do{<code>}
Here <list> is either a comma separated list of tokens or a macro expanding to such a list.
Thus, the calls
\@for\next:=abc, def, gh ,ij\do{<code>}
and
\def\mylist{abc, def, gh ,ij}
\@for\next:=\mylist\do{<code>}
are equivalent. Notice that LaTeX doesn't really ...
9
\documentclass{article}
\usepackage{longtable}
\newcounter{xyz}\newcounter{uvw}
\makeatletter
\newtoks\@tabtoks
\newcommand\addtabtoks[1]{\@tabtoks\expandafter{\the\@tabtoks#1}}
\newcommand*\resettabtoks{\@tabtoks{}}
\newcommand*\printtabtoks{\the\@tabtoks}
\makeatother
\pagestyle{empty}
\begin{document}
\resettabtoks
\loop\ifnum\theuvw<10\relax
...
9
A solution without any extra packages
\documentclass{article}
\usepackage{graphicx}
% introduce a dummy counter, initially 0
\newcount\tmp
\newcommand{\pdfappendix}[2]{% need this to prevent extra vertical space
% #1: image path and core part of name
% #2: maximum number
\tmp=0
\loop
% increment dummy counter
...
9
Your problem is that \@for is not 'expandable' (it contains an assignment, which is always a killer). The \write operation is an 'expansion context', like \edef, and so you need a fully-expandable loop. Here, I've taken the one from LaTeX3 and recoded it for LaTeX2e:
\documentclass{article}
\makeatletter
\newwrite\my@out
...
9
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach[count=\i] \txt in {20,18,...,2}
\node at (\i,0) {\txt};
\end{tikzpicture}
\end{document}
Note: It is always possible to cauculate one variable using the other one. Or you can't use ... in pgffor. So you can also use:
\begin{tikzpicture}
\foreach[evaluate=\y using ...
9
If your system is a Unix one, the following set of macros will do, but they require that the file is compiled with pdflatex -shell-escape:
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[catalan]{babel}
\usepackage{graphicx}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\figureloop}{m}
{
\group_begin:
...
9
As I mentioned in comments I wouldn't do this. But if I wanted to do it I'd probably do
\documentclass{article}
\makeatletter
\count@`\A
\loop
\lccode`\.\count@
\lowercase{\@namedef{b.}{\mathbf.}}%
\advance\count@\@ne
\ifnum\count@>`\Z\else
\repeat
\show\bA
\show\bC
\show\bZ
\stop
9
If you need only the images in the center of the page, just loop:
\documentclass{article}
\usepackage[a4paper,margin=0pt]{geometry}
\usepackage[demo]{graphicx}
\pagestyle{empty}
\newcounter{image}
\newcommand{\placeimages}[2]{%
\setcounter{image}{#1}\addtocounter{image}{-1}%
\loop\ifnum\value{image}<#2\relax
\stepcounter{image}%
...
Only top voted, non community-wiki answers of a minimum length are eligible