![使用 tikz 在数轴上圈出一个数字](https://linux22.com/image/474337/%E4%BD%BF%E7%94%A8%20tikz%20%E5%9C%A8%E6%95%B0%E8%BD%B4%E4%B8%8A%E5%9C%88%E5%87%BA%E4%B8%80%E4%B8%AA%E6%95%B0%E5%AD%97.png)
我想创建一个命令,使我能够创建一个数字线并圈出两个给定的值。
我有数轴的代码:
\documentclass{article} \usepackage{tikz}
\newcommand{\NL}[2]{\begin{center} \begin{tikzpicture}[scale =
1.07] \draw[latex-latex] (#1-0.5,0) -- (#2+0.5,0) ; \foreach \x in {#1,...,#2} \draw[shift={(\x,0)},color=black] (0pt,3pt) -- (0pt,-3pt); \foreach \x in {#1,...,#2} \draw[shift={(\x,0)},color=black] (0pt,0pt) -- (0pt,-3pt) node[below] {$\x$}; \end{tikzpicture} \end{center}}
\begin{document} \NLTwo{1}{10}
\end{document}
这段代码可以制作出好看的圆圈:
\documentclass{article}
\usepackage{tikz}
\newcommand*\circled[1]{\tikz[baseline=(char.base)]{
\node[shape=circle,draw,inner sep=2pt] (char) {#1};}}
\begin{document}
Numbers aligned with the text: \circled{1} \circled{2} \circled{3} end.
\end{document}
...我希望这些想法能够结合起来。
例如,我想使用 \NLTwo{1}{10}{3}{5} 之类的命令,该命令会生成一条数字线,其中包含从 1 到 10 的数字(输入 1 和 2)以及数字 3 和 5 的圆圈(输入 3 和 4),如下所示
非常感谢,温迪
答案1
香草溶液:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[-latex] (-1,0)--(10.5,0);
\foreach \x in {0,1,...,10}
\draw (\x,.1)--(\x,-.1) node[below] () {\small \x};
\node[below,shape=circle,draw,inner sep=2pt] at (3,-.1) {3};
\node[below,shape=circle,draw,inner sep=2pt] at (5,-.1) {5};
\end{tikzpicture}
\end{document}
输出:
答案2
虽然您可以使用您建议的语法,但我会使用pgfkeys
和键值接口。这更容易扩展和跟踪。我还将参数设为\NL
可选。如果没有可选参数,则会绘制一条从 1 到 10 的简单数字线,没有任何圆圈。使用可选参数,可以指定start
end
和circle
数字,以及常规 pgf/tikz 选项。
例如,
\NL
\NL[circle={3,5}]
\NL[every node/.append style={fill=blue!25!black!5},circle={2,4,6,8},start=-1,end=12,thick,draw=blue!25!black,text=blue!25!black]
产生三条数字线,其定制程度不断增加。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
% ateb: https://tex.stackexchange.com/a/711912/ addaswyd o gwestiwn Wendy Taylor: https://tex.stackexchange.com/q/711907/
\tikzset{%
numberline/.search also={/tikz,/pgf},
numberline/.cd,
start/.store in=\numberlinestart,
end/.store in=\numberlineend,
circle/.store in=\numberlinecircle,
start=1,
end=10,
circle={},
}
\NewDocumentCommand \NL { O{} }
{%
\begin{center}
\begin{tikzpicture}[x=1.07cm,y=1.07cm,numberline/.cd,#1]
\draw [Latex-Latex] (\numberlinestart-0.5,0pt) -- (\numberlineend+0.5,0pt);
\foreach \i in {\numberlinestart,...,\numberlineend}
\draw (\i,3pt) -- (\i,-3pt) node (n-\i) [below] {$\i$};
\foreach \i in \numberlinecircle
\node [circle,draw,inner sep=1.5pt] at (n-\i) {\phantom{$\i$}};
\end{tikzpicture}
\end{center}%
}
\begin{document}
\NL
\NL[circle={3,5}]
\NL[every node/.append style={fill=blue!25!black!5},circle={2,4,6,8},start=-1,end=12,thick,draw=blue!25!black,text=blue!25!black]
\end{document}
答案3
另一种风格的解决方案;只需清理代码,我添加了一个额外的变量,在绘制箭头后,当然,根据范围内每个请求的数字的要求执行它的样式。
结果:
梅威瑟:
\documentclass{article}
\usepackage{tikz}
\newcommand*\circled[1]{\tikz[baseline=(char.base)]{\node[shape=circle,draw,inner sep=2pt] (char) {#1};}}
\newcommand{\NL}[3]{
\begin{center}
\begin{tikzpicture}[scale =1.07]
\draw[latex-latex](#1-0.5,0) -- (#2+0.5,0);
\foreach \x in {#1,...,#2}{
\draw[shift={(\x,0)},color=black] (0pt,0pt) -- (0pt,-3pt) node[below] (N-\x) {$\x$};
}
\foreach \y/\z in {#3}{
\node[draw=\z,circle,minimum size=13pt] at (N-\y.center){};
}
\end{tikzpicture}
\end{center}}
\begin{document}
Numbers aligned with the text: \circled{2} \circled{4} \circled{6} \circled{9} end.
\NL{1}{10}{2/red,4/blue,6/orange,9/green!50!black}
Another result:
\NL{-3}{7}{-2/red,0/blue,4/orange,7/green!50!black}
\end{document}