tikz add tag
Anonymous 1123
I am trying ta draw some Open Interval, Closed interval, Half open interval.
![ScreenHunter 840.png](/image?hash=0ba2d54158d43ceb476e770fec3ebcb90a2d8572d2758f600726608df59c7576)![ScreenHunter 839.png](/image?hash=929c65f73b8406f7085486ea444afda99837ef1bb009cb16ab369b66042486aa)![ScreenHunter 838.png](/image?hash=9a911fe3984e58ada5dc6df4fd76e4f1170409c6aef56eadcc7232acfa008f89)

![ScreenHunter 841.jpg](/image?hash=398e2501744b123adbf43efed0e2935aaddff328d56c068b0f6b82cda08dc170)

![ScreenHunter 842.png](/image?hash=055491b9ba49536f03578df35965953d897f50e22acd083861552d335f64f69b)

How to get a code to draw this?
Top Answer
user 3.14159
Here is a possible way to do this. A pic is defined that takes various arguments. The axis is scaled to fit a target width, which is given by the `width` key, but you can turn scaling off with `scaled`. There are various other keys for the domains, the delimiters and the annotations, which are explained a bit in the code.

```
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{patterns.meta}
\tikzset{pics/interval/.style={code={%
\def\pv##1{\pgfkeysvalueof{/tikz/interval/##1}}%
\tikzset{interval/.cd,#1}%
\iftikzintervalscaled
\pgfmathsetmacro{\myintervalscale}{\pv{width}/(\pv{xmax}-\pv{xmin})/1cm}%
\else
\pgfmathsetmacro{\myintervalscale}{1}%
\fi
\pgfmathsetmacro{\xmid}{\pv{xmax}/2+\pv{xmin}/2}%
\begin{scope}[xscale=\myintervalscale,shift={(-\xmid,0)}]
 \draw[interval/axis] (\pv{xmin},0) -- (\pv{xmax},0);
 \pgfmathtruncatemacro{\itest}{(\pv{xmin}<\pv{imin})}%
 \ifnum\itest=1
  \path[interval/excluded]
   (\pv{xmin},-\pv{h}/2) rectangle (\pv{imin},\pv{h}/2);
  \path  ([xshift=0.2ex/\myintervalscale]\pv{imin},0) node {$\pv{left}$}
  	node[interval/both labels,interval/left label]{$\pv{l}$};
 \fi
 \pgfmathtruncatemacro{\itest}{(\pv{xmax}>\pv{imax})}%
 \ifnum\itest=1
  \path[interval/excluded]
   (\pv{imax},-\pv{h}/2) rectangle ([xshift=-1em/\myintervalscale]\pv{xmax},\pv{h}/2);
  \path  ([xshift=-0.2ex/\myintervalscale]\pv{imax},0) node {$\pv{right}$}
  	node[interval/both labels,interval/right label]{$\pv{r}$};
 \fi
\end{scope}
}},
interval/.cd,axis domain/.code args={#1:#2}{%
\tikzset{interval/.cd,xmin=#1,xmax=#2}%
},interval domain/.code args={#1:#2}{%
\tikzset{interval/.cd,imin=#1,imax=#2}%
},xmin/.initial=-5,xmax/.initial=5,
imin/.initial=0,imax/.initial=1,
left/.initial={(},right/.initial={)},
scaled/.is if=tikzintervalscaled,scaled/.default=true,
width/.initial=8cm,h/.initial=9pt,
axis/.style={thick,-stealth},
excluded/.style={pattern={Lines[angle=45,distance={2pt}]}},
both labels/.style={below,text=blue,text height=1.2em},
left label/.style={},right label/.style={},
l/.initial={\pv{imin}},r/.initial={\pv{imax}}
}
\newif\iftikzintervalscaled
\tikzintervalscaledtrue
\begin{document}
\begin{tikzpicture}
 % the axis domain denotes the domain of the arrow
 % the interval domain is just the interval
 % by default the upper and lower interval bounds are used for the nodes
\path (0,0) pic{interval={axis domain=-8:15,interval domain=-3:5}}
 % but you can overwrite them with the l and r keys
  ++ (0,-1.5) pic{interval={axis domain=0:1,interval domain=1/2:1,l={\frac{1}{2}}}} 
  ++ (0,-1.5) pic{interval={axis domain=0:1,interval domain=1/3:2/3,l={a},r={b}}}
 % initially the delimiters are ( and ) but you can change them using 
 % the left and right keys 
  ++ (0,-1.5) pic{interval={axis domain=0:1,interval domain=1/2:1,l={a},left={[}}}
  ++ (0,-1.5) pic{interval={axis domain=1/6:1,interval domain=1/2:3/4,
  	l={\frac{1}{2}},r={\frac{3}{4}},
  	left={[},right={]}}};
\end{tikzpicture}
\end{document}
```

![Screen Shot 2020-09-20 at 10.09.13 PM.png](/image?hash=a080e025825b1558f96015e19e14b738b6e3603b350b4631e09bf3fb25b267ae)

There are various ways in which one could extend this, e.g. by parsing some interval definition like `[1/2,3/4(` and set the domain and delimiters accordingly.

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.