Tag Info

Hot answers tagged

32

Requires xelatex: \documentclass{article} \usepackage{color} \XeTeXinterchartokenstate = 1 \newXeTeXintercharclass \vowelsclass \XeTeXcharclass `\a \vowelsclass \XeTeXcharclass `\e \vowelsclass \XeTeXcharclass `\i \vowelsclass \XeTeXcharclass `\o \vowelsclass \XeTeXcharclass `\u \vowelsclass \XeTeXcharclass `\A \vowelsclass \XeTeXcharclass `\E ...


30

This is just for practicing with LaTeX3 code. \documentclass{article} \usepackage{xparse,xcolor} \ExplSyntaxOn \NewDocumentCommand{\colorize}{mm} { \cs_set:cpn { maryjane_color_#1: } { \textcolor{#2}{#1} } } \tl_new:N \l_maryjane_text_tl \NewDocumentCommand{\changecolors}{ O{aeiou} m } { \tl_set:Nn \l_maryjane_text_tl { #2 } \tl_map_inline:nn { #1 ...


20

The main loop for comma separated lists in LaTeX3 is \clist_map_inline:nn The first argument is an explicit list, the second argument tells LaTeX what to do with each item. For instance, we want to print an enumerate environment from the items: \documentclass{article} \usepackage{xparse} \ExplSyntaxOn \NewDocumentCommand{\makeenumerate}{ m } { ...


12

There is a package lambda-lists by Alan Jeffrey for "Lists in TeX's mouth". Here's an example in plain TeX: \catcode`@=11 \input lambda.sty \catcode`@=12 \tracingmacros=1 % check out the log file afterwards! \def\mylist{\Listize[1, 2 ,3,,4=foo,5,]} \def\myfun#1{do something with #1\def\foo{}} \def\fooDefined?{\ifdefined\foo foo's defined\else foo's ...


12

The difficulty is not in producing it, but rather in printing it. Here's a way; for printing ten terms we need two lines and I decided to let TeX bother with the splitting. The \telescope command is generic, by redefining \Term you can print other similar series. \documentclass{article} \usepackage{xparse,amsmath} \ExplSyntaxOn ...


12

egreg just got there first but: \documentclass{article} \newcommand\term[1]{% {(\ifnum#1=1 1 \else\frac{1}{#1}\fi - \frac{1}{\the\numexpr#1+1\relax})}} \newcommand\xsum[1]{% \term{#1}% \ifnum#1<10 + \expandafter\xsum\expandafter{\the\numexpr#1+1\relax}% \fi} \begin{document} $ \xsum{1} $ \end{document}


11

Using \# simply defines \#. You need something that, given a name, becomes the command with that name. Such a thing exists as a TeX primitive command: \csname ... \endcsname \documentclass{article} \newcommand{\MkVec}[1]{\expandafter\def\csname#1\endcsname{\vec{#1}}} \MkVec{x}\MkVec{y} \begin{document} Some vectors: $\x$, $\y$. \end{document} If you ...


11

The main error is in the line \ifnum\select@i=#1\@@select@item\@endfortrue\fi% When \@@select@item the lookup for the number is not finished and TeX keeps expanding until finding something that's not a digit; when \@@select@item is 1, it's easy to predict chaos. A better style is \ifnum#1=\select@i\@@select@item\@endfortrue\fi because \select@i is a ...


10

This is using the under-development tabstackengine package, first introduced here at Writing a table with equally spaced columns, based on the widest column (source code available at Measuring align). The package extends the stackengine package by adding tabbing capability. This answer, Can I tab inside of align environment?, gives some of the syntax of ...


10

Package kvsetkeys Package kvsetkeys provides parsers for key value lists and comma separated lists. Many formats are supported: LaTeX, plain TeX, it works even with iniTeX. Syntax: \comma@parse{⟨comma separated list⟩}{⟨code/processor with one argument⟩} Example The example is given for iniTeX to show the minimal requirements of the package. % Setup ...


10

You can count how many elements there are in one of the arrays and use that number as the foreach limit. Then every spin accesses one element of the arrays; \documentclass[tikz]{standalone} \def\firstlist{{0,1,2}} % <== Notice the double brace for array notation \def\secondlist{{0,1,2}} \newcommand{\testa}{ \pgfmathdim{\firstlist}%Get the number of ...


10

Here is a very simple solution: \documentclass{article} \usepackage{tikz} \def\firstlist{0,1,2} \def\secondlist{0,1,2} \newcommand{\testa}{ \foreach \x [count=\c,evaluate=\c as \y using {{\secondlist}[\c-1]}] in \firstlist { \draw(0,\x)--(1,\y); } } \begin{document} \begin{tikzpicture} \testa \end{tikzpicture} \end{document} An extended ...


9

I think the problem arises because when the sprial changes direction two squares are drawn at each corner. For example, if the spiral is moving right, when the x < x_max condition is not met the direction is changed but the y coordinate is not moved up so the next square is drawn in the same place as the last square. I've corrected this in the code below ...


9

I'm not so sure you want to do it. ;-) \documentclass{article} \usepackage{xparse} \def\xforeach#1#{\xforeachaux{#1}} \ExplSyntaxOn \NewDocumentCommand{\xforeachaux}{mm} { \carlitos_xforeach:nn { #1 } { #2 } } \seq_new:N \l_carlitos_xf_first_seq \seq_new:N \l_carlitos_xf_second_seq \seq_new:N \l_carlitos_xf_list_seq \cs_new_protected:Npn ...


9

Use it without a second member (which would denote the step): \documentclass{article} \usepackage{tikz}% http://ctan.org/pkg/tikz \newcommand{\boucle}[1]{\foreach \x in {1,...,#1} {\x }} \begin{document} From 1 to 5 : \boucle{5} From 1 to 2 : \boucle{2} From 1 to 1 : \boucle{1} \end{document} The same result could be achieved using multido: ...


9

As mentioned in the comments it is more efficient to avoid \foreach and do your own tests for empty lists or list items. Also anything done within a \foreach iteration has to be done globally to last past the end of the loop. There may be packages which already provide this functionality but here is an example of what could be done. It contains a bit more ...


9

\foreach strips spaces before each list item, and then collects everything up to the next comma (or the list terminating token) before assigning to the relevant variables. The \foreach problem arises in this case because of the extra space given before the closing brace in the list. Consider the following: \foreach \p/\q in {a/{1,2,3}, b/{4,5,6} } ...


8

Here is another answer to propose an alternative to the very good egreg's answer: \documentclass{article} \newcounter{temp} \usepackage{pgffor} \begin{document} $\sum_{k=1}^N \left(\frac{1}{k}-\frac{1}{k+1} \right) = \left(1-\frac{1}{2}\right) \foreach \n in {2,...,10} {\setcounter{temp}{\n}\stepcounter{temp} +\left(\frac{1}{\n}-\frac{1}{\arabic{temp}} ...


8

The etoolbox package provides some list processing tools. For comma-separated lists there are \docsvlist{<list>} where each item is put in the input stream with \do{<item>}. This means a suitable definition of \do has to be provided. \forcsvlist{<handler>}{<list>} where each item is put in the input stream as argument to ...


8

Using egreg's template for straight LaTeX \documentclass{article} \makeatletter \newcommand{\makeenumerate}[2][,]{\begin{enumerate}% \def\dolist##1{\expandafter\@dolist##1#1\@eol}% \def\@dolist##1#1##2\@eol{% \begingroup\setbox0=\hbox{##1\unskip}\ifdim\wd0=0pt\endgroup\else\endgroup\item \fbox{\ignorespaces##1\unskip}\fi% ...


8

Here is an etoolbox implementation: \documentclass{article} \usepackage{etoolbox}% http://ctan.org/pkg/etoolbox \makeatletter \newif\ifprinted \newcounter{select@i} \newcommand\select[2]{% \select{<num>}{<csv list>} \printedfalse% Nothing has been printed \setcounter{select@i}{0}% List starts from 1 \renewcommand*{\do}[1]{% ...


8

I'd suggest you to switch to the more versatile algorithmicx package; using the noend option, you can achieve what you want: \documentclass{article} \usepackage[noend]{algcompatible} \begin{document} \begin{algorithmic} \IF{condition} \IF{another condition} \STATE do something \STATE do something else \ELSE ...


8

When using xpatch (or the original commands in etoolbox) for patching macros, it's recommended to use the builtin diagnostics, that is \tracingpatches and the “fail” argument. If you add \tracingpatches to the preamble, before your usage of \IfPackageLoaded, you'll get the message [debug] tracing \pretocmd on input line 26 [debug] analyzing ...


8

The usual approach to grabbing tokens with a delimiter in TeX would be something like \catcode`\@=11 % \def\grabargs#1#2#3{% \def\grabargs@aux##1#2{% \ifx\relax##1\relax% \else #3{##1}\par \expandafter\grabargs@aux \fi }% \grabargs@aux#1#2\relax#2 } \def\print#1{`{\tt #1}'} \catcode`\@=12 % \grabargs {Humpty Dumpty sat on a ...


8

There are various ways to accomplish your needs. \documentclass[11pt]{article} \usepackage{xparse} % for D % for A, B, C \newcounter{mycount} \makeatletter \newcommand\repeatstringA[2]{% \setcounter{mycount}{#1}% \ifnum\themycount>0 #2% \addtocounter{mycount}{-1}% \expandafter\@firstofone \else \expandafter\@gobble \fi ...


8

A classical implementation allowing also exponents: \documentclass{article} \makeatletter \newcommand{\env}[1]{% \sigma \expandafter\startm@keprimes\expandafter{\romannumeral#1000@}% } \def\startm@keprimes#1{\m@keprimes{}#1} \def\m@keprimes#1#2{% \if#2@% \expandafter\@firstoftwo \else \expandafter\@secondoftwo \fi {#1}% ...


7

Repetition via \romannumeral trick The number is multiplied with 1000 to convert it to a roman number. Then TeX produces a long string consisting of letter m, whose length is the original number. Then \repeatstringX looks at the next token, if it is an m, then the string unit is output. Otherwise the next token is the end marker F and the loop stops. ...


7

You could always "roll your own" solution, but it would require considerable work to make it look as nice as \foreach: \documentclass[tikz,border=5]{standalone} \makeatletter \def\attxt{@} \long\def\frch#1#2#3{\long\def\frchaction##1##2{#3}% \def\lsta{}\def\lstb{}% \edef\lst{#1,@,/#2,@,}\expandafter\@frch\lst\@} \def\@frch#1,#2/#3,#4\@{% ...


7

There are a number of possible approaches to doing this without the complexity: I'll cover a couple using expl3. First, if you don't mind keeping things non-expandable then you could do something like \documentclass{article} \usepackage{expl3} \ExplSyntaxOn \cs_new_protected:Npn \For #1#2#3#4 { \int_step_inline:nnnn {#1} {#2} {#3 - 1} {#4} } ...


7

You have to use a \protected@xdef: The first parameter of \coljoin as defined below can be used to pass in the macro that needs to be executed for each entry. It defaults to just passing the cell data thru without any additional processing. References: How do I use the ampersand (&) inside a foreach or conditional (or other group/environment) when ...



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