Forum digitalis

3.7 Environments

scope

The scope environment allows you to apply local transformations or styles to a set of TikZ commands without affecting the rest of the picture. You can scale, rotate, or shift all elements inside a scope.

\begin{tikzpicture} \draw[fill=blue!20] (0,0) rectangle (2,2); \node at (1,1) {Normal}; \begin{scope}[shift={(4,0)}, scale=1.5, rotate=30] \draw[fill=green!20] (0,0) rectangle (2,2); \node at (1,1) {Scaled+rotated}; \end{scope} \begin{scope}[shift={(0,-3)}] \draw[fill=yellow!50] (0,0) rectangle (2,2); \node at (1,1) {Yellow}; \end{scope} \end{tikzpicture}

axis / pgfplots

The axis environment with the package pgfplots (\usepackage{pgfplots}!!!) is used to create plots and function graphs. You can plot mathematical functions or data easily.

\begin{tikzpicture} \begin{axis}[ title={Quadratic functions}, xlabel={$x$}, ylabel={$y$}, grid=both, minor tick num=1, width=8cm, height=6cm, axis lines=middle, enlargelimits=0.1 ] \addplot[blue, thick, samples=100] {x^2}; \end{axis} \end{tikzpicture}

\node[draw]

The \node[draw] command creates a node with a border. Nodes can contain text, tables, or even other TikZ commands. Useful for structured or complex content. The following example requires \usetikzlibrary{positioning}.

\begin{tikzpicture}[node distance=2cm] \node[draw, fill=yellow!20] (A) { \begin{tabular}{c} Start \\ Process \end{tabular} }; \node[draw, fill=green!20, below=of A] (B) { \begin{tabular}{c} Decision \\ Yes / No \end{tabular} }; \draw[->, thick] (A) -- (B); \end{tikzpicture}

path

The \path command defines explicit paths. You can connect points, draw shapes, or define coordinates without immediately drawing them.

\begin{tikzpicture} \node (A) {Start}; \node[right=of A, yshift=13mm] (B) {M}; \node[right=of B] (C) {End}; \draw[->] (A) -- (C); \path (A) -- (C) coordinate[midway] (M); \node[fill=yellow, circle] at (M) {Z}; \end{tikzpicture}

foreach

The \foreach command allows repetition of TikZ commands over a list of values. Useful for creating grids, patterns, or multiple shapes efficiently.

\begin{tikzpicture} \foreach \x in {1,...,4} \draw (\x,0) -- (\x,5); \foreach \y in {1,...,4} \draw (0,\y) -- (5,\y); \end{tikzpicture}