我正在阅读手册中的 TikZ 教程,并尝试制作自己的示例。但是,我不明白为什么我的代码没有按预期运行。我想画一条数字线,每刻度显示一条数字线下方。然而,结果如下:
\documentclass{book}
\usepackage{tikz}
\usepackage{parskip}
\begin{document}
\begin{tikzpicture}[scale = 3]
\draw[<->, > = stealth, thick] (-2.8, 0) -- (2.8, 0);
\foreach \x in {-2, -1, ..., 2} {\draw (\x, 2pt)--(\x, -2pt);}
\foreach \x in {-2,-1, ...,2} {(\x cm, 0) node[anchor = north]{$\x$};}
\end{tikzpicture}
\end{document}
答案1
您错过了节点命令的格式:
\node at () {};
\documentclass{book}
\usepackage{tikz}
\usepackage{parskip}
\begin{document}
\begin{tikzpicture}[scale = 3]
\draw[<->, > = stealth, thick] (-2.8, 0) -- (2.8, 0);
\foreach \x in {-2, -1, ..., 2} {\draw (\x, 2pt)--(\x, -2pt);}
\foreach \x in {-2,-1, ...,2} { \node at (\x cm, 0) [anchor = north,shift={(0cm,-.2 cm)}]{$\x$};}
\end{tikzpicture}
\end{document}
使用“内联”命令的更好选择node
可能是:
\documentclass{book}
\usepackage{tikz}
\usepackage{parskip}
\begin{document}
\begin{tikzpicture}[scale = 3]
\draw[<->, > = stealth, thick] (-2.8, 0) -- (2.8, 0);
\foreach \x in {-2, -1, ..., 2} {\draw (\x, 2pt)--(\x, -2pt) node [below] {\x};}
\end{tikzpicture}
\end{document}