2

Here is a TikZ code. How improve it ?

Thanks enter image description here

\documentclass{article}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[french]{babel}
\usepackage{tikz}

\begin{document}


\begin{tikzpicture}[scale = 0.7]
\def \n {7}
\foreach \x in {0,...,\n}
\foreach \y in {-\n,...,0}
{
\draw (\x,\y) +(-.5,-.5) rectangle +(.5 ,.5) ;
}


%% ligne 0
\foreach \x [count=\cx] in {0,...,\n}
\foreach \e [count=\ce] in {$a$,$b$,$c$,$d$,$e$,$f$}
{\ifnum \ce = \cx
    \node at (\x, 0) {\e} ;
\fi
}; 

%% ligne 1
\foreach \x [count=\cx] in {0,...,\n}
\foreach \e [count=\ce] in {8,9,1,2,0,4,5,6,7}
{\ifnum \ce = \cx
    \node at (\x, -1) {\e} ;
\fi
}; 

%% ligne 2
\foreach \x [count=\cx] in {0,...,\n}
\foreach \e [count=\ce] in {1,2,0,4,$\alpha$,6,7}
{\ifnum \ce = \cx
    \node at (\x, -2) {\e} ;
\fi
}; 

%% ligne 3
\foreach \x [count=\cx] in {0,...,\n}
\foreach \e [count=\ce] in {1,1,1,1}
{\ifnum \ce = \cx
    \node at (\x, -3) {\e} ;
\fi
}; 

%% ligne 4
\foreach \x [count=\cx] in {0,...,\n}
\foreach \e [count=\ce] in {7,8,9,1,2,0,4,5}
{\ifnum \ce = \cx
    \node at (\x, -4) {\e} ;
\fi
}; 


%% ligne 5
\foreach \x [count=\cx] in {0,...,\n}
\foreach \e [count=\ce] in {1,2,7,8,9,0,4,5}
{\ifnum \ce = \cx
    \node at (\x, -5) {\e} ;
\fi
}; 

%% ligne 6
\foreach \x [count=\cx] in {0,...,\n}
\foreach \e [count=\ce] in {1,2,0,4,5,$\beta$,7,8}
{\ifnum \ce = \cx
    \node at (\x, -6) {\e} ;
\fi
}; 

%% ligne 7
\foreach \x [count=\cx] in {0,...,\n}
\foreach \e [count=\ce] in {1,$x$,0,4,$y$,6,$z$,8}
{\ifnum \ce = \cx
    \node at (\x, -7) {\e} ;
\fi
}; 
\end{tikzpicture}

\end{document}
4
  • 4
    How to improve? Make it compile for those who copy your fragment. Thank you
    – MS-SPO
    Dec 10, 2023 at 22:13
  • 3
    This is probably simple and shorter to draw as TikZ matrix ...
    – Zarko
    Dec 10, 2023 at 22:32
  • 4
    do you really use \usepackage[latin1]{inputenc} in that example it accidentally works as there are no accented characters but all code posted to this site is UTF-8 encoded, and many locally installed editors will similarly default to that, as does latex. Dec 10, 2023 at 23:08
  • Thanks for this information, David.
    – Clairon
    Dec 11, 2023 at 13:31

2 Answers 2

6

As I suggested in my comment, with use of the matrix library is the code, which reproduce your picture, very concise and clear:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[french]{babel}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
    \begin{tikzpicture}
\matrix [matrix of math nodes, nodes in empty cells,
         nodes = {draw, text width=1em, text height=0.75em, text depth=0.25ex, 
                  align=center},
         column sep=-\pgflinewidth, row sep=-\pgflinewidth
         ]
{
a   & b & c & d & e & f &   &   \\
8   & 9 & 1 & 2 & 0 & 4 & 5 & 6 \\
1   & 2 & 0 & 4 &\alpha
                    & 6 & 7 &   \\
1   & 1 & 1 & 1 &   &   &   &   \\ 
7   & 8 & 9 & 1 & 2 & 0 & 4 & 5 \\
1   & 2 & 7 & 8 & 9 & 0 & 4 & 5 \\
1   & 2 & 0 & 4 & 5 & \beta
                        & 7 & 8 \\
1   & x & 0 & 4 & y & 6 & z & 8 \\
};
    \end{tikzpicture}
\end{document}

enter image description here

1
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[french]{babel}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}


\begin{tikzpicture}
\matrix (mamatrice) [matrix of math nodes, 
         nodes in empty cells,
         nodes = {draw, text width=1em, text height=0.75em, text depth=0.25ex, align=center},
         column sep=-\pgflinewidth, 
         row sep=-\pgflinewidth
         ]
{
2 & 4 & 2 & 3 & 2 & 5 & 2 & 4 \\
3 & 2 & 5 & 2 & 5 & 2 & 4 & 2 \\
2 & 4 & 2 & 7 & 2 & 4 & 2 & 2 \\
4 & 2 & 3 & 2 & 6 & 2 & 5 & 2 \\
2 & 3 & 2 & 5 & 2 & 4 & 2 & 6 \\
5 & 2 & 5 & 2 & 3 & 2 & 2 & 2 \\
2 & 4 & 2 & 3 & 2 & 4 & 2 & 2 \\
2 & 2 & 2 & 2 & 4 & 2 & 4 & 2 \\
};

\foreach \i [count = \ci] in {0,...,7} %% indexation python 
\foreach \j [count = \cj]  in {3,5,2,1,5} %% indexation python 
\pgfmathsetmacro \myj {int(\j+1)}
\pgfmathsetmacro \myi {int(\i+1)}
{\ifnum \ci = \cj 
 \draw[red, thick]   (mamatrice-\myi-\myj) circle (2mm);
 \fi
};
\end{tikzpicture}

\end{document}

image

You must log in to answer this question.

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