![tikz を使用した座標系のノード](https://rvso.com/image/461894/tikz%20%E3%82%92%E4%BD%BF%E7%94%A8%E3%81%97%E3%81%9F%E5%BA%A7%E6%A8%99%E7%B3%BB%E3%81%AE%E3%83%8E%E3%83%BC%E3%83%89.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}
上記のコードを実行すると、次の出力が得られます。