问:见附图,我该如何实现?我想在实线的两侧画箭头,并在箭头里面画一些文字。
梅威瑟:
\documentclass[12pt]{article}
\usepackage[bindingoffset=0.2in,left=0.5in,right=0.5in,top=0.5in,bottom=0.5in,footskip=.25in]{geometry}
\usepackage[centertags]{amsmath}
\usepackage{latexsym}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{newlfont}
\usepackage{enumerate}
\usepackage{makeidx}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\usepackage{xparse}
\usetikzlibrary{backgrounds,intersections}
\begin{document}
\begin{tikzpicture}[xscale=1.5]
\draw[thick,latex-latex] (-5,0) -- (6,0)node[right]{};
\foreach \x in {-4,-3,-2,-1,0,1,2,3,4}{
\node[fill,circle,inner sep=1.20pt,label=below:$\x$] at (\x,0) {};
};
\end{tikzpicture}
\end{document}
答案1
这是通过改变节点的形状来实现的一种方法。请参阅代码中的注释。请参阅 tikzmanual 中的第 71.5 章和第 17.2.3 章。
\documentclass[12pt]{article}
\usepackage[bindingoffset=0.2in,left=0.5in,right=0.5in,top=0.5in,bottom=0.5in,footskip=.25in]{geometry}
% ~~~ commented out all that's not needed here ~~~~~~~~~
%\usepackage[centertags]{amsmath}
%\usepackage{latexsym}
%\usepackage{amsfonts}
%\usepackage{amssymb}
%\usepackage{amsthm}
%\usepackage{newlfont}
%\usepackage{enumerate}
%\usepackage{makeidx}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
%\usepackage{xparse}
%\usetikzlibrary{backgrounds,intersections}
\usetikzlibrary{shapes.arrows}
\begin{document}
\begin{tikzpicture}[
xscale=1.5,
dots/.style={fill,circle,inner sep=1.20pt},
]
\draw[thick,latex-latex] (-5,0) -- (6,0)node[right]{};
% \foreach \x in {-4,-3,-2,-1,0,1,2,3,4}{% can be shortened
\foreach \x in {-4,-3,...,4}{
\node[dots,label=below:$\x$] at (\x,0) {};
};
% ~~~ arrows ~~~~~~~~~~~~~~
\node[ single arrow, % node shaped like an arrow
anchor=west, % i.e. the postion below
fill=green!40,draw=blue
]
at (0.3,1) {Some text};
\node[ single arrow,
anchor=east,
fill=red!40,draw=purple,
shape border rotate=180, % rotates shape, not text
]
at (-0.3,1) {Some more text};
\end{tikzpicture}
\end{document}
附言对于彩色点,只需拆分循环并执行以下操作:
\foreach \x in {-4,-3,-2,-1}{
\node[dots,,fill=red,label=below:$\x$] at (\x,0) {};
};
\foreach \x in {1,2,3,4}{
\node[dots,fill=green,label=below:$\x$] at (\x,0) {};
};