Posts

Showing posts from December 1, 2018

Use a foreach statement to simplify TikZ drawing

Image
up vote 7 down vote favorite Below, the two diagrams should be the same, but actually they are not! documentclass[margin=2pt]{standalone} usepackage{tikz} usetikzlibrary{positioning,fit} newcommand{PL}[3]{ node[thick,circle,draw,minimum size=0.5cm,inner sep=0,outer sep=0,label=left:#3,#1](#2){}; draw[thick,#1] (#2.south west) -- (#2.north east); } begin{document} begin{tikzpicture} PL{L1}{1} PL[below=0.1 of L1]{L2}{2} PL[below=0.1 of L2]{L3}{3} PL[below=0.1 of L3]{L4}{4} PL[below=0.1 of L4]{L5}{5} PL[below=0.1 of L5]{L6}{6} PL[below=0.1 of L6]{L7}{7} node[fit=(L1)(L7), draw] {}; end{tikzpicture} begin{tikzpicture} PL{L1}{1} foreach i [evaluate=i as j using i - 1] in {2,...,7} { PL[below=0.1cm of Lj]{Li}{i} } node[fit=(L1)(L7), draw] {}; end{tik