我有以下代码来绘制四个轴:
\begin{tikzpicture}
\draw (-3,0) -- (3,0) node[below];
\draw[shift={(0,0)}] (0pt,2pt) -- (0pt,-2pt) node[below];
\draw (0,-3) -- (0,3) node[left];
\draw[shift={(0,0)}] (2pt,0pt) -- (-2pt,0pt) node[left];
\node[below] at (0,-3.20) {\footnotesize label1};
\node[above] at (0,3.20) {\footnotesize label2};
\node[right] at (3.20, 0) {\footnotesize label3};
\node[left] at (-3.20, 0) {\footnotesize label4};
\end{tikzpicture}
编译时出现以下错误:
"! Package tikz Error: A node must have a (possibly empty) label text"
还有其他无错误的标记方法吗?
答案1
您需要为 指定一个标签文本node
,一个空的{}
就足够了:
\begin{tikzpicture}
\draw (-3,0) -- (3,0) node[below] {}; % empty label text
\draw[shift={(0,0)}] (0pt,2pt) -- (0pt,-2pt) node[below] {}; % empty label text
\draw (0,-3) -- (0,3) node[left] {}; % empty label text
\draw[shift={(0,0)}] (2pt,0pt) -- (-2pt,0pt) node[left] {}; % empty label text
\node[below] at (0,-3.20) {\footnotesize label1};
\node[above] at (0,3.20) {\footnotesize label2};
\node[right] at (3.20, 0) {\footnotesize label3};
\node[left] at (-3.20, 0) {\footnotesize label4};
\end{tikzpicture}
如果您只想要一个命名的位置,您可以简单地使用coordinate
。 在您的例子中,\draw
命令中的节点似乎是不必要的(它们没有标签文本,也没有名称以供以后引用),更简单的方法是将节点绘制在路径内:
\begin{tikzpicture}
\draw (0,-3) node[below] {label1} -- (0,3) node[above] {label2};
\draw (-3,0) node[left] {label4} -- (3,0) node[right] {label3};
\end{tikzpicture}
答案2
{}
我们在节点末尾添加。我附上了代码,我们可以运行任何 LaTeX 引擎。
% *latex mal-nodes.tex
\documentclass[a4paper]{article}
\pagestyle{empty}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw (-3,0) -- (3,0) node[below] {}; % We added {} here,
\draw[shift={(0,0)}] (0pt,2pt) -- (0pt,-2pt) node[below] {}; % change is here,
\draw (0,-3) -- (0,3) node[left] {}; % plus here,
\draw[shift={(0,0)}] (2pt,0pt) -- (-2pt,0pt) node[left] {}; % and also on this line.
\node[below] at (0,-3.20) {\footnotesize label1};
\node[above] at (0,3.20) {\footnotesize label2};
\node[right] at (3.20, 0) {\footnotesize label3};
\node[left] at (-3.20, 0) {\footnotesize label4};
\end{tikzpicture}
\end{document}