I am trying to draw a diagram using TikZ in which I can specify particular elements within the interval that satisfy a certain condition. This example seems to be the "closest" to what I intend to draw. I tried to modify it, however, due to my limited skills in TikZ, I wasn't able to specify the pattern of labeling, and to draw the interval end braces properly. Any suggestions or hints shall be appreciated.
3 Answers
Here's a try.
Update: Changing the value of \bound
will change the maximum number of points on each side of N
. Changing the value of the x
key will change the distance between adjacent points. (Thanks to @Jubobs for fueling this change)
Code
\documentclass[border=2pt]{standalone}
\usepackage{tikz,amssymb}
\usetikzlibrary{calc}
\newcommand\bound{10} % bound number of points on each side of N
\newcommand\labelnum[3][]{
\begin{scope}[font=\footnotesize,x=.3cm,#1]
\foreach \mypt in {0,#2,...,\bound}{
\draw(\mypt,0)circle[radius=2pt];
\draw(-\mypt,0)circle[radius=2pt];
}
\draw(-\bound-5,0)--(\bound+5,0) node[pos=0,left]{$\mathbb Z$};
\node(start)[at={(-\bound-4,0)},label=below:{$N-d$}]{$[$};
\node(end)[at={(\bound+4,0)},label=below:{$N+d$}]{$]$};
\node[at={($(start)!.5!(end)$)},label=below:{$N$}]{\vphantom{$[$}};
\filldraw(0,0)circle[radius=2pt];
\node[at={(-\bound-2,0)},above]{$\cdots$};
\node[at={(\bound+2,0)},above]{$\cdots$};
\node[at={(0,0)},above=5pt]{#3};
\end{scope}
}
\begin{document}
\begin{tikzpicture}
\labelnum{1}{All integers are labeled}
\labelnum[yshift=-2cm]{2}{Even numbers are labeled}
\labelnum[yshift=-4cm]{5}{Multiples of 5 are labeled}
\labelnum[yshift=-6cm]{3}{Multiples of 3 are labeled}
\end{tikzpicture}
\end{document}
Output
-
-
-
-
Thank you. This certainly matches what I wanted to draw. Is there an easy way to add the labels, e.g., "Multiples of 3 are labeled"? Commented Jan 23, 2014 at 13:49
-
1@AbdullahHeyari: See updated answer. I modified
\labelnum
to take an extra argument, which sets the "title" of the line.– Herr K.Commented Jan 23, 2014 at 17:30
A PSTricks solution where the diagram has been 'automated'; one can now choose all the absolute and relative distances in the diagram and it will automatically change accordingly.
\documentclass{article}
\usepackage{pstricks-add}
\usepackage{amssymb}
\newcommand*\setZ{\mathbb{Z}}
\usepackage{expl3}
\ExplSyntaxOn
\cs_new_eq:NN \calc \fp_eval:n
\ExplSyntaxOff
% values needed (automatically calculated)
\newcommand*\StartDiagram{\calc{\Beginning-0.4}}
\newcommand*\EndDiagram{\End}
\newcommand*\HeightAxis[1]{\calc{(#1-1)*\AxesDist+0.35}}
\newcommand*\HeightDrawing{\calc{\HeightAxis{3}+0.35}}
\newcommand*\StartDist{\calc{\Beginning+\StartIndent} }
\newcommand*\StartDots{\calc{\StartDist+1+\StartDistIndent}}
\newcommand*\WidthDots{\calc{\End-\Beginning-2*(\StartIndent+1+\StartDistIndent)}}
\newcommand*\DistDots[1]{\calc{\WidthDots/(#1-1)}}
\newcommand*\SmallDotsStart{\calc{\StartDist+\SmallDotsIndent}}
\newcommand*\SmallDotsEnd{\calc{\End-(\StartIndent+1)}}
\newcommand*\SmallDotsDist{\calc{(1-\SmallDotsIndent)/2}}
\newcommand*\Middle{\calc{(\Beginning+\End)/2}}
%%% parameters %%%
% endpoints of axes
\newcommand*\Beginning{2 }
\newcommand*\End{12 }
% distance between axes
\newcommand*\AxesDist{2.5}
% constants of indentation on axes
\newcommand*\StartIndent{0.75}
\newcommand*\SmallDotsIndent{0.5}
\newcommand*\StartDistIndent{0.25}
% number of big, `hollow' dots on the lines
\newcommand*\DotsLineTop{23}
\newcommand*\DotsLineMiddle{7}
\newcommand*\DotsLineBottom{5}
\begin{document}
\begin{pspicture}(\StartDiagram,0)(\EndDiagram,\HeightDrawing)
{\psset{dotstyle = Bo}
\multido{\rC = \StartDots+\DistDots{\DotsLineTop}}{\DotsLineTop}{\psdot(\rC,\HeightAxis{3})}
\uput[90](\Middle,\HeightAxis{3}){\scriptsize Even numbers are labeled}
\multido{\rB = \StartDots+\DistDots{\DotsLineMiddle}}{\DotsLineMiddle}{\psdot(\rB,\HeightAxis{2})}
\uput[90](\Middle,\HeightAxis{2}){\scriptsize Multiples of $3$ are labeled}
\multido{\rA = \StartDots+\DistDots{\DotsLineBottom}}{\DotsLineBottom}{\psdot(\rA,\HeightAxis{1})}
\uput[90](\Middle,\HeightAxis{1}){\scriptsize Multiples of $5$ are labeled}}
\multido{\r = 0.35+\AxesDist}{3}{%
\psline(\Beginning,\r)(\End,\r)
\uput[180](\Beginning,\r){$\setZ$}
\multido{\rD = \SmallDotsStart+\SmallDotsDist, \rE = \SmallDotsEnd+\SmallDotsDist}{3}{%
{\psset{dotsize = 2pt}
\psdot(!\rD\space \r\space 0.1 add)
\psdot(!\rE\space \r\space 0.1 add)}}
\psdot(\Middle,\r)
\uput[270](\StartDist,\r){\scriptsize $N-d$}
\uput[270](\Middle,\r){\scriptsize $N$}
\uput[270](!\Beginning \End add \StartDist sub \r){\scriptsize $N+d$}
\rput(!\Beginning \End add \StartDist sub \r\space 0.1 add){$]$}
\rput(!\StartDist \r\space 0.1 add){$[$}}
\end{pspicture}
\end{document}
-
1Thank you. Your suggested answer introduced me to the power of PSTricks! Commented Jan 23, 2014 at 13:51
-
@AbdullahHeyari Glad I could help.
:)
(Remember to accept one of the answers in due time.) Commented Jan 23, 2014 at 13:59 -
I shall select an answer soon. Something is hindering the proper execution of the PSTricks on my computer. I received multiple error messages saying: Undefined control sequence \multido Commented Jan 23, 2014 at 15:49
-
@AbdullahHeyari Have you got an updated LaTeX distribution? If not, try to update it. Commented Jan 23, 2014 at 16:00
Another solution with PSTricks just for spending the boring time.
\documentclass[pstricks,border=12pt,12pt]{standalone}
\usepackage{multido,amssymb}
\SpecialCoor
\psset{xunit=2.5mm,yunit=5mm}
\begin{document}
\begin{pspicture}(-30,-9)(30,1)
\foreach \y/\t in {0/{Even numbers },-4/{Multiples of 3 },-8/{Multiples of 5 }}
{
\psline(-28,\y)(30,\y)
\psset{labelsep=10pt}
\uput[90](0,\y){\t are labeled}
\uput[-90](0,\y){$N$}
\uput[90](-22,\y){$\cdots$}
\uput[90](22,\y){$\cdots$}
\rput(-25,\y){$[$}
\rput(25,\y){$]$}
\uput[-90](-25,\y){$N-d$}
\uput[-90](25,\y){$N+d$}
\uput{6pt}[180](-28,\y){$\mathbb{Z}$}
}
\multido{\i=-20+1}{41}
{
\pscircle*[linecolor=red](!\i\space dup 2 mod 0 ne {/SD {} def} if 0){2pt}
\pscircle*[linecolor=green](!\i\space dup 3 mod 0 ne {/SD {} def} if -4){2pt}
\pscircle*[linecolor=blue](!\i\space dup 5 mod 0 ne {/SD {} def} if -8){2pt}
}
\end{pspicture}
\end{document}