我正在尝试寻找一种更好的方法,在简单的单轴上每个点上方添加一个字母。我使用这个得到了我需要的结果:
\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1.0cm,y=1.0cm]
\clip(-8,-1) rectangle (5.5,2);
\draw[<->,color=black] (-5.4,0.) -- (5.4,0.);
\foreach \x in {-5,-4,-3,-2,-1,0,1,2,3,4,5}
\draw[shift={(\x,0)},color=black] (0pt,2pt) -- (0pt,-2pt) node[below] {\footnotesize $\x$};
\draw[color=black] (-5,.4) node {$A$};
\draw[color=black] (-4,.4) node {$B$};
\draw[color=black] (-3,.4) node {$C$};
\draw[color=black] (-2,.4) node {$D$};
\draw[color=black] (-1,.4) node {$E$};
\draw[color=black] (0,.4) node {$F$};
\draw[color=black] (1,.4) node {$G$};
\draw[color=black] (2,.4) node {$H$};
\draw[color=black] (3,.4) node {$I$};
\draw[color=black] (4,.4) node {$J$};
\draw[color=black] (5,.4) node {$K$};
\end{tikzpicture}
但我不得不想象有一种不那么“暴力”的方式来做到这一点。有什么建议吗?(PS 我对此很陌生)。
答案1
你半途而废了……你应该利用引入的循环:
\documentclass[tikz,
border=3mm,
]{standalone}
\begin{document}
\begin{tikzpicture}
% axis
\draw[<->] (-5.4,0.) -- (5.4,0.);
% ticks with labels
\foreach \x [count=\xx from -5] in {A,B,...,K}
\draw (\xx,1mm) node[above] {$\x$}
-- + (0,-2mm)
node[below,font=\footnotesize] { $\xx$};
\end{tikzpicture}
\end{document}