# Tag Info

## Hot answers tagged loops

31

This answer has four five six four routines: the last update: leaner code 6, as I understand better my hacky use of \pdfescapestring and could remove some superfluous extras (if they had been really needed, these extras would not have been enough anyhow). the very last update: improved sub-routine for merging in code 6 bringing at least a 2x speed ...

14

No packages: \documentclass{article} \makeatletter \newcount\my@repeat@count \newcommand{\myrepeat}[2]{% \begingroup \my@repeat@count=\z@ \@whilenum\my@repeat@count<#1\do{#2\advance\my@repeat@count\@ne}% \endgroup } \makeatother \begin{document} \myrepeat{4}{x} \myrepeat{4}{\myrepeat{2}{x}} \end{document} Why the group? It allows nested ...

13

There are numerous ways to do this, but the simplest given what you've described is to use the pgffor package which provides a simple syntax for such loops: \documentclass{article} \usepackage{pgffor} % next code just generates some files to mimic yours \begin{filecontents}{test-table-1.tex} \begin{table}[htpb] \begin{tabular}{ccc} A & B & C\\ A ...

13

You can use the odsfile package, which supports manipulations with OpenDocument Spreadsheet files. You need to convert your excel file to ODS using LibreOffice (Excel can do this as well, but LibreOffice produces cleaner files). Odsfile provides \includespread command with keyval interface: \documentclass{article} \usepackage{fontspec} \usepackage{odsfile} ...

10

A short demo using l3sort: \documentclass{article} \usepackage{expl3,l3sort,xparse} \ExplSyntaxOn \cs_set:Npn \quicksort_fp:nnTF #1#2 { \fp_compare:nNnTF {#2} > {#1} } \DeclareExpandableDocumentCommand \quicksort { m } { \tl_sort:nN {#1} \quicksort_fp:nnTF } \ExplSyntaxOff \edef\demo {% \quicksort {% ...

10

You can use \@tfor. I provide also a better redefinition of the dot under according to your wish: \documentclass{article} \usepackage{graphicx} \let\d\relax \DeclareRobustCommand{\d}[1]{% \oalign{#1\cr\hidewidth\scalebox{0.5}{\textbullet}\hidewidth\cr}% } \makeatletter \newcommand{\ds}[1]{% \@tfor\next:=#1\do{\d{\next}}% } \makeatother ...

10

You could use the TikZ polygon shapes in a \foreach loop. Add color and styles as you like. Key points are regular polygon shape of the shapes.geometric library relative positioning of the positioning library remember the last node, using characters a, b, ... for node names using the south anchor for positioning and anchoring at the same base line ...

10

Here's a version without any package, just using plain \loop and \repeat etc. \documentclass{article} \newcount\myloopcounter \newcommand{\repeatit}[2][10]{% \myloopcounter0% initialize the loop counter \loop\ifnum\myloopcounter < #1 % Test if the loop counter is < #1 #2% \advance\myloopcounter by 1 % \repeat % start again } ...

9

One possibility: \documentclass[a4paper,10pt,landscape]{article} \usepackage{tikz} \begin{document} \begin{tikzpicture} \foreach [count=\i] \j in {a,b,...,j}{ \node (\i) at (\i, \i) {\j} ; } \end{tikzpicture} \end{document} percusse mentions the alphalph package in a comment, and its \alphalph does exactly what you ...

8

The definitions of \B and \C are the problem: \def\B{\advance\cnt@a by-1} \def\C{\ifnum\cnt@a>0} TeX does not stop at the end of the numbers, but continue expanding until it finds a token, which is not a digit, e.g.: \B1234\relax will decrease \cnt@a by -11234, not by -1. Or \B\C Then the \ifnum is executed before the counter is decreased. Both ...

8

Update I simplified the code using a \pic for the fundamental domain. For the color, I present three options: Randomly selecting shades of a fixed color (red, in my example), gives different coloring for each piece: \documentclass[tikz]{standalone} \usetikzlibrary{calc} \tikzset{ fundamental/.pic={ \draw[,scale=0.4,black,fill=red!\tmp,rotate ...

7

Short answer: You are looking for Auto Multiple Choice (AMC). Long answer: You are looking for Auto Multiple Choice (AMC) that is a LaTeX package and a free GUI program for Linux. If you are a Window user that make regularly exams, it worth to install Linux only to have this program. AMC can make one exam to each student with randomly questions, so each ...

6

Here's an approach based on the LaTeX 3 syntax made available by expl3. I modified the table a bit to get it to fit. Otherwise, I just duplicated your example to 3 different files with 3 different captions. Obviously, geometry is optional. I just used it to make things fit for demonstration purposes. The preamble defines a new command with the following ...

6

This algorithm uses the median of the first, middle and last element for the pivot. I tried to minimize conversions between text and numbers, assuming that \csname is faster than \strip@pt. (With all the \expandafters needed I wonder if this is really true.) The first step is to parse the array stored as a comma delimited string (in other words, a tikz ...

6

In the calculation of \pgfmathsetmacro{\theta}{360*\k/\N} 360*\k easily becomes bigger than what LaTeX can handle. Reversing the multiplication order, solves this: \pgfmathsetmacro{\theta}{360/\N*\k}

6

Like this? \documentclass{article} \usepackage{pgffor} \usepackage{graphicx} \begin{document} \foreach \x in {a,b,c} { \begin{figure}[p] \includegraphics[scale=.5]{example-image-\x} \caption{So, three figures displayed, and this one has the number \x\ of the letter position (or iteration count)} % ie. 1 for a, 2 for b, 3 for c \end{figure} ...

6

\loop is a very simple macro and can not be nested at the same group level: \documentclass{article} \newcounter{stafflines} \newcommand{\musicstaff}{% \setcounter{stafflines}{0} ...

6

This would be my first try, using a counter (no need of additional packages): \documentclass[a4paper,10pt,landscape]{article} \usepackage{tikz} \newcounter{cnti} \begin{document} \begin{tikzpicture} \foreach \i in {0,...,9}{ \setcounter{cnti}{\i}\addtocounter{cnti}{1} \node (i) at (\i, \i) {\alph{cnti}} ; } ...

6

You can use the macro \scanargs \macro x1,y1 x2,y2 ... xn,yn; and then you can use the scanned arguments in your \macro in the form \x1, \x2, ... \x9, \y9, but \x{10}, \y{22} etc. I show the example using your example: \documentclass{article}\usepackage{tikz} \newcount\tmpnum \def\scanargs #1#2;{\let\tmp=#1\tmpnum=0 \scanargsA #2 {},{} } \def\scanargsA ...

6

This is just a simple example with tcbraster. You can fix columns number and distance between rows and columns, color of background boxes, titles, ... \documentclass{article} \usepackage[most]{tcolorbox} \begin{document} \begin{tcbraster}[raster columns=4, raster equal height=rows, size=fbox, colframe=red!50!black, center title] ...

5

This is easy enough using expl3 (there are several possible approaches): \documentclass{article} \usepackage{expl3,xparse} \ExplSyntaxOn \DeclareExpandableDocumentCommand \mycmd { m } { \__mycmd_loop:nN {#1} aeiouAEIOU \q_recursion_tail \q_recursion_stop } \cs_new:Npn \__mycmd_loop:nN #1#2 { \quark_if_recursion_tail_stop_do:nn {#2} { a } ...

5

Here's an approach with expl3 \documentclass{article} \usepackage{xparse,glossaries} \ExplSyntaxOn \DeclareExpandableDocumentCommand{\indef}{m} { \str_case_x:nnF { \tl_head:f { \tl_lower_case:n { #1 } } } { {a}{an} {e}{an} {i}{an} {o}{an} {u}{an} } {a}~#1 } \ExplSyntaxOff \begin{document} \indef{abc} --- \indef{cde} --- ...

5

As you have named the coordinates, just loop over their names: \documentclass[margin=10pt]{standalone} \usepackage{tikz} \usetikzlibrary{arrows,calc} \begin{document} \begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1.0cm,y=1.0cm] \clip(-0.5,-1) rectangle (5.5,3.5); % defining coordinates \coordinate (1) at (0,0); \coordinate (2) at ...

5

Assunming that the input to \dothis is exactly one letter long in each case (which holds for this specific example -- so I guess it's the actual use case) you could simply use \expandafter: \foreach \n in {a,b,c,e} {\expandafter\dothis\n}. Full example \documentclass{beamer} \usepackage{tikz} \makeatletter \newcommand*{\dothis}[1]{% \stringcases {#1}% {% ...

5

The cells in table environments (tabular, longtable, ...) are also groups, thus the internal loop definitions and the value for \n are lost after the first cell/group. Typical workaround is to put the contents into a macro or token register first: \documentclass{article} \usepackage{longtable} \begin{document} \newcount\n \n=0 \begin{longtable}{ccc} ...

5

An implementation with expl3, where I define a \newdrawingcommand that takes as arguments a command name and the replacement text; optionally a command based on \foreach can be added, for greater flexibility. In the replacement text, the various points can be referred to by \x and \y; these macros are available only there (they won't clobber other existing ...

5

\documentclass[12pt]{article} \usepackage{forloop} \begin{document} \newcounter{x} \forloop{x}{0}{\value{x}<5}{\noindent value is \arabic{x}\\} \end{document} You can also print the numbers as strings (zero, one, two , ...), if you load the fmtcount package and instead of \arabic{x} you can use \numberstring{x}.

5

As a single path: \documentclass[tikz,border=5]{standalone} \begin{document} \tikz\draw (0,0) \foreach \i in {3,...,10} \foreach \j in {1,...,\i}{ -- ++(360/\i*\j:1) }; \end{document}

5

You can use the count feature of \foreach: \documentclass{article} \usepackage{pgffor} \begin{document} \foreach \i [count=\xi] in {a,b,c} { \ifnum\xi>1 and \fi \i } \foreach \i [count=\xi] in {a} { \ifnum\xi>1 and \fi \i } \end{document}

5

xint should have a binomial function but I forgot to include it in the last release. Here is one way in the meantime: The update has permuted the order of presentation, as testing proved that the simpler approach using the built-in factorial was significantly faster except for cases with a small #2. \documentclass{article} \usepackage{xintexpr}% at least ...

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