绘制数轴

绘制数轴

我正在阅读手册中的 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}

相关内容