我正在尝试使用 TikZ 绘制一个图表,其中我可以指定区间内满足特定条件的特定元素。这例子似乎“最接近”我想要绘制的内容。我尝试对其进行修改,但由于我在 TikZ 方面的技能有限,我无法指定标记模式,也无法正确绘制间隔结束括号。任何建议或提示都将不胜感激。
答案1
这是一个尝试。
更新:改变 的值\bound
将改变 两侧的最大点数N
。改变 键的值x
将改变相邻点之间的距离。(感谢 @Jubobs 推动这一改变)
代码
\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}
输出
答案2
PSTricks 解决方案中图表已实现“自动化”;现在可以选择图表中的所有绝对距离和相对距离,并且它将自动进行相应更改。
\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}
答案3
另一个解决方案是使用 PSTricks 来打发无聊的时间。
\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}