Hot answers tagged

11

You could remove duplicates using expl3 \ExplSyntaxOn \cs_new_eq:NN \removeclistdupes \clist_remove_duplicates:N \ExplSyntaxOff \Removeclistdupes\List A full example \documentclass[a4paper]{article} \usepackage{expl3} \ExplSyntaxOn \cs_new_eq:NN \removeclistdupes \clist_remove_duplicates:N \ExplSyntaxOff \usepackage{tikz} \begin{document} \def\zList{0,0,1,...


9

There are a few problems with your code. The first one, that generates the error, is that \foreach does (something equivalent to) \def\pgffor@body{<loop code>}, and if <loop code> contains a #, that will error (try it). The solution to this one is to double the #, so \IfNoValueF{##1}{^{##1}} then there will be no errors... but your commands ...


8

In order to reproduce the picture, the test should be 1 ≤ x < y. You can parametrize the rotation angle and test for the two special cases. \documentclass{standalone} \usepackage{tikz} \begin{document} \begin{tikzpicture} \foreach \x in {1,...,4} { \foreach \y in {1,...,4} { \def\rotation{-45} \ifnum\y=1 \ifnum\x=1 \def\rotation{0} \fi ...


6

LaTeX's ifthen package has some facility for combining conditionals with \and and \or, \not and parentheses. But your case is easy to do with \ifnum: \ifnum \x<\y \ifnum \x>1 \breakforeach \fi\fi


6

The problem is unrelated to foreach you would see the same from \def\x{1} \case{\x} You are comparing \ifx 1\x and the tokens 1 and \x are not the same so this is false. Conversely \case{12} would test \ifx112 which would test 1 and 1 as true and then typeset 2. You want to do a numeric test not test the unexpanded tokens, so \documentclass{article} \...


6

The error occurs because acrsine does not exist for angles greater than 90. After limiting refraction, total internal reflection occurs. Therefore, it is no longer possible to calculate the angle of the refracted ray as you try. \documentclass[border=10pt,tikz]{standalone} \usepackage{xcolor} \definecolor{lava}{rgb}{0.81, 0.06, 0.13} \definecolor{myblue}{rgb}...


6

You don't mention it but I assume you are using the loop from pgf which places a group around each iteration. It's easier to use a different construct eg map over the list with expl3. \documentclass{article} \usepackage{amsfonts} \ExplSyntaxOn \clist_map_inline:nn{F,N,Z,Q,R,C,H}{ \expandafter\NewDocumentCommand\csname#1#1\endcsname{o}{\ensuremath{\...


5

\documentclass[border=5mm]{standalone} \usepackage{tikz} \usepackage{xfp} \tikzset{every path/.style={draw=red,latex-}, every node/.style={above,inner sep=1pt}} \begin{document} \begin{tikzpicture} \foreach \num/\den [count=\n,evaluate=\n as \j using {isodd(\n)==1?.4:.2}] in {1/12,7/16,11/16,11/12}{ \draw (\fpeval{\num/\...


5

Use math library. \documentclass[tikz, border=1cm]{standalone} \usetikzlibrary{math} \begin{document} \begin{tikzpicture} \pgfset{fpu=true, fpu/output format=fixed} \tikzmath{ function f(\x, \n) { real \s; \s = 0; for \i in {0,...,\n}{ \s = \s + abs(\x + \i); }; return \s; }; } \pgfset{fpu=false} \draw[->,>=stealth] (-4,0)...


5

You should be more careful when choosing names for local variables in \foreach: % xcolor.sty, line 756: \def\textcolor#1#{\@textcolor{#1}} % xcolor.sty, line 757: \def\@textcolor#1#2#3{\protect\leavevmode{\color#1{#2}#3}} You're using \textcolor, which internally uses \color: if you redefine \color you can't expect that `\textcolor works; actually you ...


5

Here is a solution: \documentclass[tikz]{standalone} \newcommand{\cyclegraph}[2]{% \begin{tikzpicture} \foreach \n in {1,...,#1}{ \node[circle,fill=black] (n\n) at ({\n*360/#1}:#2cm) {}; } \foreach \n[evaluate={\n as \nnext using int(mod(\n,#1)+1)}] in {1,...,#1}{ \draw (n\n) -- (n\nnext); } \end{tikzpicture}% } \begin{...


5

It depends if you want to fill your nodes with content afterwards. But at this state, the following code is working with foreach loops. It is definitely NOT the best option here, since each line is different. A matrix of nodes should be a better answer. \documentclass{article} \usepackage{tikz} \usetikzlibrary{positioning} \begin{document} \begin{...


5

Every cycle in a \foreach loop is done inside a group, so the \newtcolorbox declaration is lost as soon as the group ends. You may appreciate a general loop that doesn't need groups: \documentclass{article} \usepackage{tcolorbox} \ExplSyntaxOn \NewDocumentCommand{\lforeach}{mm} {% #1 = list, #2 = template \cs_set:Nn \__lforeach_temp:n { #2 } \...


4

The problem is caused by your second \foreach \foreach \n in {1,...,#1-1}{ \draw (n\n) -- (n{1+\n}) } Firstly, a semicolon is missing. Secondly, calculation is not supported in node names (n{1+\n}). This just gives node names (n{1+1}), (n{1+2}), ... and raises an error. Apart from more general option evaluate used in @PaulGaborit's answer, here I use ...


4

You can't use numbers together with letters in control sequences. A control sequence name can be either a single nonletter or a sequence of one or more letters. So \name1 is an illegal name in this context and generally; in some situations it could seem to work, but don't count on this. Use \foreach \namea/\nameb in {J1/J2, J2/J3, J3/J4, J4/J5} { \...


4

Since egreg corrected your code, I simplify it by using loops with a single variable since the other two can be generated by a single variable. \documentclass[border=5mm,tikz]{standalone} \usetikzlibrary{shapes,intersections} \begin{document} \begin{tikzpicture} \tikzset{mass/.style={draw, fill=gray!20, cylinder, shape aspect=1, minimum width=1.5cm, ...


4

\documentclass{article} \usepackage{listofitems,pgffor} \newcounter{zcount} \newtoks\mytoks \mytoks{} \expandafter\def\csname zmatch0\endcsname{-9999999}% NUMBER NOT IN LIST \begin{document} \def\zList{0,0,1,1,1,2,2,10} The original list is \zList \readlist\zdata{\zList} \foreachitem\z\in\zdata[]{% \gdef\ztest{F}% \foreach\zcnt in {0,...,\thezcount}{% ...


4

Edit: Someone asked to have "unnecessary" zeros clipped. You can do this with \FPclip from the fp-package. If you wish to do the sorting-out with the tools provided by tikz, you can consider nested iteration on the already constructed list: \documentclass[a4paper]{article} \usepackage[nomessages]{fp} \usepackage{tikz} \usetikzlibrary{math} % ...


4

I can propose a more flexible set of commands. \documentclass{article} \usepackage{pgffor} %\usepackage{xparse} % uncomment if using LaTeX released before 2020-10-01 \ExplSyntaxOn \NewExpandableDocumentCommand{\checkcases}{mmO{}m} { \int_case:nnTF { #1 } { #2 } { #3 } { #4 } } \ExplSyntaxOff \newcommand{\mycaseswitch}[1]{% \checkcases{#1}{ {1}{...


4

\documentclass{article} \usepackage{amssymb,listofitems} \def\ZZstencil#1#2\relax{% \mathbb{#1}\ifx\relax#2\relax\else^{#2}\fi } \def\ZZargs{[1][]} \newcommand\makeZZ[1]{% \readlist\mylist{#1}% \foreachitem\z\in\mylist[]{% \expandafter\newcommand\csname\z\z\expandafter\expandafter \expandafter\endcsname\expandafter\ZZargs\expandafter{% \...


4

Static parts can indeed be typeset once and referenced in multiple frames; see the timeline option of animate. However, this adds some overhead and may not be worth it in terms of final PDF file size. (Therefore, we don't do this here.) There are a few options available that allow you to adjust control button size, colour, opacity and alignment, as well as ...


4

Define: \def\foreachinlist#1#2{\expandafter\foreachinlistA\expandafter#1\expandafter{#2}} \def\foreachinlistA#1{\foreach#1in} and use: \foreachinlist \i \alist In your example: \documentclass{standalone} \usepackage{pgfplots} \pgfplotsset{compat=1.16} \def\foreachinlist#1#2{\expandafter\foreachinlistA\expandafter#1\expandafter{#2}} \def\foreachinlistA#1{\...


3

Yes. The tabto package can help. Here, \tabstart defines the left margin of the table, and the length \tabinc gives the column spacing increment. \documentclass[a4paper]{article} \usepackage{tikz,tabto} \newlength\currtab \newlength\tabinc \setlength\tabinc{30pt} \def\tabstart{0pt} \newcounter{rowno} \setcounter{rowno}{-1} %\stepcounter{rowno} %\therowww \...


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