tikz가 있는 좌표계의 노드

tikz가 있는 좌표계의 노드

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 스케일에 0을 인쇄하지 않으려면 다음과 같이 코드를 수정하십시오.

\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}

위 코드를 사용하면 다음과 같은 결과가 출력됩니다.

여기에 이미지 설명을 입력하세요

관련 정보