5

I want to draw this formula but when I use the package Tikz the grid is not properly align. I don't know how to write the numbers inside the grid

\[
G_x  = \begin{tikzpicture}
       \draw[thick, black] (0,0) grid (2,1);
       \end{tikzpicture}
    \]

enter image description here

2

2 Answers 2

4

Here's a solution that doesn't employ TikZ.

enter image description here

\documentclass{article}
\usepackage{array}   % for 'w' column type
\usepackage{amsmath} % for '\smash[t]'
\newlength\mylen \settowidth\mylen{$-1$} % cell width in first 'array' env.
\begin{document}
\[
\setlength\extrarowheight{3pt}
\setlength\arraycolsep{2pt}    % default: 5pt
G_x=\begin{array}{|*{2}{w{c}{\mylen}|}}
      \hline -1 & 1 \\ \hline 
    \end{array}
\qquad
G_y=\smash[t]{
    \begin{array}{|c|} 
      \multicolumn{1}{c}{} \\ \hline 1 \\ \hline -1 \\ \hline 
    \end{array}
    }
\]
\end{document}
4

Here's a short solution using TikZ and its positioning library:

horizontal and vertical grids in TikZ

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}


\begin{tikzpicture}

    \def\minboxsize{20pt}
    \tikzset{boxnode/.style={draw,minimum height=\minboxsize,minimum width=\minboxsize}}

    \node (A) {$G_x =$};
    \node [boxnode,right of=A] (B) {$-1$};
    \begin{scope}[node distance=\minboxsize]
        \node [boxnode,right of=B] (C) {$1$};
    \end{scope}

    \node [right = 20pt of C](D) {$G_y =$};
    \node [boxnode,right of=D] (E) {$1$};
    \begin{scope}[node distance=\minboxsize]
        \node [boxnode,below of=E] (F) {$-1$};
    \end{scope}
\end{tikzpicture}

         
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .