![tikz 座標系中的節點](https://rvso.com/image/461894/tikz%20%E5%BA%A7%E6%A8%99%E7%B3%BB%E4%B8%AD%E7%9A%84%E7%AF%80%E9%BB%9E.png)
我想創建一個TikZ
創建坐標系的命令。到目前為止,我已經創建了這個 MWE:
\documentclass[12pt,a4paper, xcolor=dvipsnames]{scrartcl}
\PassOptionsToPackage{svgnames}{xcolor}
\usepackage{tikz}
\newcommand{\coordinatesystem}[4]{
\begin{tikzpicture}
\draw[step=1, gray!40] (#1,#2) grid (#3,#4);
\draw[-stealth,very thick] (#1,0) -- (#3,0);
\draw[-stealth,very thick] (0,#2) -- (0,#4);
\foreach \x in {#1,...,#3}
\foreach \y in {#2,...,#4}
{
\node[text=gray!30, below] at (\x,0) {$\x$};
\node[text=gray!30, left] at (0,\y) {$\y$};
}
\end{tikzpicture}
}
\begin{document}
\coordinatesystem{-5}{-5}{3}{4}
\end{document}
我希望 x 軸節點稍微向右一些,以避免與座標系線發生碰撞。同樣,我希望 y 軸節點比現在稍高。這可能嗎?
答案1
如果您不想在 xy 刻度上列印零,請像這樣修改代碼:
\documentclass[12pt,a4paper]{scrartcl}
\usepackage{tikz}
\newcommand{\coordinatesystem}[4]{
\begin{tikzpicture}
\draw[step=1, gray!40] (#1,#2) grid (#3,#4);
\draw[-stealth,very thick] (#1,0) -- (#3,0);
\draw[-stealth,very thick] (0,#2) -- (0,#4);
\foreach \x in {#1,...,#3}
\foreach \y in {#2,...,#4}
{
\ifnum \x=0
\relax%
\else %
{\node[text=gray!30, below] at (\x,0) {$\x$};}
\fi
\ifnum \y=0
\relax%
\else %
{\node[text=gray!30, left] at (0,\y) {$\y$};}
\fi
}
\node[text=gray!30] at (-.3,-.3) {$O$};
\end{tikzpicture}
}
\begin{document}
\noindent
\coordinatesystem{-5}{-5}{3}{4}\,
\coordinatesystem{-4}{-5}{4}{4}
\end{document}
輸出:
添加:無論如何,單獨的座標係是沒有用的。透過上述答案,您無法放置任何圖表或繪圖。所以你需要在 \newcommand 定義之外有 和 ,\begin{tikzpicture}
並且\end{tikzpicture}
有,例如,這樣的東西:
\documentclass[12pt,a4paper]{scrartcl}
\usepackage{tikz}
\newcommand{\coordinatesystem}[4]{
\draw[step=1, gray!40] (#1,#2) grid (#3,#4);
\draw[-stealth,very thick] (#1,0) -- (#3,0);
\draw[-stealth,very thick] (0,#2) -- (0,#4);
\foreach \x in {#1,...,#3}
\foreach \y in {#2,...,#4}
{
\ifnum \x=0
\relax%
\else %
{\node[text=gray!30, below] at (\x,0) {$\x$};}
\fi
\ifnum \y=0
\relax%
\else %
{\node[text=gray!30, left] at (0,\y) {$\y$};}
\fi
}
\node[text=gray!30] at (-.3,-.3) {$O$};
}
\begin{document}
\noindent
\begin{tikzpicture}
\coordinatesystem{-4}{-5}{4}{4}
\clip (-4,-5) rectangle (4,4);
\draw[cyan,line width=2pt] plot[domain=-4:4] (\x,-\x-1);
\draw[magenta,line width=2pt] plot[domain=-4:4,smooth] (\x,\x*\x-4);
\end{tikzpicture}
\end{document}
使用上面的程式碼,您將得到以下輸出: