Skip to content

Instantly share code, notes, and snippets.

@leingang
Last active February 14, 2025 00:15
Show Gist options
  • Save leingang/f01546aef4f93c9cd498244d330e2d28 to your computer and use it in GitHub Desktop.
Save leingang/f01546aef4f93c9cd498244d330e2d28 to your computer and use it in GitHub Desktop.
Use TikZ and PGFPlots keys to keep code DRY
% https://www.reddit.com/r/LaTeX/comments/1imkst5/tasty_tikz/
\documentclass{article}
\usepackage{amsmath}% \dfrac
\usepackage{tikz}
\input{svgnam.def}
\pagecolor[HTML]{2A3133}
\color{white}
\usepackage{pgfplots}
% define colors
\definecolor{c1}{HTML}{E035EB}
\definecolor{c2}{HTML}{73E7E5}
\definecolor{c3}{HTML}{F9FB64}
% replace commands with styles
% and use the PGFPLOTS `every *` keys
\pgfplotsset{
compat=1.18,
shape focus/.style={
axis lines=middle,
axis line style=dashed,
xticklabels={},
yticklabels={},
xtick={},
ytick={}
},
every axis/.append style={
width=0.4\linewidth,% now you don't need to scale the entire picture
shape focus,
xmin=-5, xmax=5,
ymin=-5, ymax=5,
clip=false,
axis line style={white},
every axis title/.append style={font=\scriptsize},
every axis plot/.append style={
<->,line width=1pt,smooth,
every node/.append style={below right,midway,font=\scriptsize}
}
},
}
\begin{document}
\section*{Basic functions}
\begin{center}
\renewcommand{\arraystretch}{2}
\begin{tabular}{ccc}
\begin{tikzpicture}%linear
\begin{axis}[c2,title={Identity}]
\addplot[sharp plot] coordinates {(-3,-3) (3,3)} node {$f(x)=x$};
\end{axis}
\end{tikzpicture} &
\begin{tikzpicture}%square
\begin{axis}[c1,title={Squaring}]
\addplot[domain=-2:2]{x^2} node {$f(x)=x^2$};
\end{axis}
\end{tikzpicture} &
\begin{tikzpicture}%cube
\begin{axis}[c3,title={Cubing}]
\addplot[domain=-1.5:1.5]{x^3} node {$f(x)=x^3$};
\end{axis}
\end{tikzpicture} \\
\begin{tikzpicture}%absolute
\begin{axis}[c1,title={Absolute Value}]
\addplot[<-] coordinates {(-3,3) (0,0)}
node[at end]{$f(x)=|x|$};
\addplot[->] coordinates {(0,0) (3,3)};
\end{axis}
\end{tikzpicture} &
\begin{tikzpicture}%squareroot
\begin{axis}[c3,title={Square Root}]
\addplot[->,domain=0:2] (x^2,x)% helps with the vertical tangent
node[at start]{$f(x)=\sqrt{x}$};
\end{axis}
\end{tikzpicture} &
\begin{tikzpicture}%cuberoot
\begin{axis}[c2,title={Cube Root}]
\addplot[domain=-1.5:1.5] (x^3,x)% helps with the vertical tangent
node {$f(x)=\sqrt[3]{x}$};
\end{axis}
\end{tikzpicture} \\
\begin{tikzpicture}%greatest integer
\begin{axis}[c3,title={Greatest Integer},
mark size=1pt
]
\addplot[arrows={-},jump mark left,mark=*]
coordinates {(-4,-4) (-3,-3) (-2,-2) (-1,-1) (0,0) (1,1) (2,2) (3,3) (4,4)};
\addplot[only marks, mark=*,mark options={fill=white}]
coordinates {(-3,-4) (-2,-3) (-1,-2) ( 0,-1) (1,0) (2,1) (3,2) (4,3)}
node[pos=0.45] {$f(x) = \lfloor x \rfloor $};
\end{axis}
\end{tikzpicture} &
\begin{tikzpicture}%reciprocal
\begin{axis}[c2,title={Reciprocal}]
\addplot[domain=-5:-0.2] {1/x}
node[above right, pos=0.7]{$f(x)=\dfrac{1}{x}$};
\addplot[domain=0.2:5] {1/x};
\end{axis}
\end{tikzpicture} &
\begin{tikzpicture}%exponential
\begin{axis}[c1,title={Exponential}]
\addplot[domain=-5:2.2]{2^x}
node[near end] {$f(x)=a^x$};
\end{axis}
\end{tikzpicture} \\
\begin{tikzpicture}%sine
\begin{axis}[c2,title={Sine}]
\addplot[domain=-4:4]{2*sin(deg(2*x))}
node[below,pos=0.8]{$f(x)=\sin x$};
\end{axis}
\end{tikzpicture} &
\begin{tikzpicture}%cosine
\begin{axis}[c1,title={Cosine}]
\addplot[domain=-4:4]{2*cos(deg(2*x))}
node[below,pos=0.7]{$f(x)=\cos x$};
\end{axis}
\end{tikzpicture} &
\begin{tikzpicture}%tangent
\begin{axis}[c3,title={Tangent},
xmin=-360,xmax=360,ymin=-12,ymax=12]
\addplot[domain=-265:-95] {tan(x)};
\addplot[domain=95:265] {tan(x)}
node[pos=0.2,anchor=north west] {\rlap{$f(x)=\tan x$}};
\addplot[domain=-85:85] {tan(x)};
\end{axis}
\end{tikzpicture}
\end{tabular}
\end{center}
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment