Hot answers tagged

32 votes
Accepted

Repeating the same column type

No pgffor package is required for this; just write your table preamble as: \begin{tabular}{l *{6}{n{2}{3}}} The general syntax is: *{n}{column(s) pattern} where n is the number of repetitions, ...
  • 268k
16 votes
Accepted

\foreach with two or more variables

Like this? \documentclass[border=2pt]{standalone} \usepackage{tikz} \begin{document} \begin{tikzpicture} \foreach\x/\y/\z in {4/2/A,5/3/B,6/4/C,2/.5/D,1/2/E,6/3/F,3/1.5/G,1/4/H} \draw [fill = ...
  • 17.6k
16 votes

Shading 25 squares within a square using `\foreach`

How can I resist to this golf challenge ! \documentclass[tikz,border=7pt]{standalone} \begin{document} \begin{tikzpicture} \foreach~in{0,...,24} \fill[shift={({mod(~,5)+.5},{div(~,5)})},...
  • 22.6k
15 votes
Accepted

Changing color in foreach

Please always post complete code so that people do not have to guess which packages and class are required to reproduce the problem or answer the question. The following gives one way of incrementing ...
  • 186k
15 votes
Accepted

Circular Motion

Here's an alternative way to draw your circle + vectors using Metapost and luamplib. Compile with lualatex (assuming you have the TeX Gyre maths fonts available). \documentclass[border=5mm]{...
  • 39.1k
14 votes
Accepted

How to draw a graph in LATEX?

The code illustrates the use of polar coordinates which make it easy to construct symmetrical graphs. \documentclass[border=2mm]{standalone} \usepackage{tikz} \usetikzlibrary{calc} \begin{document} \...
  • 46.7k
14 votes

How to draw honeycomb (line of hexagon)?

I defined a new command \hexmult that takes 2 arguments. The first is a positive integer giving the number of rows of hexagons. The second is a comma-separated list, where each entry is of the form i;...
  • 34.5k
13 votes
Accepted

Use evaluate option of \foreach to display integers not floats in TikZ

To get integer results, use the TikZ function int(...) when computing \r for the node text: \documentclass{article} \usepackage{tikz} \begin{document} \begin{tikzpicture} \draw[->] (-2,0) -...
  • 13.1k
13 votes
Accepted

Labeling axis in π increments

If you really want to stick with tikz (although I would recommend you use pgfplots for graphing),you can: Use \pgfmathsetmacro to compute the appropriate x-axis location as a multiple of pi. Use \...
12 votes
Accepted

pgfplots: use foreach inside addplot

To avoids expansion issues with \foreach, pgfplots offers \pgfplotsinvokeforeach which can be used without problems; also, you can easily draw your lines from the curve to the axis using the function (...
12 votes
Accepted

pgfmathsetmacro issue in foreach

After the definition via \pgfmathsetmacro, the macro \dest contains a real number (1.0, 2.0, ...), but very likely you need an integer. This can be achieved by using \pgfmathtruncatemacro instead of \...
12 votes

ifthenelse inside TikZ: not working

An alternative approach would be the standard \ifnum construct combined with \pgfmathparse. Note that since 1.6 is a float, you must provide a tolerance. A simple \pgfmathparse{\y == 1.6 ? int(1) : ...
  • 1,474
12 votes

TikZ foreach "experience bar"

\documentclass{article} \usepackage{tikz} \newcommand{\dotexp}[1]{% \edef\DEnum{\the#1}% \tikz{% \foreach \x in {1,...,6}{% \ifnum\x>\DEnum \edef\DEfill{lightgray}% \...
11 votes

Draw all existing intersections

Apparently Tikz doesn't like when you extend the lines artificially using shorten >=-5cm so that's the problem. Your lines don't actually reach the circle, so there are no intersections. This is ...
  • 36.8k
11 votes
Accepted

How to draw honeycomb (line of hexagon)?

If you don't mind specifying the numbers backwards: \documentclass[varwidth,border=5]{standalone} \usepackage{tikz} \tikzdeclarecoordinatesystem{hex}{% \pgfmathsetmacro\y{int(floor(sqrt(2*(#1))+0.5))...
  • 69.7k
11 votes

How to draw a curve from a previously calculated list of points

If I understand what is required, then (somewhat surprisingly) \draw plot [...] (P-\x); works, provided you set the domain and samples key correctly. Then options like smooth can be used. \...
  • 69.7k
11 votes

Color names in foreach loop

\foreach \x/\y in {..}. You forgot in. By the way, it's a bit shorter with rectangle: \documentclass{standalone} \usepackage{tikz} \begin{document} \begin{tikzpicture} \foreach \x/\y in {1/blue, ...
11 votes
Accepted

Loop code for repeated sums

To use it in math mode you could simply use a \foreach outside a tikzpicture. This would require the pgffor package (only if you're not using Tikz already): \documentclass[]{article} \usepackage{...
  • 9,638
11 votes
Accepted

Extract a list of distinct values from another list (remove dupes)

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} \...
  • 251k
10 votes

How can I draw several polygons on the same base line using TikZ?

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 ...
  • 226k
10 votes
Accepted

ifthenelse inside TikZ: not working

You can surely use \ifthenelse, but the test compares only integer it uses a single = when TikZ comes to 1.6 it actually sees it as 1.59998 Use integers, then: \documentclass[border=0.2cm]{...
  • 1.1m
10 votes
Accepted

add annotation on decoration points

\documentclass[tikz]{standalone} \usetikzlibrary{decorations.markings} \begin{document} \begin{tikzpicture}[dot/.style={ postaction=decorate, decoration={markings,mark= between positions 0 and 1 ...
  • 156k
10 votes
Accepted

pgfmathparse in foreach, use result as node coordinates

Your attempt does not work because the \pgfmathparse{…}\pgfmathresult is not expanded until \j is used within the coordinate to the \node. TikZ, of course, does not expect this, so it fails to parse ...
  • 3,029
10 votes

How to draw honeycomb (line of hexagon)?

Here's a way to draw your grids "by hand" using Metapost. The links in the comments show you similar approaches in tikz. \RequirePackage{luatex85} \documentclass[border=5mm]{standalone} \...
  • 39.1k
10 votes
Accepted

How to iteratively define coordinates in TikZ?

You can use (A\i) \documentclass[]{article} \usepackage{tikz} \begin{document} \begin{tikzpicture} \foreach \i in {0,1,2} { \coordinate(A\i) at (0, \i); \coordinate(B\i) at (1, \i); } ...
  • 7,871

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